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.
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.