英文:
What causes a Streamlit form to refresh after clicking the submit button and how can I fix it?
问题
我有一个使用st.input_text小部件接受用户输入的Streamlit表单。然而,每次我点击提交按钮时,表单都会刷新,并且在执行提交逻辑之前,所有输入都会丢失。
因此,从未提交任何内容到我的Google表格。
但是,当我为st.input_text小部件设置默认值时,插入操作可以正常工作。
英文:
I have a streamlit form that takes user input in the st.input_text widget. However, everytime I click submit, the form refreshes and all the input is lost before the submission logic is implemented.
As such, nothing is ever submitted to my google sheet.
with st.form(key="payments"):
st.markdown(
"**Hi foo, please choose the month and year for which you are entering data**")
month, year = st.columns(2)
with month:
selected_month = st.selectbox("Month", months)
with year:
selected_year = st.selectbox("Year", years)
status, name_column, amount = st.columns(3)
name_column.markdown("_Name_")
amount.markdown("_Amount (ugx)_")
name_input = dict()
input_amounts = dict()
for name in names:
amount_key = str(fx.create_guid())
name_input[name] = amount_key
with name_column:
st.write(name)
st.write("")
with amount:
input_amounts[amount_key] = st.text_input(
placeholder="ugx", label=" ", label_visibility="collapsed", disabled=False, key=amount_key)
submitted = st.form_submit_button("Save")
if submitted:
'Open google sheet'
for k, v in name_input.items():
amount_entered = input_amounts[v]
if amount_entered.strip() != "" and int(amount_entered) > 0:
all_values = worksheet.get_all_values()
next_row_index = len(all_values) + 1
# data to insert
data = [selected_month, k, amount_entered, selected_year]
print(data)
# insert data in the next row
worksheet.append_row(
data,
value_input_option='user_entered',
insert_data_option='insert_rows',
table_range= #table_range)
However when I put a default value for the st.input_text widgets, the insertion works.
答案1
得分: 1
我找到了问题。问题从未是表单刷新。
无需使用 input_amounts 字典存储不同输入框的值。该字典在表单加载时加载为空值,用户点击提交后不会更新。
相反,我只是利用输入框的键,并使用会话状态来检索输入的值。在以下更正后的代码中显示:
with st.form(key="payments"):
st.markdown("**Hi foo, please choose the month and year for which you are entering data**")
month, year = st.columns(2)
with month:
selected_month = st.selectbox("Month", months)
with year:
selected_year = st.selectbox("Year", years)
st.write("---")
st.markdown("**Member payments**")
name_column, amount = st.columns(2)
name_column.markdown("_Name_")
amount.markdown("_Amount (ugx)_")
name_input = dict()
counter = 1
for name in names:
amount_key = f"key{counter}"
name_input[name] = amount_key
with name_column:
st.write(name)
st.write("")
with amount:
st.text_input(
placeholder="ugx",
label=" ",
label_visibility="collapsed",
disabled=False,
key=amount_key)
counter += 1
submitted = st.form_submit_button("Save")
if submitted:
payments_for_insertion=[]
for name, amount_key in name_input.items():
amount_entered = st.session_state.get(amount_key,"")
if amount_entered.strip() != "" and int(amount_entered) > 0:
data = [selected_month, name, amount_entered, selected_year]
payments_for_insertion.append(data)
if payments_for_insertion:
'Open Worksheet'
all_values = worksheet.get_all_values()
next_row_index = len(all_values) + 1
worksheet.append_rows(
payments_for_insertion,
value_input_option='user_entered',
insert_data_option='insert_rows',
table_range='your table_range"'
)
请注意,我已经删除了 HTML 编码(")以便更清晰地阅读代码。
英文:
I figured out the problem. It was never the form refreshing.
There is no need to store the values of the different input boxes using the input_amounts dictionary. It gets loaded with blank values when the form loads which never get updated after the user clicks submit.
Instead, I just leveraged the keys of the input boxes and used session state to retrieve the values entered. This is shown in the corrected code below;
with st.form(key="payments"):
st.markdown("**Hi foo, please choose the month and year for which you are entering data**")
month, year = st.columns(2)
with month:
selected_month = st.selectbox("Month", months)
with year:
selected_year = st.selectbox("Year", years)
st.write("---")
st.markdown("**Member payments**")
name_column, amount = st.columns(2)
name_column.markdown("_Name_")
amount.markdown("_Amount (ugx)_")
name_input = dict()
counter = 1
for name in names:
amount_key = f"key{counter}"
name_input[name] = amount_key
with name_column:
st.write(name)
st.write("")
with amount:
st.text_input(
placeholder="ugx",
label=" ",
label_visibility="collapsed",
disabled=False,
key=amount_key)
counter += 1
submitted = st.form_submit_button("Save")
if submitted:
payments_for_insertion=[]
for name, amount_key in name_input.items():
amount_entered = st.session_state.get(amount_key,"")
if amount_entered.strip() != "" and int(amount_entered) > 0:
data = [selected_month, name, amount_entered, selected_year]
payments_for_insertion.append(data)
if payments_for_insertion:
'Open Worksheet'
all_values = worksheet.get_all_values()
next_row_index = len(all_values) + 1
worksheet.append_rows(
payments_for_insertion,
value_input_option='user_entered',
insert_data_option='insert_rows',
table_range='your table_range'"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论