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:
- 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.
- 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)