I’m encountering an issue with st.session_state.running
not reflecting correctly across my Streamlit app.
I have two main buttons, “Predict” and “Generate,” that trigger different processes. The goal is to disable all interactive elements while an action is in progress using st.session_state.running
as a flag.
- When I click “Generate,” both “Predict” and “Generate” buttons are deactivated as expected.
- However, when I click “Predict,” only the “Predict” button is deactivated, while “Generate” remains active. Additionally,
st.write(st.session_state.running)
at the end of the script showsFalse
, suggesting that the rerun only executes the code above it.
import streamlit as st
import time
if 'predict_button' in st.session_state and st.session_state.predict_button == True:
st.session_state.running = True
elif 'generate_button' in st.session_state and st.session_state.generate_button == True:
st.session_state.running = True
else:
st.session_state.running = False
st.write(st.session_state.running)
# Trigger fitting process
if st.button('Predict', disabled=st.session_state.running, key='predict_button'):
# Fit the model and make predictions
# Model fitting code here...
status = st.progress(0)
for t in range(10):
time.sleep(.2)
status.progress(10*t+10)
st.session_state.output_p = 'Your Output Here'
st.rerun()
st.write("#####")
st.write("Hello World!")
if st.button('Generate', disabled=st.session_state.running, key='generate_button'):
st.write("Hello World!")
status = st.progress(0)
for t in range(10):
time.sleep(.2)
status.progress(10*t+10)
st.session_state.output_g = 'Your Output Here'
st.rerun()
if 'output_p' in st.session_state:
st.write(st.session_state.output_p)
if 'output_g' in st.session_state:
st.write(st.session_state.output_g)
st.write(st.session_state.running)
Any advice on ensuring consistent deactivation and proper state reflection across the app would be appreciated!
Thanks!