Problem saving number_input in a list

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)

Hi @Miguel_Sc

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:

You have the line,

st.write(st.session_state["user_ans"])

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"])