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.
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.