-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Widget output is different than widget state #6146
Comments
Thanks for sharing these findings, @MathCatsAnd! |
I was able to reproduce this. The widget having the same return value after switching pages isn't surprising, with how widget identity works, so the mystery is why the component in the frontend is reset. Which I should be able to dig into this week. |
I understood the reverse to be true: a widget should reset if you switch pages (unless you do something to manually copy the data stored in the session state key). At least, that's how I've understood the staff responses in the linked issues above. |
I just went through and closed a few other issues that were having confusion around this underlying behavior. I think this is a pretty gnarly bug. I am wondering with #7003 merged if it's any easier to address now. We should discuss! |
It is not directly easier to address with the widget stability work. The frontend clears all its state when a new session is started, which happens when switching to a new page. The backend preserves session state (including widget state), so the identical widgets on different pages are seen as the same and the state transfers. There are some potential uses for sharing the widget state across pages like this, but widgets on different pages being distinct is what I think most people expect, and usecases for sharing would make sense to address as part of potential MPA widget persistence work. The work required for this is pretty reasonable, so I expect to have a fix soon. |
Should be fixed in the next release |
@AnOctopus What is the behavior of the merged fix in the following scenario
In this scenario, when going back from Page 2 to Page 1 the displayed value of the widget in the browser will have been reset, but, assuming that the Page 1 hash has not changed, the value returned by the There might be a way out by relying on the functionality where widgets that have not been seen on a given run are removed from state (thus, when Page 2 runs, the Page 1 widget's data is cleared), but I'm not sure if that works as expected for widgets with user keys, and I'm having trouble getting the master branch of streamlit to build on my workstation right now (it's an ENV thing) so I cannot actively test this hypothesis. |
@Asaurus1 By switching from Page 1 to Page 2, the widget from Page 1 gets deleted from memory and now you have the widget from Page 2 in memory. Switching back to Page 1 is the same. Since Streamlit deletes widget information as soon as the widget is not rendered, you will observe resetting behavior without other steps to keep that information between pages. This should be the same whether or not a user-defined key is passed. PS In case you didn't know, |
Hi, everyone! Do we have any update on this issue? Thanks in advance |
It was fixed in Streamlit 1.27 |
Hi, Joshua! Thanks for the answer! Additionally, I'am still having a problem regarding the session state of the widgets when changing pages. How can I solve it? Thanks again! |
I forgot to mention that the widget I'm talking about is st.selectbox |
@heitormarcal ah, this is intended behavior currently. We're considering a change or a better way to support this, see #6074. In the meantime, the best way to do it today is documented here. |
Hi, Joshua! I'm trying to do what is documented as below:
and my widget is: However, when I choose 'B', change the page and then return to the original page, the value is back to 'A'. Do you know what might be happening? Thanks a lot in advance! |
I used the suggested best way to do it. It works well when dealing with 1 multi-select on the side bar. However, when I do it for 2 multi-select and clear out one of the multi-select boxes to re-select, both of the multi-select disappeared and I got an error. Any idea why?
Current code:
|
These questions with code examples will be better answered on https://discuss.streamlit.io/ The code examples in the prior two comments don't seem to match the one in the docs |
Checklist
Summary
This is likely deeply tied to many existing issues regarding the widget cleanup process and confusion over expected behavior. However, I believe it is distinct in that it is a true bug.
As of current, it is expected behavior for widgets with the same key on different pages to not sync. However, I am observing a case where widgets with the same specification (with or without a manually assigned key), will retain the output of a previously rendered instance (unexpected) while forgetting the state (expected).
Related issues regarding widget states:
#6074 [Request for input], considering changes
#5813
#5591
#4989
#4458
I tried this in 1.18.0 and also jumped back to 1.13.0: same bug in both.
Reproducible Code Example
Steps To Reproduce
Expected Behavior
The output should match the widget state. If state is forgotten, then the output needs to revert to the default value as well.
Current Behavior
Widgets are only half forgetful of their twin's state on a previous page: widget state resets to default, but output does not.
Is this a regression?
Debug info
Additional Information
No response
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: