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
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()
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!
Great to hear! (Please mark this as your solution?).