Hello.
I want to be able to control the number of text inputs by the number_inputs field. For example, if the number_inputs is equal to 3, I would like to add 3 text_inputs to the dataframe. At the current state, I am able to add the text input only when pressing the Add button. But I want to set number_inputs equal to 3, and have 3 ids to complete. At the end, I want to press the Add button and to have all ids saved successfully.
Also, is it possible to have the input fields available in a page manner, so that I can go to the next page to enter values for the id 2, id 3 and so on?
The current code only saves the last value of the id, even if there are more values given as input.
import streamlit as st
import pandas as pd
number_inputs = st.number_input('number of nr fields', step=1, min_value=1)
st.write('number of nr fields ', number_inputs)
data = pd.DataFrame(
columns=['nr'])
if 'df' not in st.session_state:
st.session_state.df = data
for i in range(number_inputs):
id=st.text_input('nr',i+1)
if st.button("Add to df",key=i+1):
# update dataframe state
st.session_state.df = st.session_state.df.append(
{'nr': id},
ignore_index=True)
st.text("Updated dataframe")
st.dataframe(st.session_state.df)
My suggestion is to store the st.text_inputs as a list and then concatenate it to the dataframe stored in the st.session_state.
import streamlit as st
import pandas as pd
number_inputs = st.number_input('number of nr fields', step=1, min_value=1)
st.write('number of nr fields ', number_inputs)
data = pd.DataFrame(columns=['nr'])
if 'df' not in st.session_state:
st.session_state.df = data
input_values = [st.text_input(f'nr {i}', i+1, key=f"text_input_{i}")
for i in range(number_inputs)]
if st.button("Add to df", key="button_update"):
# Update dataframe state
st.session_state.df = pd.concat(
[st.session_state.df, pd.DataFrame({'nr': input_values})],
ignore_index=True)
st.text("Updated dataframe")
"## Dataframe state:"
st.dataframe(st.session_state.df)
The input_values list in the snippet could be stored in another st.session_state variable to keep them available across pages.
PS:
Pandas df.append() is deprecated (see docs), pd.concat() should be used instead.
Thanks for the solution! Is there any way to edit the already added values to the df? For example, I press the Add to df button and then I want to change a “nr” entry. Is it possible to update the df with the new values?
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.