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’
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! does anyone know?
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.
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.