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

Hi!I am facing the same issue. How do I pass the context info to my thread in question? My code is something like this:

from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx
kws = KiteTicker(info['api_key'], info['access_token'])
    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    
    kws.connect(threaded=True)   #This statement will run as a separate thread and needs context info
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx

kws = KiteTicker(info['api_key'], info['access_token'])

# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close

# Get the script run context
script_run_ctx = get_script_run_ctx()

# Pass the script run context to the connect method
kws.connect(threaded=True, script_run_ctx=script_run_ctx)

It is important to note that this is just a general example and you will have to adjust it to your specific use case and connect to the kite connect API and make changes to the code as per your specific needs. It is also important to note that the above code is purely fictional, as the package and methods provided in the code are not valid and may not work. You may have to use different package or methods to connect to the kite connect API and implement your callback function accordingly. Also, it’s important to thoroughly test your automated trading strategy on historical data and have a solid understanding of the risks involved before implementing it in a live trading environment.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.