New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expander resets its expanded state when new elements are included. #2360
Comments
Probably related: #2241 |
Is there an update on this issue? |
Any update on this? I built a webapp with streamlit for my team and this behaviour happens for half the team and not for the rest. Seems weird and I can't think of or find a resolution. But because it doesn't happen to some people (myself included), i'm assuming its not related to the code i've written and possibly to something else. Any ideas? |
Hi @simonlipsonrisk I can try to provide guidance, but would need to look at the code. The main challenge that always exists is connecting the actual line of code to the specific element in the DOM. In many cases, we connect it correctly, but expanders have been unique as a layout feature and not an element. This is a result of an element appearing or disappearing before causing a misalignment in this connection. For the example above, The following workaround would work. import streamlit as st
checked = st.checkbox("I have something")
placeholder = st.empty()
if checked:
placeholder.write("Checked")
with st.expander("expand"):
a = st.selectbox("Choose 1", ['1','2']) Note the use of Hope that helps! |
Thanks @kmcgrady . I'm not sure its relevant to my situation. I'll give a short version of the code that captures what happens. Basically I have check boxes within the expander and for some people, when the box is checked or unchecked, the expander closes. whereas for others it does not close. The code is something similar to
Does it make sense that checking or unchecking the checkbox in the above could result with the expander closing? And could the st.empty() help in this case? Thanks |
Hmmmm @simonlipsonrisk can't say I understand what's going on here. This issue corresponds to items around the expander appearing and not, vs. the expander itself. We did some work recently with regards to multiple expanders #4290 in case you are not working on the latest version. That being said, if you have a repro case with a small code sample that demonstrates the issue, it could be investigated. |
@kmcgrady I found the cause of my issue (however not sure what the real underlying issue is). The people in my team that have the latest version (1.7.0) of streamlit experience the closing expanders whereas those of us that have version 1.2.0 do not experience this issue. I even upgraded my streamlit to 1.7.0 and did experience the same issue where the expanders close. So definitely seems it due to difference in version. That being said, I'm not sure what it is about 1.7.0 thats making those expanders close. I also tried replicating it with a smaller webapp however wasn't able to replicate. If I do succeed, i'll send you the code. |
Ok @kmcgrady I worked out the issue. To spice-up the webapp, I wrote a function that returns a random emoji from a list and then would call that function when creating the title for each expander
Whenever anything happened within the expander, it calls that random_emoji function again and updates the emoji in the title. As its technically a new title, I guess there can be an argument that it should be closed as its a new expander and expanders by definition start closed. In streamlit 1.2.0 it does not close but in streamlit 1.7.0 it does. I am not sure what behaviour makes more sense. But I do know that it was wrong (and lazy) of me to call the function within the title string and not set it as a variable before and just inject the variable into the title after. Here's code that replicates it and you can see for yourself and decide what behaviour is best in this case.
|
Ah yea @simonlipsonrisk Thanks for the write up! That makes sense. We had made the decision that an expander is a layout element. An argument can be made that an expander is like a widget (text input, etc) with a state. In that case, one solution is the |
I think I'm also hit by this, on v1.12.2. |
On 1.23.1 Setting the expanded flag to True in the rerun when the expander label was changed keeps it open for me. Only once probably, but thats enough for my case, since I add a little emoji if the user edited the data inside. |
Similar underlying issue to #6257 |
I made a demo app to show the behavior: https://container-state-bugs.streamlit.app/Expander_example Note the same thing happens with Having some discussions within the Streamlit team about whether / how we address this long term. |
Thank you all for your input on this issue, it is sincerely appreciated! 😄 Since this is expected behavior for the Feel free to upvote the request, as its helpful to us in gauging interest & priority. Really appreciate your usage of Streamlit & engagement in making it even better!! 🎈 |
Summary
A expander resets when there are changes in the DOM (like an element is inserted before it). This is likely because the element is unmounted and mounted triggering a reset to the expander itself
Steps to reproduce
Expected behavior:
The expander should remain open with "Checked" appearing above it.
Actual behavior:
The expander collapses
Is this a regression?
No
The text was updated successfully, but these errors were encountered: