Last active
May 18, 2023 12:05
-
-
Save tvst/6ef6287b2f3363265d51531c62a84f51 to your computer and use it in GitHub Desktop.
Hack to get a session-specific ID in Streamlit. See https://discuss.streamlit.io/t/session-specific-caching/271
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit.ReportThread as ReportThread | |
from streamlit.server.Server import Server | |
def get_session_id(): | |
# Hack to get the session object from Streamlit. | |
ctx = ReportThread.get_report_ctx() | |
this_session = None | |
current_server = Server.get_current() | |
if hasattr(current_server, '_session_infos'): | |
# Streamlit < 0.56 | |
session_infos = Server.get_current()._session_infos.values() | |
else: | |
session_infos = Server.get_current()._session_info_by_id.values() | |
for session_info in session_infos: | |
s = session_info.session | |
if ( | |
# Streamlit < 0.54.0 | |
(hasattr(s, '_main_dg') and s._main_dg == ctx.main_dg) | |
or | |
# Streamlit >= 0.54.0 | |
(not hasattr(s, '_main_dg') and s.enqueue == ctx.enqueue) | |
): | |
this_session = s | |
if this_session is None: | |
raise RuntimeError( | |
"Oh noes. Couldn't get your Streamlit Session object" | |
'Are you doing something fancy with threads?') | |
return id(this_session) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have used this for version
1.22.0
@akaJasonK