Hi,
Let’s say I have a radio button to choose a color:
[‘Red’, ‘Green’, Blue], and my default is element 0 (‘Red’)
if the user chose ‘Green’, then it stays like that over reruns (which is good), but if there is an error,
the app refreshes itself and selects 0 again (‘Red’), but I want to preserve the user selection.
How can I do it with the session state?
Could you maybe share a code snippet of what you mean/working on exactly?
I’m unsure what kind of error you’re expecting the user to run into, or what kind of error you’re running into currently which makes your application forget the selected color.
Do you already save the color choice in a session state variable?
Cause putting it in session_state is as simple as something like:
import streamlit as st
if "color" not in st.session_state:
st.session_state.color = "Red"
color = st.radio("Select a color", ["Red", "Green", "Blue"])
st.session_state.color = color
st.write("You chose: ", st.session_state.color)
For now, if you refresh a Streamlit page it will always reset everything because you’re basically making a new connection to the application. There’s no browser/cookie based storage yet.
If you would like to persist things for a user it would be best to save the color to a database and retrieve it for the user on login.
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.