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.
were you able to insert the user input in a sheet of your own?
Yes, but I don’t see how that could be related to the issue of the form refreshing. The contents of the form don’t depend on what is in the worksheet.
Hello, I figured out the problem. It was never the form refreshing as @Goyo mentioned.
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'"
)
Thank you @Goyo for pointing me away from the wrong problem !!!
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.