I had this working on streamlit v1.13 but it appears that no longer works on streamlit 1.16
from streamlit.report_thread import get_report_ctx
ctx = get_report_ctx()
print(ctx.session_id)
With the release 1.16, is there a new approach to handling session_ids for multiple users in a streamlit cloud instance uploading files? It seems the module that it used to live (report_thread>get_report_ctx) in has been renamed or removed.
The above was modeled after the approached discussed in this closed forum thread:
Hi, you honestly shouldnāt be using this api but if you want to really keep attempting to use an internal, private api, you can check out this threadā¦ Because itās an internal api, it can change in any release and thus you run into maintainability problems.
Agreed we shouldnāt be using it but there are legitimate reasons for wanting to have an identifier that is associated with the session, for example logging user activity with a session - especially if the user can access sensitive data. Iāve been using session_id, in the various hacks, for this since a couple of months after Streamlit was first released. There have been many requests for a standard, supported, API but Iām not aware of one.
Iām currently updating the application to 1.20 and have had to set the applications session tracker to None as I really canāt be bothered to find out where the session_id has moved to in the latest refactoring.
from streamlit.runtime.scriptrunner import get_script_run_ctx
import streamlit as st
ctx = get_script_run_ctx()
session_id = ctx.session_id
You could also use one or more websocket headers as a proxy for session_id (perhaps Sec-Websocket-Key):
from streamlit.web.server.websocket_headers import _get_websocket_headers
import streamlit as st
headers = _get_websocket_headers()
session_id = headers.get("Sec-Websocket-Key")
if session_id is not None:
# ... do something with session_id
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking āAccept allā, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.