Is it possible to get the current expanded state of st.beta_expander?
For example, like this:
expander = st.beta_expander("Expander")
print(expander.expanded)
with expander:
# ...
I was looking through layouts.py which led me to delta_generator.py but couldn’t find anything on a possible state. Perhaps it wasn’t ever transferred to python and remained only in the frontend. Any help would be amazing. Thanks in advance!
For now the expanded info is not sent back to Python. Since the team is currently working on state, we would be very interested in hearing about your usecase: what are you trying to achieve by detecting changes in the expander state?
My use-case is mainly for more intuitive UX. I have a form in which the submit button closes the expander around the form in order to show the results, all controlled by a variable in session state. (Note that, therefore, expanded parameter is set to this variable). But since the variable is unaware of when the user interactively opens or closes the expander, if an event triggers a page re-run (like navigating between pages in a multi-page app), then the expander will use the state in the session, which may not be what the user had just opened the input to.
Also it’s great to hear the team is working on making streamlit stateful! I’ve been using the session state hack and while it does work pretty well, it’s been pretty janky at times.
In my use-case, I have two expanders and each has a radio list in it. I want to be able to say - if one expander is expanded (expander_state = True) then radio_selection would use the selection in this expander.
The way the expander is currently, you don’t have access to its current state from backend. The user clicking to expand or collapse the expander is something that happens entirely in the browser. So it would be a feature request to add this functionality.
Here is a feature request where you can vote on this:
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.