Something like this below should work. You’ll need the latest version of Streamlit for the form button disabled
parameter. I use the form’s submit button on_click
callback to ensure st.session_state.CONNECTED
is set before the form’s post-submission state is evaluated, since st.session_state.CONNECTED
is used to control the disabled states of the form’s submit buttons. It is also used to gate the select box. The key idea is to keep all the form state inside the form and if any of the form’s state is used outside the form, then put it in a session state variable. It’s a bit more work, but reaps dividends in the long run.
HTH,
Arvindra
import streamlit as st
if 'CONNECTED' not in st.session_state:
st.session_state.CONNECTED = False
def _connect_form_cb(connect_status):
st.session_state.CONNECTED = connect_status
def display_db_connection_menu():
with st.form(key="connect_form"):
c1, c2 = st.columns(2)
with c1:
if st.form_submit_button("🟢 Connect", on_click=_connect_form_cb, args=(True,), disabled=st.session_state.CONNECTED):
pass
with c2:
if st.form_submit_button("🔴 Disconnect", on_click=_connect_form_cb, args=(False,), disabled=not st.session_state.CONNECTED):
pass
def display_table_selection():
if st.session_state.CONNECTED:
option = st.selectbox('Select table name', ['Table 1', 'Table 2', 'Table 3'], index=0, key='option')
st.write(f'Selected: {option}')
display_db_connection_menu()
display_table_selection()