Writing to Streamlit ui from a thread

I have a dashboard with live data, so I am inclined to have a while loop with time.sleeps in it. However, while the app is sleeping, input from the app is unresponsive (e.g. if the user wants to add something to the dashboard from a sidebar form). This is the underlying problem I’m trying to solve.

To do that, I tried updating the dashboard from a different thread, but it seems like it only kind of works or doesn’t work. Example:

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


def do_stuff():
    st.write("Start")
    count = 0
    while True:
        st.write(count)
        time.sleep(5)
        print(count)
        count += 1


t = Thread(target=do_stuff, daemon=True)
add_script_run_ctx(t)
t.start()

Terminal prints out:
image

But the website is blank. If I reload the website, it shows this:

Further reloads don’t do anything.

Streamlit version 1.13.0

Something else I’ve tried is to not sleep, but instead to do:

while True:
    if time.time() - last_time > 30:
        self.update()
        last_time = time.time()

I thought this would mean that the UI would check for updates more often, but it seems like that isn’t the case and the sidebar still responds slowly.

Hi @kevinlinxc,

Were you able to find a solution to this problem?

Hi @kevinlinxc,

Did you find a solution? I have the same problem.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.