Session State Variables are re-initialized to initial state going through pages

hi all,

I am having issues with my session state variables with the following code.

  1. I instantiate the session state variables

  2. In the sidebar, I can select the pages to which I want to navigate to

  3. In each page, there is a widget that takes a value and saves it in the session state.

Problem: when going through different pages, the values that were given are being removed from the session state.

Here is my code:

# Instantiate the Session State Variables

if 'name' not in st.session_state:

    st.session_state.name = ''

if 'age' not in st.session_state:

    st.session_state.age = ''

if 'gender' not in st.session_state:

    st.session_state.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():

    name = st.text_input('What is your name?', key = 'name' )

# Page 2

def page2():

    age = st.text_input('What is your age?', key = 'age')

# Page 3

def page3():

    gender = st.text_input('What is your gender?', key = 'gender')

# Navigate through pages

if sidebar_pages == 'Page 1':

    page1()

elif sidebar_pages == 'Page 2':

    page2()

else:

    page3()

st.write(st.session_state)
1 Like

hello,
I have the same issue, and I don’t understand why.

The doc:
https://docs.streamlit.io/library/advanced-features/widget-semantics
seems to say that it should be the way to save widgets states through session.

An (ugly?) workaround found is to put the following lines somewhere in the page

for k in st.session_state.keys():
    st.session_state[k] = st.session_state[k]

but I am not clear if we should really do that
 ?

3 Likes

Hi @ismet_aksoy

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)

Thank you asehmi, I will give it a try

See my solution here: Widget use of `index` and `key` together... `index` overrides `key` · Issue #3963 · streamlit/streamlit · GitHub.

I had some difficulty understanding the mechanisms for resetting widgets and session states, and this setup seems to handle most use cases

@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.

1 Like

@bertrandL see my response to another user here: Session State Variables are re-initialized to initial state going through pages - #6 by ryanlampkin

can this be used with sliders? I can’t get it to work

st.slider('level_1', min_value=0, max_value=10, key = 'level_key',on_change=callback_function, args=('level_1','level_key'))
	st.write(" ")
 
	st.write('level_1 = ',st.session_state.level_key)

TIA

Hi @steeley - Not exactly sure what you’re trying to do, but


import streamlit as st

if 'level_1' not in st.session_state:
    st.session_state['level_1'] = 0

def callback_function(mod, key):
    st.session_state[mod] = st.session_state[key]

st.slider('Slider title', min_value=0, max_value=10, key='level_key', on_change=callback_function, args=('level_1','level_key'))
st.write(" ")
st.write(st.session_state)
st.write('level_1 = ', st.session_state.level_key)

This seems to work as expected for me. Perhaps you had the variables in the callback_function reversed?

yep, got some vars messed up
!
Thanks - looks like its doing what I need now.

Ahhh , but of course this only works for a single page. Gets reset every time you go away/come back to the page.

However this seems to work, but not really that good as its sort of a circular ref


slider_val =  st.session_state.volume
st.slider('volume', min_value=0, max_value=10, value =slider_val, key = 'volume_key',on_change=cb_update, args=('volume','volume_key'))

@steeley did you find a solution. if you can reproduce the code with error, I may be able to help.

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)

Thank you for your input. this has solved my problem.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.