Is there any working example for Session State for streamlit version >= 0.63.1

Hello,

I have tried every example I can find on the forums for preserving session state and all of them throw errors related to something the session class doesn’t contain. Is there a working example anywhere that will allow me to save session state using any method for streamlit version 0.63.1? Is the solution that this version or streamlit doesnt work yet and I need to use an older version? If so which version will allow me to save session state using any method that works?

  1. My use case is to have a checkbox in the sidebar.
  2. Check the box in the sidebar and have a new checkbox appear in the page.
  3. Click that new checkbox without have the whole page go blank or throw an error

I appreciate the help and hello everyone! Im new here and excited to see what this light weight web dev method can do!

1 Like

Hey @Codeguyross,

I’m assuming you meant button instead of checkbox, since you don’t need SessionState for checkboxes because they preserve their return values on script rerun :

import streamlit as st

if st.checkbox('Click me'):
    if st.checkbox("Click me too !"):
        st.write("Hello world")

Whereas buttons don’t, so when you click the first one it gets to True but when you click the second one, the first one goes back to None and doesn’t pass the first check. So for this case :

  1. First copy the SessionState gist in a SessionState.py file next to your app.py script.
  2. Initialize a state to False. When you click the first button pass it to True and use it to bypass the check of the first button next time.
import SessionState
import streamlit as st

session_state = SessionState.get(checkboxed=False)

if st.button('Click me') or session_state.checkboxed:
    session_state.checkboxed = True
    if st.button("Click me too !"):
        st.write("Hello world")

Here is another thread for another example.

Tested in Streamlit 0.63 and 0.64.

Hope that helps,
Fanilo

8 Likes

Thank you. You saved me a lot of time!!

2 Likes

Hey @Vignesh & @Codeguyross,

Wanted to update the thread that Session State is now natively supported in Streamlit via the 0.84.0 release! :partying_face: To use, upgrade to the latest version:

pip install --upgrade streamlit

Here are some helpful links:

Looking forward to hearing what you think :balloon:

2 Likes