Create a background scheduled task which will update the cache and auto update UI

Description

I want to create an app. When user clicks button, the app will create a background task. After a specific time interval, the task is completed. The task will change something and I want this change to auto appear in the UI. Here is a simple example code below.

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


def task():
    st.session_state.counter += 1
    # want to auto update the website UI
    st.rerun()

if __name__ == "__main__":

    st.session_state.counter = 0

    if st.button("Click me."):
        # do not block the main thread
        interval = 3
        timer = threading.Timer(3, task)
        add_script_run_ctx(timer)
        timer.daemon = True
        timer.start()

    # you can do other things here
    st.write("Counter: ", st.session_state.counter)

Question

The code st.rerun() does not work as expected. I still need to manually rerun in the website.
I want the counter to update automatically while the main thread remains unblocked.

Can anyone help me?

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