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.
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)
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")
...
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()
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.