Issue with sharing widget info using session_state across pages

Hi there,

I’m creating an app where I want some information (st.text_input) to be shared across two pages. On page_1 I saved the st.text_input to st.session_state as follows:

text_info = col1.text_input(‘Name’, key=‘name’)

I then use the following on page_2 to check that the variable has been saved:


which prints the output as expected. However, as soon as I click on a second widget (st.select_slider) on page_2 this variable disappears and it produces the following error:

AttributeError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Why is this? I thought session_state was created to share information across pages within the app. Is the above syntax correct or do I need to save the variables differently? Am I misunderstanding something fundamental about session_states?

Thank you in advance,

It’s hard to diagnose without looking at the code.
I’m sure you have checked this, but the key for the second widget (st.select_slider) is not “name”, correct?

I managed to reproduce this issue and I found the same behaviour.
I did also find a work around. If you define a function that takes the key of your text_input and adds a new session state object (with a different key) then you can use the new key in the next page.

A working example is here

Hi Luke,

Yes the key for the second widget (st.select_slider) is not the same name!

Thank you for your advice and working example! It really helped - I was able to solve this by putting my widgets within an st.form and adding a callback function on the form_submit_button which created a new key (with a different name from the text_input key) in the st.session_state from the text_input key.

def save_example():
  if 'save_widget' not in st.session_state:
          st.session_state['save_widget'] = st.session_state.text_input_name

submitted = st.form_submit_button("Save", on_click=save_example)
1 Like