Multi-page apps with widget state preservation. The simple way

st.session_state.active_page = st.session_state.active_page
st.session_state.slider1     = st.session_state.slider1
st.session_state.check1      = st.session_state.check1
st.session_state.slider2     = st.session_state.slider2
st.session_state.check2      = st.session_state.check2

Actually that’s what st.session_state.update(st.session_state) does for every item of your session state, I’m just lazy to specify each one of them like that :stuck_out_tongue:

Internally, Streamlit manages two different states : user-defined states (used when you store values like so: st.session_state.my_state = "hey"), and widget states (when you use a key parameter). These two states work a little bit differently. User-defined states are completely persistent after multiple runs. However if a widget with a key assigned disappear (when your page changes for example), its associated widget state will be cleared.

So to make widget state persistent, the trick is to transform a widget state into a user-defined state. And it is done by self-assigning session state items that were created by a widget, either with st.session_state.update(…), or like you’ve done it in your last example.

In practice, you don’t have to manage those two different states. What Streamlit does instead is, behind the scene, it merges both states into one single object (st.session_state) you can use in your scripts.