Summary
I am trying to create a streamlit app which has multiple buttons and each button runs some slow process. Once a button is clicked, I want to disable all buttons (to avoid double clicks and interfering processes), and run the process. Once the process is finished, I want to enable all the buttons and display a status message.
Code
This is the closest I can get. Clicking the button 3 displays the exact behavior I want, but if you click button 1 or 2, you’ll notice that they don’t disable the buttons below them as they should. Is there any way to do this?
import streamlit as st
import time
if 'running' not in st.session_state:
st.session_state.running = False
def callback():
st.session_state.running = True
if st.button('Button 1', disabled=st.session_state.running, on_click=callback):
time.sleep(5) #simulated slow process
st.session_state.success_message = True
st.session_state.running = False
st.rerun()
if st.button('Button 2', disabled=st.session_state.running, on_click=callback):
time.sleep(5) #simulated slow process
st.session_state.success_message = True
st.session_state.running = False
st.rerun()
if st.button('Button 3', disabled=st.session_state.running, on_click=callback):
time.sleep(5) #simulated slow process
st.session_state.success_message = True
st.session_state.running = False
st.rerun()
if 'success_message' in st.session_state and st.session_state.success_message:
st.success("Success")
The code you provided still has the issues I’m looking to avoid. If the first button is clicked, the second and third buttons are not disabled properly. And since you’ve removed st.rerun(), they don’t update back to enabled once the process is finished. Does your code work for you? I’m wanting a way to disable all 3 buttons until the process is finished running at which point all 3 are re-enabled.
For context, I’m using Python 3.12.4 and Streamlit 1.36.0
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.