Streamlit.script_run_context() gone with 1.8?


wenn using threading I understood to run a thread inside a streamlit context I need to
add the Thread to a context.
t = Thread(target=prepare_pd_threaded, args=(x, pd_dict))

Recently the function changed from add_report_ctx() to add_script_run_ctx().
With latest version 1.8 it changed again:

ModuleNotFoundError: No module named ‘streamlit.script_run_context’

Any hints?


I have the same problem, it breaks this code here:

Change the import from

from streamlit.script_run_context import get_script_run_ctx


from streamlit.scriptrunner.script_run_context import get_script_run_ctx

Thanks a lot - that works :+1:

It also worked in versions 1.9, 1.10, and 1.11.

But version 1.12 changed the API:

from streamlit.scriptrunner.script_run_context import get_script_run_ctx


from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx

Also, and more importantly, the Server class lost the method:


So I’m looking forward to find another way to get the server instance…

Any ideas? =)


In case there is no other way (if there is, please tell me), the server instance can be found in the object list of the garbage collector:

import gc
for obj in gc.get_objects():
    if type(obj) is Server:
        server = obj

with these versions installed:

streamlit                   1.12.0
streamlit-server-state      0.14.0

I can use these imports:

from streamlit_server_state import server
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx

then this code retrieves the current server and session_info, from which I am able to extract the http headers (which I need for authentication)

# The container can host multiple sessions, so we must make sure to select the correct one!
session_id = get_script_run_ctx().session_id
session_info = server.get_current_server()._get_session_info(session_id)
session_headers = session_info.client.request.headers._dict

I have to say this looks worse than it did in the previous streamlit version, and it looked bad enough before. We really need a non-hacky way of getting to this information, it feels like it will break with every upgrade. Maybe there is a way! :smiley: does anyone know?

1 Like