Disable Streamlit chat_input During Prompt Processing

I’m working on a Streamlit application where I want to disable the chat_input component while processing user input. My goal is to prevent users from submitting additional prompts until the current one is fully processed.

Minimal Example:

import time
import streamlit as st

prompt = st.chat_input("your prompt here...")

if prompt:
    time.sleep(5)  # Simulate processing
    st.write(prompt)

In this example, while the application is processing the input (simulated by time.sleep), users can still enter new prompts. I want to ensure that the input field is disabled during processing.

Any suggestions on how to achieve this?

Take advantage of the chat input callback to disable it.

import time
import streamlit as st


if 'disprompt' not in st.session_state:
    st.session_state.disprompt = False
if 'msg' not in st.session_state:
    st.session_state.msg = []


def write_msg():
    if len(st.session_state.msg):
        st.write(st.session_state.msg[-1])


def prcb():
    if st.session_state.promptk:
        st.session_state.msg.append(st.session_state.promptk)
        st.session_state.disprompt = True


write_msg()

st.chat_input("your prompt here...",
              disabled=st.session_state.disprompt,
              key='promptk',
              on_submit=prcb)

if st.session_state.promptk:
    time.sleep(5)  # Simulate processing
    st.session_state.disprompt = False
    st.rerun()
2 Likes

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