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)