The widget is being created anew on each page so its value is being initialized to its default value. Iâd create a separate âvalue cacheâ to store values between pages. Something like this below. (Note, when a widget has a key and you initialize its value with a session state key value other than the widgetâs own key value, Streamlit will complain. Therefore, I have removed the widget keys.)
import streamlit as st
# Instantiate the Session State Variables
if 'cache' not in st.session_state:
st.session_state.cache = {'name': '', 'age': '', 'gender': ''}
# Sidebar Widgets
sidebar_Title = st.sidebar.markdown('# Streamlit')
sidebar_pages = st.sidebar.radio('Menu', ['Page 1', 'Page 2', 'Page 3'])
# Page 1
def page1():
st.session_state.cache['name'] = st.text_input('What is your name?', value=st.session_state.cache['name'])
# Page 2
def page2():
st.session_state.cache['age'] = st.text_input('What is your age?', value=st.session_state.cache['age'])
# Page 3
def page3():
st.session_state.cache['gender'] = st.text_input('What is your gender?', value=st.session_state.cache['gender'])
# Navigate through pages
if sidebar_pages == 'Page 1':
page1()
elif sidebar_pages == 'Page 2':
page2()
else:
page3()
st.write(st.session_state.cache)
@ismet_aksoy here is your solution. You need 2 separate variables and a callback function.
import streamlit as st
# Instantiate the Session State Variables
vars = ['name', 'age', 'gender']
for _ in vars:
if _ not in st.session_state:
st.session_state[_] = ''
# Sidebar Widgets
st.sidebar.markdown('# Streamlit')
sidebar_pages = st.sidebar.radio('Menu', ['Page 1', 'Page 2', 'Page 3'])
def callback_function(state, key):
# 1. Access the widget's setting via st.session_state[key]
# 2. Set the session state you intended to set in the widget
st.session_state[state] = st.session_state[key]
# Page 1
def page1():
name = st.text_input('What is your name?', key='name_key', on_change=callback_function, args=('name','name_key'))
# Page 2
def page2():
age = st.text_input('What is your age?', key='age_key', on_change=callback_function, args=('age','age_key'))
# Page 3
def page3():
gender = st.text_input('What is your gender?', key='gender_key', on_change=callback_function, args=('gender','gender_key'))
# Navigate through pages
if sidebar_pages == 'Page 1':
page1()
elif sidebar_pages == 'Page 2':
page2()
else:
page3()
st.write(st.session_state)
The key and the session state you want to set cannot be the same. Otherwise, when the new page comes up, the widget is re-drawn, and the widgets key resets the session state.
Here, I added a callback function, which can be used generally for setting most session states from the widgets using on_change and args parameters. Note: if you are going to set a default value on certain widgets, you may need to complicate things with a third varable.
No not getting it working well, but thanks for the interest.
My code example above does remember the slider setting across pages. However, the slider
jumps in valued and if you drag it left then try to Goth other way it stops and you have to let go and grab it again. Iâm sure this is because its a sort of circular ref. - you move the slider, it updates the state and then the state sets the slider etc.
here is my quick hack in context:
import streamlit as st
# Instantiate the Session State Variables
vars = ['name', 'age', 'gender', 'level']
for _ in vars:
if _ not in st.session_state:
st.session_state[_] = ''
st.session_state.level=1
# Sidebar Widgets
st.sidebar.markdown('# Streamlit')
sidebar_pages = st.sidebar.radio('Menu', ['Page 1', 'Page 2', 'Page 3'])
def callback_function(state, key):
# 1. Access the widget's setting via st.session_state[key]
# 2. Set the session state you intended to set in the widget
st.session_state[state] = st.session_state[key]
# Page 1
def page1():
name = st.text_input('What is your name?', key='name_key', on_change=callback_function, args=('name','name_key'))
st.write(st.session_state.name)
# Page 2
def page2():
age = st.text_input('What is your age?', key='age_key', on_change=callback_function, args=('age','age_key'))
st.slider('level_1', min_value=0, max_value=10, value = st.session_state.level, key = 'level_key',on_change=callback_function, args=('level','level_key'))
st.write(" ")
st.write('level_1 = ',st.session_state.level_key)
# Page 3
def page3():
gender = st.text_input('What is your gender?', key='gender_key', on_change=callback_function, args=('gender','gender_key'))
# Navigate through pages
if sidebar_pages == 'Page 1':
page1()
elif sidebar_pages == 'Page 2':
page2()
else:
page3()
st.write(st.session_state)
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.