Input Table for User

Hi everyone,
So Iโ€™m trying to make an input table that my user can put values into that will automatically send them into the above table and redo the Total column calculation. I want the value they input to be inserted in the both rows under the same titled column in the score card table.
How would I do this in streamlit?

Yes, there is multiple ways you can achieve this. First option is accept that text input from the user and it automatically update it to table.
Here is the example code:

import streamlit as st
import pandas as pd


# Initial scorecard dataframe


df = pd.DataFrame({
    'Title': ['A', 'B', 'Total'],
    'Row1': [0, 0, 0],
    'Row2': [0, 0, 0]
})


# Getting user input
input_A = st.number_input('Input for A', value=0.0)
input_B = st.number_input('Input for B', value=0.0)

# Update dataframe with user input
df.at[0, 'Row1'] = input_A
df.at[1, 'Row1'] = input_B
df.at[0, 'Row2'] = input_A
df.at[1, 'Row2'] = input_B

# Recalculate the Total column
df.at[2, 'Row1'] = df['Row1'].sum()
df.at[2, 'Row2'] = df['Row2'].sum()

# Display the updated dataframe
st.table(df.set_index('Title'))

Another way is by using data editor from streamlit. You need to install streamlit nightly update package for it. Update: no need to use streamlit nightly anymore!

Here is the sample code:

import streamlit as st
import pandas as pd

# Initial scorecard dataframe
scorecard = pd.DataFrame(columns=['User', 'Pushups', 'Pullups', 'Totaled'])

# Input your data using experimental data editor
st.write("Input your data below:")
input_data = pd.DataFrame(index=[0], columns=['User', 'Pushups', 'Pullups'])
input_data = input_data.fillna(0)  # fill with zeros

edited_data = st.experimental_data_editor(input_data)

# Handle user input
if st.button('Submit'):
    edited_data['Totaled'] = edited_data['Pushups'] + edited_data['Pullups']
    scorecard = scorecard.append(edited_data, ignore_index=True)

# Display the updated scorecard
st.write("Updated Scorecard:")
st.table(scorecard)

Hope it works.

All the best

1 Like

Your sample code doesnt runโ€ฆ But thereโ€™s gotta be someway to manipulate st.number input to directly put the value into my table.

Hereโ€™s an easy way to do it โ€“ this uses a form to collect input, rather than a data editor, but you could do something similar with data editor

import streamlit as st
import pandas as pd

if "scores" not in st.session_state:
    st.session_state.scores = [
        {"name": "Josh", "Pushups": 10, "Situps": 20},
    ]


def new_scores():
    st.session_state.scores.append(
        {
            "name": st.session_state.name,
            "Pushups": st.session_state.pushups,
            "Situps": st.session_state.situps,
        }
    )


st.write("# Score table")

score_df = pd.DataFrame(st.session_state.scores)
score_df["total_points"] = score_df["Pushups"] + score_df["Situps"]

st.write(score_df)

st.write("# Add a new score")
with st.form("new_score", clear_on_submit=True):
    name = st.text_input("Name", key="name")
    pushups = st.number_input("Pushups", key="pushups", step=1, value=0, min_value=0)
    situps = st.number_input("Situps", key="situps", step=1, value=0, min_value=0)
    st.form_submit_button("Submit", on_click=new_scores)
1 Like

Hey there,

Wanted to thank @suneelbvs for submitting those suggestions in the first place. I gave a minor edit to your post which should have the code work - there were some indenting / formatting issues.

1 Like

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