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 ?
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.experimental_rerun()
else:
st.stop()
st.write(e, "=", st.session_state["data"][e])
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.