Function that runs in the background


i want a progress bar to keep runing in the background this progress bar show where the function process reached

Steps to reproduce

Code snippet:

def get_session_state():
    return st.session_state

def compute():
   session_state = get_session_state()

   if "progress" not in session_state:
        session_state.progress = {"value": 0, "status": ""}

   st.title("Streamlit Progress Bar Demo")
   progress_bar = st.progress(session_state.progress["value"])
   status_text = st.empty()
   for i in range(session_state.progress["value"], 100):
       status_text.text(f"Progress: {i+1}%")
       session_state.progress["value"] = i+1
       # adjust this to control the speed of the progress bar
   session_state.progress["status"] = "Process completed!"
   st.experimental_rerun()  # Rerun the app to update the UI with the new progress value

def main():

    if st.button("Start Computation"):
     # Start a new thread to execute the compute function
     thread = threading.Thread(target=compute)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

i used threading but it seems when i leave the page the progress bar stops , i want the progress bar keep and the function keep working even if i leave the page

1 Like