Session state-submit-= st.form_submit_button("Connect")

Hi Team,
I have two functions:
def menu:
with st.form(key=“my_form”):
.
.
global submit
submit = st.form_submit_button(“Connect”)
def options()
if submit:
option = st.selectbox(‘Select table name’, df,key=‘option’)
The requirement is that after submit button is hit, the option should appear.
As soon as I change the selection, option is disappearing. I tried to use session state so that the option should not disappear as soon as I select something from the drop down. However, I could not grab how to implement session state in this scenario
It would be much appreciated if you can help me fix this issue please?

Hi Team,
Is there an update on this? It’s a bit urgent.

Hi - can you post a minimally working version of your code, even if it doesn’t do what you want it to. Refer to this post to structure your question.

Arvindra

1 Like

def display_db_connection_menu():
with st.form(key=“my_form”):

         submit = st.form_submit_button("Connect")

def display_table_selection():
if submit:

        option = st.selectbox('Select table name', df,index=0,key='option')

display_db_connection_menu()

display_table_selection()

Hi @asehmi Thank you for your reply.
If I select the option st.selectbox() is disappears. I know it could be fixed with session state, but I don’t know how to apply session state to my app

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()

form_multiple_submit_buttons

1 Like

Hi @asehmi, with a bit of tweaking, my code started working. I can’t thank you enough mate. Your help is appreciated. God bless you!

1 Like

Great to hear! (Please mark this as your solution?).

Hi @asehmi, I marked it as a solution already.

Warm regards,
Omer

1 Like