Hi all, I’ve tried searching through the forums and found a couple related posts that weren’t quite what I wanted, so I’ll post another here.
Desired behavior:
In a form I want toggle-able optional inputs, that resets (and hides) after form submission
Attempts:
Using a checkbox, and an if-statement right after the checkbox that creates the new inputs of the checkbox is clicked. From my understanding this is common practice outside of a form, but within a form this doesn’t work b/c nothing is evaluated until the submit button is pressed, and callbacks aren’t allowed within a form.
Tried using an expander. However, the expander stays open after submitting the form, when I want the expander to reset to closed. For reference, I do set clear_on_submit = True.
Thanks! I have a related but different problem now, where a button only closes the expander the first time. But if one manually opens the expander, the button no longer works:
import streamlit as st
st.write("The button can only close the expander the first time! And not after the expander is manually opened.")
if "expander_state" not in st.session_state:
st.session_state["expander_state"] = True
def toggle_closed():
# <potentially do other actions>
st.session_state["expander_state"] = False
st.button("do stuff then close expander",on_click=toggle_closed)
with st.expander("test expander",expanded = st.session_state["expander_state"]):
st.write("expander is open")
It seems to me that buttons can only toggle expanders if the expanded keyword in the expander definition changes.
The challenge is that one can’t ensure that the expander is only opened by a button that toggles the expander definition (indeed, the intuitive thing for people is to open the expander directly instead of via some other toggling button, which is also a clunky UX experience).
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.