would st.form work for u? There’s a parameter called clear_on_submit that you can set to True so that it clears all the selectboxes.
with st.form("my_form",clear_on_submit=True):
st.write("Inside the form")
slider_val = st.slider("Form slider")
checkbox_val = st.checkbox("Form checkbox")
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
if submitted:
st.write("slider", slider_val, "checkbox", checkbox_val)
st.write("Outside the form")
i have a situation like 2 selectboxes, 1 with regions and another 1 one with countries. how to write the session state for both selectboxes such that when region APAC is selected, countries related to APAC region only to be visible as option and vice versa.
No need to put them in a form or have callbacks. Simply have the Regions and Countries selection code blocks following each other. Filter the countries based on the region before handing it to the Countries selection. Whenever a region is selected, Streamlit will rerun preserving the selected region, then you can region-filter the countries list to pass to the Countries selection.
Thanks for your time, i dont want to keep region as a parent always. client expecting region and country both as individual. your current flow region->country->city. what if i want to select the country directly
Please feel free to modify the example I made for you. Instead of filtering the countries based on regions, just build an all countries list and use that selection as is to filter its cities.
is it possible to check and uncheck the st.checkbox when i am changing the selectbox options.
i tried many possiblity using session state. can you help me out.
Are your checkboxes inside a form? If so, then it isn’t possible to do this dynamically without a rerun, and you can’t do a rerun in a callback, so that’ll need to be done based on the value of another state variable outside the form, and this you can set in the callback.
@asehmi Both Selectbox and checkbox are not inside form. With your help, i created two selectboxes for region and country. Parent 1st is Region, once Region selected, 2nd Country confined to region was given as option for selectbox 2. now once i selected options from selectbox2. i want to check and uncheck the checkbox as when i change the options of 2nd select box.