Hello,
I am trying to combine session states with radio button selections in the sidebar. Here is the example
import streamlit as st
if 'name' not in st.session_state:
st.session_state.name = 'Example Name'
if 'phone' not in st.session_state:
st.session_state.phone = '1234'
with st.sidebar:
radio = st.radio(
"Please select",
('Name', 'Phone'))
if radio == 'Name':
st.text_input('Enter your name', key='name')
st.file_uploader('Select a picture', key='file_pic')
if radio == 'Phone':
st.text_input('Enter phone number', key='phone')
Two problems:
the phone number dont get initialized with the key.
inserted Name, uploaded file and phone number are not saved when switching between radio button options in the sidebar.
I would like that entered values get stored when switching between the radio buttons.
using Streamlit v1.13.0
Thanks,
Robert
This seems to be a common issue amongst new Streamlit users, so I wrote a mini-tutorial app to explain how widgets are used with initialized values and how to make them stick using session state and callbacks.
There are three ways: (1) the most basic where the initial value is not given but the widget is always reset, (2) where itās initialized but there are issues getting the return value to stick, and finally (3) overcoming all issues with session state and callbacks. (I think the issue in the second case is a Streamlit bug ).
If you donāt call a widget function in a script run, we neither store the widget state nor render the widget. If you call a widget function with the same arguments later, Streamlit treats it as a new widget.
This means that even if the widget has an explicit key, that key is deleted from session_state when it is not instantiated.
Apparently this behavior can be overridden by unconditionally assigning a value to the session_state item. This is not documented as far as I know so I guess it might stop working at any moment but so far it works.
Unfortunately you cannot assign a value to a file_uploader (or its assiciated key), youāll need to keep the file in yet another key so that it is available in your code even after the widget is reset.
import streamlit as st
# unconditionally assign values to 'name' and 'phone'
st.session_state.name = st.session_state.get('name', 'Example Name')
st.session_state.phone = st.session_state.get('phone', '1234')
with st.sidebar:
radio = st.radio(
"Please select",
('Name', 'Phone'))
if radio == 'Name':
st.text_input('Enter your name', key='name')
# store the uploaded file when it is not None.
if st.file_uploader('Select a picture', key='file_pic'):
st.session_state.uploaded_file = st.session_state.file_pic
# show that the file is stil there
st.write(st.session_state.get('uploaded_file', None))
if radio == 'Phone':
st.text_input('Enter phone number', key='phone')
I tried your code and it works fine, however I still would like to solve the following problems considering the file_uploader:
After uploading the file and directly deleting it afterwards, the other variable (āst.session_state.uploaded_fileā) donāt get deleted as well.
It would be nice to have a way to delete the uploaded file after file uploader is initiated again but still a file is stored in āst.session_state.uploaded_fileā. In other words: after uploading a file and switching the radio buttons I would like to be able to delete the uploaded file.
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.