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
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
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
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.