Hi everyone, I’m trying to create my fist streamlit web app, a quiz of math problems (integrals). I have a form with a number_input and a submit button, and I want to store the user response (number input) in a list. When the submit button is clicked, the current response isn’t stored in the list, instead the previous answer is appended. How could I solve this? I’d really appreciate your help.
My code
if "user_ans" not in st.session_state:
st.session_state["user_ans"] = []
if 'problem' not in st.session_state:
st.session_state.problem = 1
with st.form(key=f"form {st.session_state.problem}"):
st.write("Users' responses")
st.write(st.session_state["user_ans"])
number = st.number_input('Insert a number', format="%f")
send_btn = st.form_submit_button("Send answer")
if send_btn:
st.session_state["user_ans"].append(number)
I’d recommend to try refactoring the code and instead of using the if conditional to append the widget results, you can embed that inside a callback function and use the callback function from an st.button().
Here’s more info and code snippet on implementing this in the Docs:
showing the content of the user answer state even if the button is not yet pressed.
The last statements,
if send_btn:
st.session_state["user_ans"].append(number)
could have appended the number successfully, but it could not show it because the streamlit execution stops there.
The code is begging please rerun me from the top so that I could show the current value of user answer.
With that the solution is:
if send_btn:
st.session_state["user_ans"].append(number)
st.rerun() # this one
Be careful with this rerun() as it is capable of doing an infinite loop. But in that code it is clear it is only triggered when user presses that button.
Also put your if condition outside of the form, like this.
with st.form(key=f"form {st.session_state.problem}"):
st.write("Users' responses")
st.write(st.session_state["user_ans"])
number = st.number_input('Insert a number', format="%f")
send_btn = st.form_submit_button("Send answer")
if send_btn:
st.session_state["user_ans"].append(number)
st.rerun() # this one
Another solution using placeholder technique
This one does not use rerun().
import streamlit as st
if "user_ans" not in st.session_state:
st.session_state["user_ans"] = []
if 'problem' not in st.session_state:
st.session_state.problem = 1
with st.form(key=f"form {st.session_state.problem}"):
st.write("Users' responses")
# Create a placeholder. Why? Because we are going to update
# it from somewhere after the button is pressed specifically.
user_ans_placeholder = st.empty()
user_ans_placeholder.write(st.session_state["user_ans"])
number = st.number_input('Insert a number', format="%f")
send_btn = st.form_submit_button("Send answer")
if send_btn:
st.session_state["user_ans"].append(number)
# Update the placeholder value.
user_ans_placeholder.write(st.session_state["user_ans"])
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.