Get expanded state of st.beta_expander

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!

1 Like

Hello @poroia , welcome to the community!

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?

Have a nice day,
Fanilo :balloon:

One handy thing to do would be to not run code inside the expander when it’s closed. Good for hiding big slow things unless wanted.

1 Like

Thank you for your response, @andfanilo!

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.