Skip to content

Instantly share code, notes, and snippets.



Last active Aug 27, 2020
What would you like to do?
Hack to get a session-specific ID in Streamlit. See
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()
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)
# 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)

This comment has been minimized.

Copy link

@walterwang walterwang commented Jun 8, 2020

thanks this is awesome since it allows people to see headers as well from the session object!


This comment has been minimized.

Copy link

@pommedeterresautee pommedeterresautee commented Aug 27, 2020

Now it is simpler:

from streamlit.report_thread import get_report_ctx
ctx = get_report_ctx()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.