Dynamic dataframe from user input

I am Building a streamlit code where the user takes the input of n x m variables and then takes for each variable its cost value

here is my code

import streamlit as st
import numpy as np

def main():
    st.title("Dynamic Distribution Table")

    # Get number of production items and distribution points
    num_productions = st.number_input("Enter the number of production items:", min_value=1, step=1)
    num_distributions = st.number_input("Enter the number of distribution points:", min_value=1, step=1)

    # Collect production and distribution names
    production_names = [st.text_input(f"Enter name for Production Item {i}:") for i in range(1, num_productions + 1)]
    distribution_names = [st.text_input(f"Enter name for Distribution Point {j}:") for j in range(1, num_distributions + 1)]

    # Create NumPy array to store cost data
    cost_data = np.zeros((num_productions, num_distributions))

    # Submit button for cost information
    submit_button = st.button("Submit Production, Distribution, and Cost Information")

    if submit_button:
        # Collect production and distribution costs
        collect_cost_data(production_names, distribution_names, cost_data)
        st.write("Cost Data:", cost_data)

def collect_cost_data(production_names, distribution_names, cost_data):
    for i, prod_name in enumerate(production_names):
        for j, dist_name in enumerate(distribution_names):
            key = f"{prod_name}_{dist_name}"
            cost_value = st.number_input(f"Enter cost for {prod_name} x {dist_name}:", min_value=0.0, key=key)
            cost_data[i, j] = cost_value

if __name__ == "__main__":
    main()

For some reason, while taking the cost value after putting the first value it automatically gets disbursed.

  1. Put your number_input in a form so that streamlit will not rerun the code from top to bottom as users are still modifying the numbers.
def collect_cost_data(production_names, distribution_names, cost_data):
    with st.form('form'):  # <========= this
        for i, prod_name in enumerate(production_names):
            for j, dist_name in enumerate(distribution_names):
                key = f"{prod_name}_{dist_name}"
                cost_value = st.number_input(f"Enter cost for {prod_name} x {dist_name}:", min_value=0.0, key=key)
                cost_data[i, j] = cost_value
        submit = st.form_submit_button('ok')  # Once done

    if submit:
        st.write("Cost Data:", cost_data)
  1. Declare a session variable at the top to pass through the submit_button because this button only allows once per clicked. If there is rerun (which is present) the value of it becomes false and we cannot pass through it.
import streamlit as st
import numpy as np


if 'submit' not in st.session_state:
    st.session_state.submit = False


def main():
    st.title("Dynamic Distribution Table")

...
  1. Use that variable.
    # Submit button for cost information
    submit_button = st.button("Submit Production, Distribution, and Cost Information")

    if submit_button:
        st.session_state.submit = True

    if st.session_state.submit:
        # Collect production and distribution costs
        collect_cost_data(production_names, distribution_names, cost_data)

Full code

import streamlit as st
import numpy as np


if 'submit' not in st.session_state:
    st.session_state.submit = False


def main():
    st.title("Dynamic Distribution Table")

    # Get number of production items and distribution points
    num_productions = st.number_input("Enter the number of production items:", min_value=1, step=1)
    num_distributions = st.number_input("Enter the number of distribution points:", min_value=1, step=1)

    # Collect production and distribution names
    production_names = [st.text_input(f"Enter name for Production Item {i}:") for i in range(1, num_productions + 1)]
    distribution_names = [st.text_input(f"Enter name for Distribution Point {j}:") for j in range(1, num_distributions + 1)]

    # Create NumPy array to store cost data
    cost_data = np.zeros((num_productions, num_distributions))

    # Submit button for cost information
    submit_button = st.button("Submit Production, Distribution, and Cost Information")

    if submit_button:
        st.session_state.submit = True

    if st.session_state.submit:
        # Collect production and distribution costs
        collect_cost_data(production_names, distribution_names, cost_data)

def collect_cost_data(production_names, distribution_names, cost_data):
    with st.form('form'):
        for i, prod_name in enumerate(production_names):
            for j, dist_name in enumerate(distribution_names):
                key = f"{prod_name}_{dist_name}"
                cost_value = st.number_input(f"Enter cost for {prod_name} x {dist_name}:", min_value=0.0, key=key)
                cost_data[i, j] = cost_value
        submit = st.form_submit_button('ok')

    if submit:
        st.write("Cost Data:", cost_data)


if __name__ == "__main__":
    main()

Sample Output

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