How to add more fields based on user input?

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)

Thanks.

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.

manyTextInputs

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.

1 Like

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?

Is there any way to extend this solution to objects inside a st.form?

A tried this code for basically the same problem, with the difference that number_inputs and input_values are within a form.

The behavior I got: it was only updating the input_values after pressing the submit button.

Would appreciate any help!

That is the whole point of Streamlit forms. If you don’t want this then you don’t want a form.

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