Shivraj8615 commited on
Commit
3be3f9c
·
verified ·
1 Parent(s): bc42fc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -143
app.py CHANGED
@@ -1,143 +1,151 @@
1
- import streamlit as st
2
- import requests
3
- import pandas as pd
4
- import matplotlib.pyplot as plt
5
- from plot_app import Plot
6
- plotter = Plot()
7
- from recipes import (
8
- recipes_1_cotton_dark,
9
- recipes_2_cotton_medium,
10
- recipes_3_cotton_light,
11
- recipes_4_polyester_dark,
12
- recipes_5_pc_dark,
13
- )
14
- recipes_dict = {
15
- ("cotton", "dark"): recipes_1_cotton_dark,
16
- ("cotton", "medium"): recipes_2_cotton_medium,
17
- ("cotton", "light"): recipes_3_cotton_light,
18
- ("polyester", "dark"): recipes_4_polyester_dark,
19
- ("polycotton", "dark"): recipes_5_pc_dark,
20
- }
21
-
22
- def calculate_outputs(mlr, recipes, capacity):
23
- """Perform calculations for each machine."""
24
- steam_consumption = []
25
- moment_time = 0
26
- time = []
27
- initial_temp = []
28
- final_temp = []
29
- temp_grad = []
30
- for i in recipes:
31
- time.append(moment_time)
32
- delta_t = recipes[i]["Duration"]
33
- moment_time += delta_t
34
-
35
- temp_diff = recipes[i]["final_temp"] - recipes[i]["init_temp"]
36
- final_temp.append(recipes[i]["final_temp"])
37
- initial_temp.append(recipes[i]["init_temp"])
38
- temp_grad.append(recipes[i]["temp.grad"])
39
- temp_diff = abs(temp_diff)
40
- steam_use = (capacity * mlr * temp_diff) + (capacity * temp_diff)
41
- steam_consumption.append(steam_use)
42
-
43
- return {
44
- "Steam Consumption": steam_consumption,
45
- "Time": time,
46
- "init_Temp": initial_temp,
47
- "final_Temp": final_temp,
48
- "temp_grad": temp_grad
49
- }
50
-
51
- def calculate_peak_avg_load(machine_results):
52
- """Calculate peak and average steam load for all machines."""
53
- combined_steam_load = {}
54
- start_time_offset = 0
55
-
56
- for machine, results in machine_results.items():
57
- for t, load in zip(results["Time"], results["Steam Consumption"]):
58
- adjusted_time = t + start_time_offset
59
- if adjusted_time in combined_steam_load:
60
- combined_steam_load[adjusted_time] += load
61
- else:
62
- combined_steam_load[adjusted_time] = load
63
- start_time_offset += 10 # Each machine starts 10 units after the previous one
64
-
65
- peak_load = max(combined_steam_load.values())
66
- avg_load = sum(combined_steam_load.values()) / len(combined_steam_load)
67
-
68
- return peak_load, avg_load
69
-
70
- def convert_df_to_csv(df):
71
- return df.to_csv(index=False).encode('utf-8')
72
-
73
- st.title("Dyeing Machine Load & Steam Calculator")
74
-
75
- machine_type = st.selectbox("Select Type of Machine", ['SoftFlow'])
76
- num_machines = st.number_input("Number of Dyeing Machines", min_value=1, value=5)
77
- mlr = st.number_input("Enter the MLR for the process", min_value=2, value=6)
78
- machine_capacities = {}
79
-
80
- st.write("### Enter Machine Capacities")
81
- for i in range(1, num_machines + 1):
82
- capacity = st.number_input(f"Capacity of Machine {i} (kg)", min_value=1, value=100)
83
- machine_capacities[f"{machine_type} {i}"] = capacity
84
-
85
- toggle_fetch = st.checkbox("Take pre-built Recipe")
86
- if toggle_fetch:
87
- fabric = st.radio("Choose Fabric Type", ["Cotton", "Polyester", "PolyCotton"], index=0)
88
- shade = st.radio("Choose your Shade", ["Dark", "Medium", "Light", "White"])
89
- fabric_type = fabric.strip().lower()
90
- shade = shade.strip().lower()
91
- recipes = recipes_dict.get((fabric_type, shade), {})
92
- else:
93
- recipes = {}
94
-
95
- st.write("### Results")
96
- if "current_machine_index" not in st.session_state:
97
- st.session_state["current_machine_index"] = 0
98
-
99
- machine_names = list(machine_capacities.keys())
100
- machine_results = {}
101
- for machine in machine_names:
102
- machine_results[machine] = calculate_outputs(mlr, recipes, machine_capacities[machine])
103
-
104
- peak_load, avg_load = calculate_peak_avg_load(machine_results)
105
-
106
- if st.button("Calculate Summary"):
107
- st.markdown(
108
- f"""
109
- <div style='padding:10px; border-radius:10px; background-color:#05f5f5;'>
110
- <h3 style='color:black;'>Summary</h3>
111
- <p style='color:black;'><strong>Peak Steam Load:</strong> {peak_load} units</p>
112
- <p style='color:black;'><strong>Average Steam Load:</strong> {avg_load} units</p>
113
- </div>
114
- """,
115
- unsafe_allow_html=True,
116
- )
117
-
118
- col1, col2 = st.columns(2)
119
- with col1:
120
- if st.button("Previous Machine"):
121
- st.session_state["current_machine_index"] = (st.session_state["current_machine_index"] - 1) % len(machine_names)
122
- with col2:
123
- if st.button("Next Machine"):
124
- st.session_state["current_machine_index"] = (st.session_state["current_machine_index"] + 1) % len(machine_names)
125
-
126
- current_machine = machine_names[st.session_state["current_machine_index"]]
127
- st.write(f"### Calculations for {current_machine}")
128
- df_results = pd.DataFrame(machine_results[current_machine])
129
- st.table(df_results)
130
-
131
- csv = convert_df_to_csv(df_results)
132
- st.download_button("Download Table as CSV", data=csv, file_name=f"{current_machine}_results.csv", mime='text/csv')
133
-
134
- st.header("Steam Load Variation Over Time")
135
- if st.button("Plot Steam Load Variation"):
136
- fig1 = plotter.plot_steam_load(machine_results[current_machine]['Time'], machine_results[current_machine]['Steam Consumption'])
137
- st.pyplot(fig1)
138
-
139
- st.header("Temperature Variation Over Time")
140
- if st.button("Plot Temperature Variation"):
141
- 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'])
142
- st.pyplot(fig2)
143
-
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ from plot_app import Plot
5
+ plotter = Plot()
6
+ from recipes import (
7
+ recipes_1_cotton_dark,
8
+ recipes_2_cotton_medium,
9
+ recipes_3_cotton_light,
10
+ recipes_4_polyester_dark,
11
+ recipes_5_pc_dark,
12
+ cotton_cotton_lycra
13
+ )
14
+ recipes_dict = {
15
+ ("cotton", "dark"): recipes_1_cotton_dark,
16
+ ("cotton", "medium"): recipes_2_cotton_medium,
17
+ ("cotton", "light"): recipes_3_cotton_light,
18
+ ("polyester", "dark"): recipes_4_polyester_dark,
19
+ ("polycotton", "dark"): recipes_5_pc_dark,
20
+ ("lycra","dark"): cotton_cotton_lycra
21
+ }
22
+ latent_heat = 494
23
+
24
+ def calculate_outputs(mlr, recipes, capacity):
25
+ """Perform calculations for each machine."""
26
+ steam_consumption = []
27
+ moment_time = 0
28
+ time = []
29
+ initial_temp = []
30
+ final_temp = []
31
+ temp_grad = []
32
+ for i in recipes:
33
+ time.append(moment_time)
34
+ delta_t = recipes[i]["Duration"]
35
+ moment_time += delta_t
36
+
37
+ temp_diff = recipes[i]["final_temp"] - recipes[i]["init_temp"]
38
+ final_temp.append(recipes[i]["final_temp"])
39
+ initial_temp.append(recipes[i]["init_temp"])
40
+ temp_grad.append(recipes[i]["temp.grad"])
41
+ temp = recipes[i]["temp.grad"]
42
+ temp_diff = abs(temp_diff)
43
+ steam_use = (capacity * mlr * temp_diff*1) + (capacity * temp_diff*1) # cp = 1
44
+ if temp != 0 :
45
+ steam_ = steam_use*60/temp
46
+ else:
47
+ steam_ = steam_use*60
48
+ steam_kg_hr = steam_/latent_heat
49
+ steam_consumption.append(round(steam_kg_hr,2))
50
+
51
+ return {
52
+ "Steam Consumption": steam_consumption,
53
+ "Time": time,
54
+ "init_Temp": initial_temp,
55
+ "final_Temp": final_temp,
56
+ "temp_grad": temp_grad
57
+ }
58
+
59
+ def calculate_peak_avg_load(machine_results):
60
+ """Calculate peak and average steam load for all machines."""
61
+ combined_steam_load = {}
62
+ start_time_offset = 0
63
+
64
+ for machine, results in machine_results.items():
65
+ for t, load in zip(results["Time"], results["Steam Consumption"]):
66
+ adjusted_time = t + start_time_offset
67
+ if adjusted_time in combined_steam_load:
68
+ combined_steam_load[adjusted_time] += load
69
+ else:
70
+ combined_steam_load[adjusted_time] = load
71
+ start_time_offset += 10 # Each machine starts 10 units after the previous one
72
+
73
+ peak_load = max(combined_steam_load.values())
74
+ avg_load = sum(combined_steam_load.values()) / len(combined_steam_load)
75
+
76
+ return peak_load, avg_load
77
+
78
+ def convert_df_to_csv(df):
79
+ return df.to_csv(index=False).encode('utf-8')
80
+
81
+ st.title("Dyeing Machine Load & Steam Calculator")
82
+
83
+ machine_type = st.selectbox("Select Type of Machine", ['SoftFlow'])
84
+ num_machines = st.number_input("Number of Dyeing Machines", min_value=1, value=5)
85
+ mlr = st.number_input("Enter the MLR for the process", min_value=2, value=6)
86
+ machine_capacities = {}
87
+
88
+ st.write("### Enter Machine Capacities")
89
+ for i in range(1, num_machines + 1):
90
+ capacity = st.number_input(f"Capacity of Machine {i} (kg)", min_value=1, value=100)
91
+ machine_capacities[f"{machine_type} {i}"] = capacity
92
+
93
+ toggle_fetch = st.checkbox("Take pre-built Recipe")
94
+ if toggle_fetch:
95
+ fabric = st.radio("Choose Fabric Type", ["Cotton", "Polyester", "PolyCotton","Lycra"], index=0)
96
+ shade = st.radio("Choose your Shade", ["Dark", "Medium", "Light", "White"])
97
+ fabric_type = fabric.strip().lower()
98
+ shade = shade.strip().lower()
99
+ recipes = recipes_dict.get((fabric_type, shade), {})
100
+ else:
101
+ recipes = {}
102
+
103
+ st.write("### Results")
104
+ if "current_machine_index" not in st.session_state:
105
+ st.session_state["current_machine_index"] = 0
106
+
107
+ machine_names = list(machine_capacities.keys())
108
+ machine_results = {}
109
+ for machine in machine_names:
110
+ machine_results[machine] = calculate_outputs(mlr, recipes, machine_capacities[machine])
111
+
112
+ peak_load, avg_load = calculate_peak_avg_load(machine_results)
113
+
114
+ if st.button("Calculate Summary"):
115
+ st.markdown(
116
+ f"""
117
+ <div style='padding:10px; border-radius:10px; background-color:#05f5f5;'>
118
+ <h3 style='color:black;'>Summary</h3>
119
+ <p style='color:black;'><strong>Peak Steam Load:</strong> {peak_load} kg/hr</p>
120
+ <p style='color:black;'><strong>Average Steam Load:</strong> {avg_load} kg/hr</p>
121
+ </div>
122
+ """,
123
+ unsafe_allow_html=True,
124
+ )
125
+
126
+ col1, col2 = st.columns(2)
127
+ with col1:
128
+ if st.button("Previous Machine"):
129
+ st.session_state["current_machine_index"] = (st.session_state["current_machine_index"] - 1) % len(machine_names)
130
+ with col2:
131
+ if st.button("Next Machine"):
132
+ st.session_state["current_machine_index"] = (st.session_state["current_machine_index"] + 1) % len(machine_names)
133
+
134
+ current_machine = machine_names[st.session_state["current_machine_index"]]
135
+ st.write(f"### Calculations for {current_machine}")
136
+ df_results = pd.DataFrame(machine_results[current_machine])
137
+ st.table(df_results)
138
+
139
+ csv = convert_df_to_csv(df_results)
140
+ st.download_button("Download Table as CSV", data=csv, file_name=f"{current_machine}_results.csv", mime='text/csv')
141
+
142
+ st.header("Steam Load Variation Over Time")
143
+ if st.button("Plot Steam Load Variation"):
144
+ fig1 = plotter.plot_steam_load(machine_results[current_machine]['Time'], machine_results[current_machine]['Steam Consumption'])
145
+ st.pyplot(fig1)
146
+
147
+ st.header("Temperature Variation Over Time")
148
+ if st.button("Plot Temperature Variation"):
149
+ 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'])
150
+ st.pyplot(fig2)
151
+