Streamlit.script_run_context() gone with 1.8?

Hi,

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

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?

thx

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

Change the import from

#v1.7.0
from streamlit.script_run_context import get_script_run_ctx

to

#v1.8.0
from streamlit.scriptrunner.script_run_context import get_script_run_ctx
2 Likes

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:

#v1.11
from streamlit.scriptrunner.script_run_context import get_script_run_ctx

to

#v1.12
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx

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

Server.get_current()

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

Any ideas? =)

2 Likes

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
        break

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
st.write(session_headers)

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