How to reset the selectbox to default option

Hi Team,

I am developing an application which has five selectbox widget, currently as soon as the widget is selected with option, i am doing computation.

I dont know to go back to default selectbox option. Kindly do the needful.

is there a way to create a button and reset the whole page rerun

Hi,

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.

Example gist
st_cascade_selection.py

Demo

Hi @asehmi

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.

@asehmi

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.

What’s the checkbox used for? Can you post a screen shot… better still please post the code, as per the usual guidance on posting?

Checkbox used for selecting type of tuning computation. sure i will try and post the code

Which do you want:

  1. Checking or unchecking?

When do you want that to happen:

  1. When the first select box value changes?
  2. When the second select box value changes?
  3. When only specific values in the second select box are selected?

I want to uncheck the checkbox when second selectbox value is changed

Can you post your code here so I can take a look and perhaps use it as a starting point, if I have time?