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)
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'])
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.