Wait input before continuing execution


I try to add a feature to an Streamlit app which allows to add a record to a database.

Simplified code snippet:

for e in some_list:
   cursor.execute("SOME SQL")
   if cursor.rowcount == 0:
      new_value = st.number_input(f"Missing value : {e}", key="value2DB")
      tmp_button = st.button("Add value to database")
      if tmp_button:
         cursor.execute("MORE SQL")

Actually I can’t make it wait click on tmp_button before continue loop.
I tried to implement several workaround like time.sleep(), while not tmp_button, st.form, event.wait(), …, without success. May you help ?

Thanks for your attention.

Here is one way to accomplish this, using st.stop and st.experimental_rerun. I’ve used st.session_state rather than a database to simplify the example, but something similar should work with a database.

import streamlit as st

some_list = [1, 2, 3, 4, 5]

if "data" not in st.session_state:
    st.session_state["data"] = {
        1: 123,
        2: 456,

def get_data(key: int):
    return st.session_state["data"].get(key, None)

def add_data(key: int, value: int):
    st.session_state["data"][key] = value

for e in some_list:
    result = get_data(e)
    if not result:
        new_value = st.number_input(f"Missing value for {e}", value=5)
        tmp_button = st.button("Add value to database", key=f"missing_{e}")
        if tmp_button:
            add_data(e, new_value)

    st.write(e, "=", st.session_state["data"][e])

Works perfectly. Thank you!