I have a form where I need to use some conditional rendering. Basically, the form changes dynamically based on what the user input is. For instance, if I ask “Where did you hear about us?” and give the users some default options (e.g. “Linkedin”, “Our website”…), I want that if the user selects “Other” a st.text_input appears where the user can type the answer to the question.
The problem I am facing is that:
If I use st.form (with the st.submit_form_button), what happens is that the form does not dynamically adapt to the user’s input. So the text_field won’t show up at all when the user ticks “Other” in the example above.
If I do not use st.form, then the form reloads every time the user clicks on any widget. This does not affect the functionality of the form, but it does make for a very bad user experience!
Any tips on how I could either include conditional rendering within st.form or just avoid the bad user experience of the form being reloaded every time the user clicks on any widget?
Hi @tommaso-moro , you cant use Forms if you want to conditionally check for widget values within the form (as forms batch-submit inputs together).
You can, however, use read up about session states in the Streamlit docs, which will help you persist widget values despite reload. Here is the link: Session State - Streamlit Docs
For the reason you pointed out, I have ditched st.form so that I can make interdependent widgets. I am using the Session State. However, while the values persist across page reloads, the page does reload every time the user clicks on a widget. This doesn’t impact the functionality of the form (because the values persist across page reloads) but it makes for a very bad user experience.
Do you have any tips on how the user experience can be improved?
I don’t think you can escape the page reload part as it is how Streamlit is designed to work. You could try other frameworks (like Flask, Justpy, etc.), or another programming language/tool, but you wont get the simplicity and ease of use that Streamlit offers you, and there will be a more investment of your time to perfect that learning.
Maybe, a Streamlit insider can opine (in case of any new developments)
Try st.stop. You could collect input with reruns up to a certain point in your page, thus allowing widgets to be shown/hidden, and then allow the whole page to run based on your logic. Or, use a workflow (multi-tab) type of data input gathering. Or, implement the flexible data gathering form as a static HTML component.
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.