I want to create a form that allows me to create a form with N number_inputs based on a number chosen by the user and after that, get a list with the inputted values
I can create the form using
import streamlit as st
number = st.number_input('Choose number', step=1)
if st.button('create form'):
with st.form('my_form'):
for i in range(number):
st.number_input(f'input {i}')
st.form_submit_button('show_values')
But i don’t know how to get that values after submit the form.
Anyone can help me?
If you add keys to your inputs within the form, you can retrieve them from session_state upon submit.
import streamlit as st
if 'submit' not in st.session_state:
st.session_state.submit = False
def submitted():
st.session_state.submit = True
number = st.number_input('Choose number', step=1)
if st.button('create form'):
with st.form('my_form'):
for i in range(number):
st.number_input(f'input {i}', key=str(i))
st.form_submit_button('show_values', on_click = submitted)
if st.session_state.submit:
for i in range(number):
st.write(st.session_state[str(i)])
st.session_state.submit = False
Note that if you have anything else on the page, the logic around the buttons can cause problems. The form will disappear if the user clicks on something outside the form and also when they click ‘show_values’. As written, you will have only one singe page load to access those form values and then they’ll be gone. Information associated to widgets is deleted when the widgets are deleted (not rendered/not executed on a particular page load). Since buttons don’t have state anything conditioned directly on a button is going to exist for a single page load then disappear. Buttons will be true for the one page load resulting from their click and go back to false as soon as the next thing happens.
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.