Created
October 8, 2019 03:38
-
-
Save tvst/fa33b9dcb58040cbcb0ea376146d4e8c to your computer and use it in GitHub Desktop.
Hack to write to Streamlit app from another thread. See: https://discuss.streamlit.io/t/live-plot-from-a-thread/247
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit.ReportThread as ReportThread | |
from streamlit.server.Server import Server | |
class ThreadSafeSt(): | |
def __init__(self): | |
"""Object that write to a Streamlit app outside the main thread. | |
This object must be created from the main thread, and then passed | |
around to other threads so they can write to the Streamlit app. | |
Example | |
------- | |
>>> # This must be called from the main thread: | |
>>> thread_safe_st = ThreadSafeSt() | |
>>> | |
>>> def function_called_from_another_thread(arg1, arg2, st): | |
... st.main.markdown('arg1 is: %s' % arg1) | |
... st.sidebar.markdown('arg2 is: %s' % arg2) | |
>>> | |
>>> # On another thread: | |
>>> function_called_from_another_thread(10, 20, thread_safe_st) | |
""" | |
ctx = ReportThread.get_report_ctx() | |
self.main = ctx.main_dg | |
self.sidebar = ctx.sidebar_dg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that they made it easier, hence this wrapper is now obsolete. Here is what seems to work for me:
Credit: @tim - https://discuss.streamlit.io/t/how-to-run-a-subprocess-programs-using-thread-inside-streamlit/2440