Toggling button state when user_input is being processed

I need a functionality:

  1. There is a button which is on by default
  2. When user input is give, the button is disabled for time untile the input is being processed.
  3. Button is enabled again after is processing is completed.
import streamlit as st
import time

def expensive_process():
    with st.spinner('Processing...'):
        time.sleep(2)

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

lock = st.session_state.run
st.button('Run', disabled=lock)

input = st.chat_input("Enter")

if input:
    st.session_state.run = True # Button is disabled as the input as to be processed
    st.session_state.result = expensive_process()
    st.session_state.run = False # Button is enabled again
        def expensive_process():
            with st.spinner('Processing...'):
                time.sleep(2)

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

        def disable(value):
            st.session_state["disabled"] = value
            
        st.button('Run', disabled=st.session_state.get('disabled', False))

        input = st.chat_input("Enter", on_submit=disable, args=(True,))

        if input:
            st.session_state.result = expensive_process()
            st.session_state["disabled"] = False
            st.rerun()# Button is enabled again

Something like this ?

2 Likes

@Faltawer Thanks a lot. This is exactly what i was looking for.

1 Like

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