Hello Community,
I am trying to create a nested structure of buttons and selectbox.
list_city_from_db
city=st.selectbox(" Select a city",list_city_from_db)
b1=st.button("See further options")
if b1:
suburbs=st.selectbox("Select Suburb",sub)
if suburbs:
col1,col2 = st.columns(2)
with col1:
select_bus=st.selectbox("Available Bus",bus_schedule)
bus_timetable=st.download_button("Download Bus timetable",data=df.to_csv(),....)
with col1:
select_train=st.selectbox("Available Train",Train_schedule)
train_timetable=st.download_button("Download train timetable",data=df.to_csv(),....)
The issue that I am facing is when I select the suburb after entering the city, it disappears.
What should I do to avoid this issue? Is the session state the solution? for answers to questions.
I don’t know if it’s the only solution, but yes, session state would be an easy fix for this.
Just define a function that changes the session state variable to 0 if it is 1, or 1 if it is 0. Call the function whenever the button is clicked with the on_click option in st.button.
A User first enters a city and hits the button. Then our session state will be 1. We will be showing the suburbs since state=1. Then user changes the city and hits the button . Now, session state will be 0. and we will not show suburb.
I have a list of cities that is displayed in a selectbox.
Once the user has selected the city, we want the user to hit the button so we will show the user another selectbox that will have a list of suburbs.
After selecting the suburb we will show Available bus and train timetable as showed in sample code.
This would work if we did not want to include download button. Unfortunately, I want to include a download button. But thanks for your time and comments.
Sorry, I don’t understand.
You can add a download button outside of the form!
Only this bit is the form
with st.form('my_form'):
city=st.selectbox(" Select a city",['please select one'] + list_city_from_db)
st.write(city)
submitted = st.form_submit_button("Submit")
if submitted:
st.session_state['my_city'] = city
You will add your download button here
if st.session_state['my_city'] is not None:
suburbs=st.selectbox("Select Suburb",['please choose one'] + sub[st.session_state['my_city']])
if suburbs != 'please choose one':
mybus=st.selectbox("Select bus",['please choose one'] + bus_schedule)
if mybus != 'please choose one':
st.write(mybus)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.