Alright I think I’ve got my case figured out at least. This is what worked for me:
from streamlit.runtime.scriptrunner import add_script_run_ctx,get_script_run_ctx
from subprocess import Popen
ctx = get_script_run_ctx()
##Some code##
process = Popen(['python','my_script.py'])
add_script_run_ctx(process,ctx)
My interpretation of the error is that streamlit does not like streamlit function calls outside the main controlling script, so if you launch your script with streamlit run script.py and try to use st.write or other functions in another script that the main script.py calls using multithreading or subprocess, then we get the error. To suppress the error, we just need to add it to the context to make streamlit happy again. The documentation only refers to multithreading, but subprocess also works for me. I’m not sure if multiprocess would have similar behavior, but in theory I think it would. I have not tested it though.
Lots of people seem to be trying (myself included) only add_script_run_ctx(process) without much success. For me, the key was I needed to also supply the ctx to which it would add the script, so that’s why we instantiate it at the top of the script. After that, you just add the script to the context manager like we’ve been trying in order to suppress the annoying error spam.
Further, the “bare mode” it refers to is used for script/streamlit testing without launching the script via the streamlit CLI method, so for most of us it seems we would have to deal with it eventually since we wouldn’t want to deploy in bare mode.
Hi…i actually have functions that i export and execute in the main script that return a plotly figure and then i just use st.plotly_chart to plot it…in some cases i export a function that calculates x and then use it in the function that returns the plotly figure and then i use that in the main script!
In my case, when my script starts other threads and runs streamlit functions within them, this warning appears.
When I try to avoid running st functions in other threads (including avoid running functions decorated with @st.cache_resource), this warning disappears.
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.