It’s my first day with streamlit – apologies if the question is basic.
I am having an issue of keeping widget state for a multipage app where widgets are displayed conditionally.
Below is a minum example: as long as I stay on page 1 the choices is preserved as I modify the selection. If I move to page 2 and then go back to page 1, it get lost from the state for some reason.
Is this a bug?
Many thanks in advance
import streamlit as st
if 'choice' in st.session_state:
st.write(f'choice is in the session state and the value is {st.session_state.choice}')
else:
st.write(f'choice is not yet in the session state ')
navigation = st.sidebar.radio('Navigation', [1, 2])
if navigation == 1:
st.multiselect('Choice', [1, 2, 3], key='choice')
Answering my own question if it may help anyone: this seems to be an intended (but unintuitive) feature,
it is being tracked over here together with a workaround:
opened 12:51PM - 31 Jan 22 UTC
closed 01:12AM - 02 Feb 22 UTC
bug
needs triage
### Summary
Session state in Streamlit 0.89.0 to 1.5.0 has a strange behavior… when bound to widgets (`key` parameter).
### Steps to reproduce
#### First case
1. In the same script run, initialize a session state item, and display a widget bound to it.
2. Make the widget disappear (by unchecking a checkbox for instance).
3. Reload with `R`. **ISSUE: Session state item doesn't exist anymore.**
4. Reload again multiple times. **ISSUE: Session state is cleared every other run.**
#### Second case
1. In a first script run, initialize a session state item.
2. In a second run, display a widget bound to the previous session state item (by checking a checkbox for instance).
**ISSUE: Widget does not take value from session state.**
3. Change widget value, it updates the session state item correctly.
4. Make the widget disappear.
5. Reload with `R`. **ISSUE: Session state item resets to its initial value**.
#### Code snippet
Feel free to compare the following script behavior with Streamlit 0.88.0 (working session state) and Streamlit 1.5.0. Both cases are implemented.
```python
import streamlit as st
st.set_page_config(layout="wide")
c1, c2 = st.columns(2)
with c1:
st.subheader("Widget session state item 1")
st.write("Session state and widget setup in the same run.")
st.write("When widget disappear, session state item is cleared every other run.")
with st.echo(code_location="below"):
if "widget_bound" not in st.session_state:
st.session_state.widget_bound = "hello"
st.success(f"Item added: {st.session_state.widget_bound}")
else:
st.info(f"Item exists already: {st.session_state.widget_bound}")
# Displayed from the beginning by default
if st.checkbox("Display widget", True):
st.text_input("Some widget", key="widget_bound")
with c2:
st.subheader("Widget session state item 2")
st.write("Session state setup first, then widget in another run.")
st.write("Widget does not get value initial value from session state.")
with st.echo(code_location="below"):
if "widget_unbound" not in st.session_state:
st.session_state.widget_unbound = "hello"
st.success(f"Item added: {st.session_state.widget_unbound}")
else:
st.info(f"Item exists already: {st.session_state.widget_unbound}")
# Displayed after session state initialization
if st.checkbox("Display widget", False):
st.text_input("Some widget", key="widget_unbound")
```
### Is this a regression?
Yes, session state with widgets worked as expected from version 0.86.0 to 0.88.0.
### Debug info
- Streamlit version: 0.89.0 to 1.5.0
- Python version: 3.8.6 (pyenv)
system
Closed
March 31, 2023, 8:42am
3
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.