# Session state is not preserved when navigating pages

**URL:** <https://discuss.streamlit.io/t/session-state-is-not-preserved-when-navigating-pages/48787>\
**Category:** Using Streamlit\
**Created:** [August 7, 2023, 7:31am UTC](https://discuss.streamlit.io/t/session-state-is-not-preserved-when-navigating-pages/48787 "2023-08-07T07:31:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![royaad](https://avatars.discourse-cdn.com/v4/letter/r/258eb7/32.png) [@royaad](https://discuss.streamlit.io/u/royaad)\
**Post date:** [August 7, 2023, 7:31am UTC](https://discuss.streamlit.io/t/session-state-is-not-preserved-when-navigating-pages/48787/1 "2023-08-07T07:31:36Z")

</div>

### 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:**

```auto
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

---

<div class="post-metadata">

**Author:** ![mathcatsand](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/mathcatsand/32/48006_2.png) [@mathcatsand](https://discuss.streamlit.io/u/mathcatsand)\
**Post date:** [October 7, 2023, 2:37am UTC](https://discuss.streamlit.io/t/session-state-is-not-preserved-when-navigating-pages/48787/2 "2023-10-07T02:37:34Z")

</div>

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](https://docs.streamlit.io/library/advanced-features/widget-behavior)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [April 4, 2024, 2:37am UTC](https://discuss.streamlit.io/t/session-state-is-not-preserved-when-navigating-pages/48787/3 "2024-04-04T02:37:58Z")

</div>

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