Show all list from form Streamlit

Hi There

I want to make form and then when i press the button, i hope the result can show in st.write like this.
tes

My problem the result only show the latest result. the previous result will be deleted.

How can i get all the result, not only the latest result

Could you post a sample code?

Also have a look on posting guide.

This is my sample code

import streamlit as st

with st.form("template_form"):
    name=st.text_input("Name")
    age = st.number_input("Age", 1, 100)
    ageyears=(f"{age}"" years old")
    submit = st.form_submit_button("Add Data")

data_list = []
if submit:
    data_item=[name,ageyears]
    data_list.append(data_item)

st.write(data_list)

so the result like this

i want to get result like this
tes
so if i click button “add data”
the previous result did not delete, but can show again with the latest result

the result must be show in list like this image, don’t using pandas or similar to show the result

You need to use a session variable.

Also read the streamlit main concept. This is very important especially the Data Flow. The data_list = [] cannot store previous data because streamlit reruns the code from top to bottom after pressing the button. Hence it will initialize the data_list again.

import streamlit as st

# Create a session variable. When streamlit reruns
# the code from top to bottom, session variable 
# value will not be reseted, unless the page
# is refreshed.
if 'data' not in st.session_state:
    st.session_state['data'] = []  # init only

with st.form("template_form"):
    name=st.text_input("Name")
    age = st.number_input("Age", 1, 100)
    ageyears=(f"{age}"" years old")
    submit = st.form_submit_button("Add Data")

# data_list = []
if submit:
    data_item=[name,ageyears]
    st.session_state['data'].append(data_item)

st.write(st.session_state['data'])

That’s amazing, i say thank you for your help.
But i get error “TypeError: ‘list’ object is not callable” when i run your code.

Looks fine on my test.

Try to refresh it and enter some data.

Thank you very much, that code work in my computer, after i close my visual studio code and google chrome. and the i open it again. i can get the result.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.