I’m not sure if this is a bug or if I’m doing something wrong so I wanted to post here before raising an issue…
import streamlit as st
if 'text' not in st.session_state:
st.session_state.text = 'text 1'
st.session_state.number = 42
st.session_state.text = st.text_input('text', value=st.session_state.text)
st.session_state.number = st.number_input('number', value=st.session_state.number)
st.write(st.session_state.text)
st.write(st.session_state.number)
Steps to reproduce:
Update the text - the two st.write calls at the bottom show the updated text & the original number as expected
Update the number - the two st.write calls at the bottom show the update text & the updated number as expected
Put the text back to its original value text 1 - the two st.write calls at the bottom show the original text & the updated number as expected
Put the number back to its original value 42 - the two st.write calls at the bottom now show the updated text value (even though we just put it back to its original value in step 3)
Can anybody see if this is a bug in the session_state or something wrong in my code?
Here’s an animation if it helps show the behaviour I’m seeing…
I had a similar issue, see here Callbacks - how to get current control value? - Using Streamlit - Streamlit
Setting the value by the value argument to number_input etc via session_state does not work.Rather define a key for the widget and set the intial value with some constant. If you use the key parameter, an entry for the widget is automatically created in the session_state dict. If you need to modify the value after the user has entered something, use a callback.
I gues one problem in general is that while the streamlit code looks like normal python, the actual operation is determined by the streamlit engine and can be quite different to what the user expects.
I did try using the on_change callback but I couldn’t work out how to get the current value of the control into the callback. Thanks for the explanation.
Thanks for this explanation @ksxx, as it was driving me nuts where I had text_input values bound to session_state variables. Thats a real shame it doesnt work properly (and that you then can’t clear the session state variable to clear the value of the text_input for 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.