streamlit: Improve "missing ReportContext" threading error
If you create a thread from your Streamlit script, and call streamlit functions from within that thread, you need to attach a ReportContext to it or else your st.foo() calls won’t do anything, and you’ll get a warning message that looks something like this:
“Thread ‘Thread-7’: missing ReportContext”
We should improve the error message to tell the user how to fix this! (We may just want an FAQ entry that we can link to from the error message.)
(The fix is to use add_report_ctx on the thread immediately after it’s created:)
from streamlit.ReportThread import add_report_ctx
thread = threading.Thread(target=...)
add_report_ctx(thread)
thread.start()
Related discussion: https://discuss.streamlit.io/t/how-to-run-a-subprocess-programs-using-thread-inside-streamlit/2440
(2022 edit: please see my comments here - this is an internal Streamlit API and has some major caveats. In particular, please do not call st.foo commands from other threads - at least in production code that needs to be stable.)
Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.
If you’d like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 44
- Comments: 34 (7 by maintainers)
For anyone stumbling over this, it has been renamed to
streamlit.report_thread.add_report_ctx(thread).Also you can do
Hi, try my solution:
Referring https://github.com/streamlit/streamlit/issues/1326#issuecomment-1220534436, I found the following.
It does work (script by @royassis ):
It does not work (added
time.sleep(1)):I wonder if it relates to:
I’m using the current latest version:
1.23.1Here’s my donation to the list of solutions:
Decorate your callback just before passing it to whatever function, to make sure that the context exists and is the right context for this session.
This function checks if the thread has no context, and if so, adds it and then removes it again after the callback, as if nothing ever happened. This is to safeguard against the hypothetical situation where the same thread is re-used in another user’s session.
Do not do anything async in your callback, though!
For version 1.12.0:
I’m getting the same error using joblib’s
Parallelanddelayed. so I have no control of the threads creation. Any Ideas to workaround this ?For version 1.10 :
from streamlit.scriptrunner.script_run_context import get_script_run_ctxAs of Streamlit version 1.4.0, this cannot be imported successfully anymore, with the error of `ModuleNotFoundError", any help?
But I don’t see this “missing ReportContext” anymore without using the
add_report_ctx()for my MQTT client connected with thepaho-mqttlibrary. So I guess this is not needed anymore?UPDATE: I actually just realized that the MQTT client did not work properly. Now I see
missing ScriptRunContextinstead ofmissing ReportContext. And I found that they have changed the module name and function name, so from now on they need to be imported as such:instead of the previous
The title of this issue may need to be changed to
missing ScriptRunContextto match recent code.IMO if a API is designed to be called from script thread, calling it from other threads should be an explicit error like “missing ScriptRunThread. Please ensure you are calling from a script thread” , rather than the current warning and ignore behavior.
@ging-dev your solution worked! I used it in StreamlitCallbackHandler_example/st_callable_util.py if anyone is trying to use LangGraph with Streamlit you can now invoke it like this or below and you won’t get
missing ReportContext threading error.awesome work!
Can you share code? I’m trying the same but running into the same error.
I made a child class from OfficialStreamlitCallbackHandler and wrapped it with the above decorator:
No luck.
I thought I’d add my own bit of sugar to the mix with an example that updates a progress bar asynchronously using a
withstatement and @valkenburg-prevue-ch’swith_streamlit_contextdecorator:Example
Demo
@valkenburg-prevue-ch Works like a charm, thanks! I’m using streamlit with Langchain and was running into this error due to Langchain spinning up various threads. Instead of having to modify the Langchain internals, I could just wrap the
on_llm_new_tokenof my callback with your decorator and everything works right away 🚀I think it would be really helpful to add the
add_script_run_ctxmethod to the__init__.pyfile instead of chasing its absoute path in each new version and causing backward incompatibilities !So everyone can just do :
from streamlit import add_script_run_ctxDoes
add_script_run_ctx(t)have to be called immediately after the thread is created?I’m working with an asynchronous library that works via callback functions. I’m hoping that these functions, when called, can update the browser window. However, the callbacks appear to be called from a “dummy” thread. I’ve tried to do the following:
Adding the call to
add_script_run_ctxremoves the errorThread 'Dummy-9': missing ScriptRunContext, but it does not update the window.I assume there is something wrong with what I’m doing here, but I think I don’t quite understand well enough how streamlit works, and why this doesn’t work. Can someone help me to understand, or provide a workaround?
For everybody comming from a search engine: In Streamlit 1.3.1 and below ist was
and now for streamlit 1.4.0 and above it has to be updated to