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")
Hello,
The disabled is “disabled” when True not False. You just need to switch it !
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 = True
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 = True
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 = True
if 'success_message' in st.session_state and st.session_state.success_message:
st.success("Success")
I remove st.rerun() because it’s working like this, but maybe you want an other behaviour
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
The first part, all the button are disabled correctly.
To reset after you can add
if st.button('reset'): #Or the condition you want like waiting 5 seconds/ user input
st.session_state.running= False
st.rerun()