Streamlit Re-Runs on User Interaction and Fails to Wait for Selectbox Selections

I’m working on a Streamlit application where I need to collect multiple inputs from the user (e.g., text inputs, date pickers, and selectboxes) before processing the data. However, I’m facing two major issues:

  1. Page Reload on Interaction: Every time I interact with the input fields, especially when filling out a selectbox or text input, Streamlit re-runs the entire script. This creates a loop where the page constantly reloads, preventing the code from executing completely.
  2. Premature Code Execution: The code proceeds to execute even if I haven’t finished interacting with the selectboxes or other fields. Instead of waiting for my input, it runs immediately, which leads to incomplete data processing and unintended results.

I’ve tried using a combination of checkboxes, st.button, and st.session_state to control the flow, but none have effectively solved both issues. I’m aiming to implement a way for the app to wait until all fields are filled and only proceed when I confirm it’s ready.

This function is inside a class that I imported in another script:

def agente_anotador_de_incidencias(self, query):
# Extracción de entidades clave
entities = self.extract_entities(query)

    # Inicialización del estado
    if "task" not in st.session_state:
        st.session_state.task = entities.get("tarea")
    if "equipment" not in st.session_state:
        st.session_state.equipment = entities.get("equipo")
    if "user" not in st.session_state:
        st.session_state.user = entities.get("usuario")
    if "start_date" not in st.session_state:
        st.session_state.start_date = entities.get("fecha_inicio", "today")
    if "end_date" not in st.session_state:
        st.session_state.end_date = entities.get("fecha_fin", "today")
    if "time" not in st.session_state:
        st.session_state.time = entities.get("horas")

    # Crear un formulario para agrupar las entradas
    with st.form(key='incident_form'):
        st.text_input("Introduce la tarea:", key="task")
        st.text_input("Introduce el equipo:", key="equipment")
        if entities.get("realizado"):
            st.text_input("Introduce el usuario:", key="user")
            st.date_input("Fecha de inicio:", key="start_date")
            st.date_input("Fecha de fin:", key="end_date")
            st.number_input("Horas de duración de la tarea:", min_value=0, value=st.session_state.time or 0, key="time")

        # Botón para confirmar datos dentro del formulario
        submit_button = st.form_submit_button(label='Confirmar datos')

    if submit_button:
        # Procesa solo al confirmar
        coincidencias = self.buscar_coincidencias("equipo", st.session_state.equipment)
        if coincidencias:
            st.session_state.equipment = self.elegir_coincidencia(coincidencias, st.session_state.equipment)

        if not entities.get("realizado"):
            new_row = self.create_incidence(st.session_state.task, st.session_state.user, entities.get("fecha_solicitud"), st.session_state.equipment)
        else:
            new_row = self.update_incidence(st.session_state.task, st.session_state.user, st.session_state.start_date, st.session_state.end_date, st.session_state.equipment, st.session_state.time)

        # Muestra solo la fila modificada o insertada
        st.write("Resultado:", new_row)

That is how streamlit applications are supposed to work, according to the documentation. There are exceptions, like widgets in a form or a fragment.

I don’t see how this would prevent any code from executing. As you said, streamlit re-runs the entire script.

I ran your code, after writing my own implementations of the missing parts, and it seems to work exactly like that. It only proceeds when the user clicks the submit button. By “proceed” I mean everything after the comment “# Procesa solo al confirmar”. If you mean something else, please clarify.

I fixed it. The issue was that this piece of code was inside a function that was being called in another script. In that script, the initial query on which these functions are applied was not defined using st.session_state. This caused Streamlit to get stuck on the initial query’s text box when something was selected because the query was not stored in the session state, and the app would reset on reload.

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