Shivraj8615's picture
Update app.py
3be3f9c verified
raw
history blame contribute delete
5.71 kB
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from plot_app import Plot
plotter = Plot()
from recipes import (
recipes_1_cotton_dark,
recipes_2_cotton_medium,
recipes_3_cotton_light,
recipes_4_polyester_dark,
recipes_5_pc_dark,
cotton_cotton_lycra
)
recipes_dict = {
("cotton", "dark"): recipes_1_cotton_dark,
("cotton", "medium"): recipes_2_cotton_medium,
("cotton", "light"): recipes_3_cotton_light,
("polyester", "dark"): recipes_4_polyester_dark,
("polycotton", "dark"): recipes_5_pc_dark,
("lycra","dark"): cotton_cotton_lycra
}
latent_heat = 494
def calculate_outputs(mlr, recipes, capacity):
"""Perform calculations for each machine."""
steam_consumption = []
moment_time = 0
time = []
initial_temp = []
final_temp = []
temp_grad = []
for i in recipes:
time.append(moment_time)
delta_t = recipes[i]["Duration"]
moment_time += delta_t
temp_diff = recipes[i]["final_temp"] - recipes[i]["init_temp"]
final_temp.append(recipes[i]["final_temp"])
initial_temp.append(recipes[i]["init_temp"])
temp_grad.append(recipes[i]["temp.grad"])
temp = recipes[i]["temp.grad"]
temp_diff = abs(temp_diff)
steam_use = (capacity * mlr * temp_diff*1) + (capacity * temp_diff*1) # cp = 1
if temp != 0 :
steam_ = steam_use*60/temp
else:
steam_ = steam_use*60
steam_kg_hr = steam_/latent_heat
steam_consumption.append(round(steam_kg_hr,2))
return {
"Steam Consumption": steam_consumption,
"Time": time,
"init_Temp": initial_temp,
"final_Temp": final_temp,
"temp_grad": temp_grad
}
def calculate_peak_avg_load(machine_results):
"""Calculate peak and average steam load for all machines."""
combined_steam_load = {}
start_time_offset = 0
for machine, results in machine_results.items():
for t, load in zip(results["Time"], results["Steam Consumption"]):
adjusted_time = t + start_time_offset
if adjusted_time in combined_steam_load:
combined_steam_load[adjusted_time] += load
else:
combined_steam_load[adjusted_time] = load
start_time_offset += 10 # Each machine starts 10 units after the previous one
peak_load = max(combined_steam_load.values())
avg_load = sum(combined_steam_load.values()) / len(combined_steam_load)
return peak_load, avg_load
def convert_df_to_csv(df):
return df.to_csv(index=False).encode('utf-8')
st.title("Dyeing Machine Load & Steam Calculator")
machine_type = st.selectbox("Select Type of Machine", ['SoftFlow'])
num_machines = st.number_input("Number of Dyeing Machines", min_value=1, value=5)
mlr = st.number_input("Enter the MLR for the process", min_value=2, value=6)
machine_capacities = {}
st.write("### Enter Machine Capacities")
for i in range(1, num_machines + 1):
capacity = st.number_input(f"Capacity of Machine {i} (kg)", min_value=1, value=100)
machine_capacities[f"{machine_type} {i}"] = capacity
toggle_fetch = st.checkbox("Take pre-built Recipe")
if toggle_fetch:
fabric = st.radio("Choose Fabric Type", ["Cotton", "Polyester", "PolyCotton","Lycra"], index=0)
shade = st.radio("Choose your Shade", ["Dark", "Medium", "Light", "White"])
fabric_type = fabric.strip().lower()
shade = shade.strip().lower()
recipes = recipes_dict.get((fabric_type, shade), {})
else:
recipes = {}
st.write("### Results")
if "current_machine_index" not in st.session_state:
st.session_state["current_machine_index"] = 0
machine_names = list(machine_capacities.keys())
machine_results = {}
for machine in machine_names:
machine_results[machine] = calculate_outputs(mlr, recipes, machine_capacities[machine])
peak_load, avg_load = calculate_peak_avg_load(machine_results)
if st.button("Calculate Summary"):
st.markdown(
f"""
<div style='padding:10px; border-radius:10px; background-color:#05f5f5;'>
<h3 style='color:black;'>Summary</h3>
<p style='color:black;'><strong>Peak Steam Load:</strong> {peak_load} kg/hr</p>
<p style='color:black;'><strong>Average Steam Load:</strong> {avg_load} kg/hr</p>
</div>
""",
unsafe_allow_html=True,
)
col1, col2 = st.columns(2)
with col1:
if st.button("Previous Machine"):
st.session_state["current_machine_index"] = (st.session_state["current_machine_index"] - 1) % len(machine_names)
with col2:
if st.button("Next Machine"):
st.session_state["current_machine_index"] = (st.session_state["current_machine_index"] + 1) % len(machine_names)
current_machine = machine_names[st.session_state["current_machine_index"]]
st.write(f"### Calculations for {current_machine}")
df_results = pd.DataFrame(machine_results[current_machine])
st.table(df_results)
csv = convert_df_to_csv(df_results)
st.download_button("Download Table as CSV", data=csv, file_name=f"{current_machine}_results.csv", mime='text/csv')
st.header("Steam Load Variation Over Time")
if st.button("Plot Steam Load Variation"):
fig1 = plotter.plot_steam_load(machine_results[current_machine]['Time'], machine_results[current_machine]['Steam Consumption'])
st.pyplot(fig1)
st.header("Temperature Variation Over Time")
if st.button("Plot Temperature Variation"):
fig2 = plotter.plot_temperature_curve(machine_results[current_machine]['Time'], machine_results[current_machine]['init_Temp'], machine_results[current_machine]['final_Temp'], machine_results[current_machine]['temp_grad'])
st.pyplot(fig2)