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

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