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:
st.write(st.session_state.name)
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?
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.
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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.