How to Use Streamlit Session State

Sharing is caring!

Last Updated on July 14, 2022 by Jay

This tutorial will introduce you to the streamlit session state. Streamlit is super powerful and easy to use for creating web-based user interfaces.

Library

We can use pip to get streamlit:

pip install streamlit

Let’s create a very simple app to show how the streamlit session state works.

What’s A Session State

A Session State is the library’s unique way of storing values and sharing those values between reruns. It’s like a Python dictionary that has a key-value pair. We can access the session state by:

st.session_state

To access the individual values inside the session state, use the same syntax as accessing a dictionary:

st.session_state['key']

Sessions Don’t Store Values Forever

Note that a “session” refers to an instance of the streamlit app that we have open in the browser. As long as the same session is open, we can use the session state to store and remember values.

However, if we refresh the page, it will technically re-connect to the streamlit app thus will load a new session. And all values we stored in the previous session will disappear.

Storing Value

Our simple app will ask the user to input a name, each time a name is entered, the app will greet the new user. However, the app will remember the first name entered throughout the session.

The key takeaway from this example is that each time we enter a new name, the variable input_var updates. However, the st.session_state[‘name’] never changes.

import streamlit as st


input_var = st.text_input('enter a name')


st.write(f"Hello, {input_var}!")

if ('name' not in st.session_state) and (input_var != ''):
    st.session_state['name'] = input_var

st.write('first name you have entered:')
if 'name' in st.session_state:
    st.write(st.session_state['name'])

st.write(st.session_state)
streamlit session state storing value
streamlit session state storing value

Associate Session State With Input Widgets

We can associate streamlit input widget values with session state by using the key argument, which should be available in all kinds of input widgets.

Let’s create a dropdown menu with some food options, and set key=’food’. This basically stores the value of this dropdown into session state with a dictionary key “food”.

When we change the value in the dropdown list, the session state value “food” updates automatically. With this approach, we do not need to manually set the session_state like the previous example.

The key takeaway from this example is that each time we modify the dropdown menu, the session state updates automatically with the new value. This works similar to the variable input_select, which updates every time the dropdown list changes.

input_select = st.selectbox('select a food', 
                             options= ('sushi', 'steak','burger'),
                             key='food')

st.write(st.session_state)
streamlit session state with input widget
streamlit session state with input widget

Sharing Value Between Reruns

Session state is also useful in sharing values between reruns.

Let’s make a counter and a button. Each time we click on the button, the counter will increase by 1.

import streamlit as st

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

button = st.button('press me')

if button:
    st.session_state['count'] += 1

st.write(st.session_state)
streamlit session state share values between reruns
streamlit session state share values between reruns

Delete Values From Session State

As previously mentioned, once we refresh the page. All values in the session state will reset.

To delete individual values from the session state, simply use the del Python keyword. This will remove both the key and corresponding value from the st.session_state dictionary.

del st.session_state['name']

Additional Resources

Streamlit tutorial – making a dashboard

How to Make Streamlit Multiple Callback

Leave a Reply

Your email address will not be published. Required fields are marked *