If a key is tied to a widget, it will get deleted from session state if that widget disappears (whether conditionally not rendered on the same page or from navigating to a different page). If you have two widgets on two different pages with the same parameters/key, Streamlit will still recognize them as different widgets so you will in effect get the key deleted and a new one created on the same page load if you switched pages with an attempt to connect it up to a new widget.
There are two solutions to this:
- Save information into a different key in session state not associated to a widget so it is protected from deletion.
- Recommit widget keys you want to persist across pages at the top of every page:
#list of keys tied to widgets that you want to protect
keeper_list = ['widget_key_1', 'widget_key_2']
for key in keeper_list:
if key in st.session_state:
st.session_state[key] = st.session_state[key]
What this trivial assignment does is interrupt the widget cleanup process, thereby protecting it from getting deleted.