I want to have a button that creates an additional input box when clicked. It needs to be able to put labels on each input box, add as many boxes as desired, and keep them around for the entire time the page is loaded. It seems that the Streamlit mainloop runs all code from the beginning each time it runs, meaning values that are redefined during runtime go back to their default value after about one second. This means the user-generated input boxes disappear as soon as they're generated.
The most obvious solution to me is to create a .txt or .json file to store the data. However, I'm not sure this is the canonical way to do it as it requires accessing the file every loop and feels more like a workaround than an intended solution. Does anyone know if there is a known Streamlit solution for this?
def add_entry(holder, data_type, label=""):
"""
Creates a user-specified entry
Inputs:
-holder: dict: {label, value}
-data_type: string, whether to accept numbers, text, or true/false
-label: string, text to be displayed in entry
"""
if data_type == "Number":
val = st.number_input(label)
elif data_type == "Text":
val = st.text_input(label)
elif data_type == "True/False":
val = st.checkbox(label)
else:
val = None
holder.update({label: val})
user_added_params = {}
user_added_col_1, user_added_col_2, user_added_col_3 = st.beta_columns(3)
with user_added_col_2:
options = ["Number", "Text", "True/False"]
user_added_type = st.selectbox("Type", options=options)
with user_added_col_3:
user_added_label = st.text_input("Parameter Label")
with user_added_col_1:
if st.button("Add Parameter"):
new_entry = True
for label, value in zip(user_added_params.keys(), user_added_params.values()):
add_entry(holder=user_added_params, label=label, data_type=user_added_type)
if new_entry:
add_entry(holder=user_added_params, label=user_added_label, data_type=user_added_type)