Summary
To make code easier to read, I store st.session_state in a variable (eg. ss = st.session_state), and I do this in main.py and modules I import. Recently, I stumbled upon this thread about module state and started to wonder if using “ss = st.session_state” could create concurrency issues (ie. sharing module state between users).
For example, I tested the below in 2 separate tabs in a browser:
Steps to reproduce
Code snippet:
#Module debug2.py
import streamlit as st
ss = st.session_state
def fake():
ss.button = True
#Module debug.py
import streamlit as st
import debug2
ss = st.session_state
st.write(ss) #Should be empty on first run, then contain 'button' == True in subsequent runs
if 'button' not in ss:
ss.button = False
st.button('Push', on_click=debug2.fake)
st.write(ss) #Should contain 'button' == True
Expected behavior:
If concurrency was happening, I should see ‘button’ == True in a freshly opened tab, but it’s not the case, it always opens as empty, ie. {}. It’s good, I don’t want to share state between users, but I don’t understand what’s happening here.
I even tried the code exactly as in the referenced thread but replaced dict() by session_state, and could not see concurrency.
My questions:
- Can someone explain the difference between the above the thread I shared in the description?
- Is storing st.session_state in a variable a bad practice? Should I always use “st.session_state” instead?
Thank you (Streamlit is awesome by the way )