How to enable input widgets after disabling them?

The main issue is that after disabling certain input and buttons using session state, I’m unable to re-enable them effectively.

streamlit : 1.37

Expected Behavior:
After calling the enable() function, which sets st.session_state.disabled to False, all previously disabled inputs should become active and interactive, allowing user interaction as normal.
As long as the fitting process is running, all inputs and buttons will be disabled.

Observed Behavior:
Even after setting st.session_state.disabled back to False, the inputs remain disabled, preventing further user interaction.

import streamlit as st

if 'disabled' not in st.session_state:
    st.session_state.disabled = False

def disable():
    st.session_state.disabled = True

# User input for the ticker symbol
user_ticker_input = st.text_input(
    "Enter a ticker",
    disabled=st.session_state.disabled,
).upper()

# Trigger fitting process
if st.button("Predict", on_click=disable, disabled=st.session_state.disabled):
    
    # Fit the model and make predictions
    # Model fitting code here...

    # Re-enable interactions after prediction
    st.session_state.disabled = False

I found the solution :

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
else:
    st.session_state.running = False

st.write(st.session_state.running)

# User input for the ticker symbol
user_ticker_input = st.text_input(
    "Enter a ticker",
    disabled=st.session_state.running,
).upper()

# 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...

    # Example:
    status = st.progress(0)
    for t in range(10):
        time.sleep(.2)
        status.progress(10*t+10)
    st.session_state.output = 'Your Output Here'
    st.rerun()

if 'output' in st.session_state:
    st.write(st.session_state.output)
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.