Issue with Handling Background Tasks in Streamlit

Summary

I’m building a Streamlit app that involves running a time-consuming task when a “push 1” is clicked.

I tried different keys. it seems not work without “while True” it work, but I need loop for taking data in real time from device

what can be reason for this? and how fix it?

Steps to reproduce

if I use “push 1” than “push 2”, “push 2” give result like “push 1”+“push 2” if I use “push 2” than result correct.

Code snippet:

import streamlit as st
import time


if st.button('📐 Push 1 ', key=1):
    with st.spinner("wait Push 1"):
        time.sleep(1)
        st.success('Push 1')

if st.button('🎬 Push 2', key=2):
    st.success('Push 2')

while True:
    pass

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

Expected behavior:

expected - button not influence each other

Actual behavior:

if I use “push 1” than “push 2”, “push 2” give result like “push 1”+“push 2”

Debug info

  • Streamlit version: 1.25
  • Python version: 3.8
  • Using pyCharm
  • OS version: Win10 64x
  • Browser version: Chrome Version 115.0.5790.171 (64-Bit)
import streamlit as st
# Initialize session state
if 'push_1_clicked' not in st.session_state:
    st.session_state.push_1_clicked = False
if st.button('📐 Push 1', key=1):
    st.session_state.push_1_clicked = True
    with st.spinner("wait Push 1"):
        st.success('Push 1')
if st.button('🎬 Push 2', key=2):
    push_1_clicked = st.session_state.push_1_clicked
    if push_1_clicked:
        st.success('Push 1 + Push 2')
    else:
        st.success('Push 2')

Am using session state and making it influence each other
hope it will work for you

I need to have

while True:
    pass

with it your code same as my.

image

import streamlit as st
import time

# Initialize session state
if 'push_1_clicked' not in st.session_state:
    st.session_state.push_1_clicked = False

if st.button('📐 Push 1', key=1):
    st.session_state.push_1_clicked = True
    with st.spinner("wait Push 1"):
        time.sleep(1)
        st.success('Push 1')

if st.button('🎬 Push 2', key=2):
    push_1_clicked = st.session_state.push_1_clicked
    if push_1_clicked:
        st.success('Push 1 + Push 2')
    else:
        st.success('Push 2')

while True:
    pass

Sorry?
Can you repeat again?
I dont get it

I maded video.
0:11 - I use “push 2”. but “push 1” work as well
I need independent buttons. now they somehow connected

In the code i sent before
if you click push 2 it will only give push2
but if u click after push 1 and then push 2
then it will give push1+push2

u asked for it only right?

This is description of actual behaviour or problem

I asked about this:

Opps sorry for that

This code might not work for you but i tried to introduce multi threading as you said independent

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

def print_even_numbers():
    with st.spinner("Even Numbers"):
        for num in range(2, 11, 2):
            st.text(num)

def print_odd_numbers():
    with st.spinner("Odd Numbers"):
        for num in range(1, 10, 2):
            st.text(num)

# Define threads
thread_even = Thread(target=print_even_numbers)
add_script_run_ctx(thread_even)

thread_odd = Thread(target=print_odd_numbers)
add_script_run_ctx(thread_odd)

# Create two columns
col1, col2 = st.columns(2)

with col1:
    button1 = st.button("Start Even Numbers")
    if button1:
        thread_even.start()

with col2:
    button2 = st.button("Start Odd Numbers")
    if button2:
        thread_odd.start()

Hope someone will answer this and clear your doubt