Session state is not preserved when navigating pages

Summary

Hi,
I am new to streamlit and I want to know if it is possible to preserve session state variable when navigating through pages. I am writing the pages as separate .py files in pages folder. I noticed that when I input text in a page then go to another page the text state is preserved only for 1 click after.

Steps to reproduce

Code snippet:

import streamlit as st

st.text_input('file path', key=tmp_filepath)
if len(st.session_state.tmp_filepath)
  st.session_state.filepath=st.session_state.tmp_filepath

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

I want to input text in one page. Be able to use it in different pages and for it to be not be updated or erased unless I re-input the text.

Actual behavior:

Text input is only preserved after 1 page navigation

Hi @royaad,

Sorry for the late reply. The short answer is yes, Session State is preserved between pages, but there is a caveat when you specifically mean a key-value pair associated to a widget. Streamlit deletes key-value pairs for widgets when they are not rendered on an app run. In particular, it gets deleted at the end of the app run. This gives the appearance of being available for β€œone click” on the new page. In reality, by the time the page has finished loading, the information has been deleted. I recently wrote a longer guide that explains this behavior of widgets and how to work with it in multipage apps or more complex scenarios.

Widget behavior guide

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.