Skip to content

Instantly share code, notes, and snippets.

@FranzDiebold
Forked from tvst/SessionState.py
Last active August 25, 2023 14:38
Show Gist options
  • Save FranzDiebold/898396a6be785d9b5ca6f3706ef9b0bc to your computer and use it in GitHub Desktop.
Save FranzDiebold/898396a6be785d9b5ca6f3706ef9b0bc to your computer and use it in GitHub Desktop.
A possible design for doing per-session persistent state in Streamlit
# https://gist.github.com/FranzDiebold/898396a6be785d9b5ca6f3706ef9b0bc
"""Hack to add per-session state to Streamlit.
Works for Streamlit >= v0.65
Usage
-----
>>> import SessionState
>>>
>>> session_state = SessionState.get(user_name='', favorite_color='black')
>>> session_state.user_name
''
>>> session_state.user_name = 'Mary'
>>> session_state.favorite_color
'black'
Since you set user_name above, next time your script runs this will be the
result:
>>> session_state = get(user_name='', favorite_color='black')
>>> session_state.user_name
'Mary'
"""
import streamlit.report_thread as ReportThread
from streamlit.server.server import Server
class SessionState():
"""SessionState: Add per-session state to Streamlit."""
def __init__(self, **kwargs):
"""A new SessionState object.
Parameters
----------
**kwargs : any
Default values for the session state.
Example
-------
>>> session_state = SessionState(user_name='', favorite_color='black')
>>> session_state.user_name = 'Mary'
''
>>> session_state.favorite_color
'black'
"""
for key, val in kwargs.items():
setattr(self, key, val)
def get(**kwargs):
"""Gets a SessionState object for the current session.
Creates a new object if necessary.
Parameters
----------
**kwargs : any
Default values you want to add to the session state, if we're creating a
new one.
Example
-------
>>> session_state = get(user_name='', favorite_color='black')
>>> session_state.user_name
''
>>> session_state.user_name = 'Mary'
>>> session_state.favorite_color
'black'
Since you set user_name above, next time your script runs this will be the
result:
>>> session_state = get(user_name='', favorite_color='black')
>>> session_state.user_name
'Mary'
"""
# Hack to get the session object from Streamlit.
session_id = ReportThread.get_report_ctx().session_id
session_info = Server.get_current()._get_session_info(session_id)
if session_info is None:
raise RuntimeError('Could not get Streamlit session object.')
this_session = session_info.session
# Got the session object! Now let's attach some state into it.
if not hasattr(this_session, '_custom_session_state'):
this_session._custom_session_state = SessionState(**kwargs)
return this_session._custom_session_state
@hhackbarth
Copy link

Thanks. Works as expected.

@Uranium2
Copy link

Uranium2 commented Jan 24, 2022

New version of streamlit changed the internal usage of ReportThread

I was working until i've updated to streamlit==1.4.0

Works with 1.3.0

streamlit/streamlit#4099 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment