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 4 years ago
  • Reactions: 25
  • Comments: 25 (4 by maintainers)

Most upvoted comments

For anyone stumbling over this, it has been renamed to streamlit.report_thread.add_report_ctx(thread).

For version 1.12.0:

streamlit.runtime.scriptrunner import get_script_run_ctx

Also you can do

import streamlit as st
from threading import Thread
from streamlit.runtime.scriptrunner import add_script_run_ctx

def target():
    st.text("thread")

t = Thread(target=target)
add_script_run_ctx(t)

For version 1.12.0:

streamlit.runtime.scriptrunner import get_script_run_ctx

I’m getting the same error using joblib’s Parallel and delayed. 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_ctx