I'm building a Streamlit multipage application and am having trouble keeping session state when switching between pages. My main page is called mainpage.py and has something like the following:
import streamlit as st
if "multi_select" not in st.session_state:
st.session_state["multi_select"] = ["abc", "xyz"]
if "select_slider" not in st.session_state:
st.session_state["select_slider"] = ("1", "10")
if "text_inp" not in st.session_state:
st.session_state["text_inp"] = ""
st.sidebar.multiselect(
"multiselect",
["abc", "xyz"],
key="multi_select",
default=st.session_state["multi_select"],
)
st.sidebar.select_slider(
"number range",
options=[str(n) for n in range(1, 11)],
key="select_slider",
value=st.session_state["select_slider"],
)
st.sidebar.text_input("Text:", key="text_inp")
for v in st.session_state:
st.write(v, st.session_state[v])
Next, I have another page called 'anotherpage.py' in a subdirectory called 'pages' with this content:
import streamlit as st
for v in st.session_state:
st.write(v, st.session_state[v])
If I run this app, change the values of the controls and switch to the other page, I see the values for the control being retained and printed. However, if I switch back to the main page, everything gets reset to the original values. For some reason st.session_state
is cleared.
Anyone have any idea how to keep the values in the session state? I'm using Python 3.11.1
and Streamlit 1.16.0