Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,7 @@ st.markdown(
|
|
21 |
st.sidebar.header("Step 1: Upload Dataset")
|
22 |
uploaded_file = st.sidebar.file_uploader("Upload your Excel file (.xlsx)", type=["xlsx"])
|
23 |
|
24 |
-
# Initialize
|
25 |
fiber_impact_data = None
|
26 |
transport_impact_data = None
|
27 |
washing_impact_data = None
|
@@ -49,46 +49,6 @@ def process_excel(file):
|
|
49 |
if uploaded_file:
|
50 |
fiber_impact_data, transport_impact_data, washing_impact_data = process_excel(uploaded_file)
|
51 |
|
52 |
-
# Sidebar for product and fiber composition inputs
|
53 |
-
st.sidebar.header("Step 2: Input Product Details")
|
54 |
-
product_weight = st.sidebar.number_input("Product Weight (kg)", min_value=0.01, step=0.01, value=0.5)
|
55 |
-
|
56 |
-
st.sidebar.header("Material Composition (%)")
|
57 |
-
cotton = st.sidebar.number_input("Conventional Cotton (%)", min_value=0, max_value=100, value=50, step=1, key="cotton")
|
58 |
-
polyester = st.sidebar.number_input("Polyester (%)", min_value=0, max_value=100, value=30, step=1, key="polyester")
|
59 |
-
nylon = st.sidebar.number_input("Nylon 6 (%)", min_value=0, max_value=100, value=10, step=1, key="nylon")
|
60 |
-
acrylic = st.sidebar.number_input("Acrylic (%)", min_value=0, max_value=100, value=5, step=1, key="acrylic")
|
61 |
-
viscose = st.sidebar.number_input("Viscose (%)", min_value=0, max_value=100, value=5, step=1, key="viscose")
|
62 |
-
|
63 |
-
total_percentage = cotton + polyester + nylon + acrylic + viscose
|
64 |
-
if total_percentage != 100:
|
65 |
-
st.sidebar.error("The total of all fiber percentages must equal 100%!")
|
66 |
-
|
67 |
-
composition = {
|
68 |
-
"Conventional Cotton": cotton,
|
69 |
-
"Polyester": polyester,
|
70 |
-
"Nylon 6": nylon,
|
71 |
-
"Acrylic": acrylic,
|
72 |
-
"Viscose": viscose,
|
73 |
-
}
|
74 |
-
|
75 |
-
# Sidebar for lifecycle inputs
|
76 |
-
st.sidebar.header("Step 3: Input Lifecycle Details")
|
77 |
-
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
|
78 |
-
washing_cycles = st.sidebar.number_input("Number of Washing Cycles", min_value=0, step=1, value=30)
|
79 |
-
washing_temperature = None
|
80 |
-
use_dryer = False
|
81 |
-
transport_mode = None
|
82 |
-
transport_distance = 0
|
83 |
-
|
84 |
-
if washing_impact_data:
|
85 |
-
washing_temperature = st.sidebar.selectbox("Washing Temperature", list(washing_impact_data.keys()))
|
86 |
-
use_dryer = st.sidebar.checkbox("Use Tumble Dryer?")
|
87 |
-
|
88 |
-
if transport_impact_data:
|
89 |
-
transport_mode = st.sidebar.selectbox("Transport Mode", list(transport_impact_data.keys()))
|
90 |
-
transport_distance = st.sidebar.number_input("Transport Distance (km)", min_value=0, step=10, value=100)
|
91 |
-
|
92 |
# Function to calculate footprints
|
93 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
94 |
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
@@ -115,45 +75,102 @@ def calculate_footprints(weight, composition, lifecycle_inputs):
|
|
115 |
energy_fp += washing_energy
|
116 |
carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
lifecycle_inputs = {
|
125 |
-
"transport_mode": transport_mode,
|
126 |
-
"transport_distance": transport_distance,
|
127 |
"washing_temperature": washing_temperature,
|
128 |
"washing_cycles": washing_cycles,
|
129 |
"use_dryer": use_dryer,
|
|
|
|
|
130 |
}
|
|
|
|
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
st.
|
135 |
-
|
136 |
-
st.markdown(f"- **Energy Footprint**: {energy_fp:.2f} MJ")
|
137 |
-
st.markdown(f"- **Carbon Footprint**: {carbon_fp:.2f} kg CO2e")
|
138 |
-
|
139 |
-
# Visualization
|
140 |
if comparison_mode:
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
else:
|
143 |
-
#
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
"Value": [water_fp, energy_fp, carbon_fp],
|
147 |
})
|
148 |
-
|
149 |
-
fig = px.line(
|
150 |
-
footprint_data,
|
151 |
-
x="Footprint Type",
|
152 |
-
y="Value",
|
153 |
-
title="Footprint Trends",
|
154 |
-
markers=True,
|
155 |
-
)
|
156 |
st.plotly_chart(fig)
|
157 |
-
|
158 |
else:
|
159 |
-
st.info("Please upload a dataset
|
|
|
21 |
st.sidebar.header("Step 1: Upload Dataset")
|
22 |
uploaded_file = st.sidebar.file_uploader("Upload your Excel file (.xlsx)", type=["xlsx"])
|
23 |
|
24 |
+
# Initialize data containers
|
25 |
fiber_impact_data = None
|
26 |
transport_impact_data = None
|
27 |
washing_impact_data = None
|
|
|
49 |
if uploaded_file:
|
50 |
fiber_impact_data, transport_impact_data, washing_impact_data = process_excel(uploaded_file)
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
# Function to calculate footprints
|
53 |
def calculate_footprints(weight, composition, lifecycle_inputs):
|
54 |
water_fp, energy_fp, carbon_fp = 0, 0, 0
|
|
|
75 |
energy_fp += washing_energy
|
76 |
carbon_fp += washing_carbon + (dryer_carbon * lifecycle_inputs["washing_cycles"])
|
77 |
|
78 |
+
# Convert water footprint from liters to kiloliters for visualization
|
79 |
+
water_fp_kL = water_fp / 1000
|
80 |
+
return water_fp_kL, energy_fp, carbon_fp
|
81 |
+
|
82 |
+
# Sidebar inputs for all scenarios
|
83 |
+
def get_inputs(key_prefix):
|
84 |
+
st.sidebar.subheader(f"Inputs for {key_prefix}")
|
85 |
+
product_weight = st.sidebar.number_input(f"{key_prefix} - Product Weight (kg)", min_value=0.01, step=0.01, value=0.5, key=f"{key_prefix}_weight")
|
86 |
+
|
87 |
+
st.sidebar.subheader(f"{key_prefix} - Material Composition (%)")
|
88 |
+
cotton = st.sidebar.number_input("Conventional Cotton (%)", min_value=0, max_value=100, value=50, step=1, key=f"{key_prefix}_cotton")
|
89 |
+
polyester = st.sidebar.number_input("Polyester (%)", min_value=0, max_value=100, value=30, step=1, key=f"{key_prefix}_polyester")
|
90 |
+
nylon = st.sidebar.number_input("Nylon 6 (%)", min_value=0, max_value=100, value=10, step=1, key=f"{key_prefix}_nylon")
|
91 |
+
acrylic = st.sidebar.number_input("Acrylic (%)", min_value=0, max_value=100, value=5, step=1, key=f"{key_prefix}_acrylic")
|
92 |
+
viscose = st.sidebar.number_input("Viscose (%)", min_value=0, max_value=100, value=5, step=1, key=f"{key_prefix}_viscose")
|
93 |
+
|
94 |
+
total_percentage = cotton + polyester + nylon + acrylic + viscose
|
95 |
+
if total_percentage != 100:
|
96 |
+
st.sidebar.error(f"Total composition for {key_prefix} must be 100%!")
|
97 |
+
|
98 |
+
composition = {
|
99 |
+
"Conventional Cotton": cotton,
|
100 |
+
"Polyester": polyester,
|
101 |
+
"Nylon 6": nylon,
|
102 |
+
"Acrylic": acrylic,
|
103 |
+
"Viscose": viscose,
|
104 |
+
}
|
105 |
+
|
106 |
+
st.sidebar.subheader(f"{key_prefix} - Lifecycle Inputs")
|
107 |
+
washing_cycles = st.sidebar.number_input(f"{key_prefix} - Washing Cycles", min_value=0, step=1, value=30, key=f"{key_prefix}_wash_cycles")
|
108 |
+
washing_temperature = st.sidebar.selectbox(f"{key_prefix} - Washing Temperature", list(washing_impact_data.keys()), key=f"{key_prefix}_wash_temp")
|
109 |
+
use_dryer = st.sidebar.checkbox(f"{key_prefix} - Use Tumble Dryer?", key=f"{key_prefix}_use_dryer")
|
110 |
+
transport_mode = st.sidebar.selectbox(f"{key_prefix} - Transport Mode", list(transport_impact_data.keys()), key=f"{key_prefix}_transport_mode")
|
111 |
+
transport_distance = st.sidebar.number_input(f"{key_prefix} - Transport Distance (km)", min_value=0, step=10, value=100, key=f"{key_prefix}_transport_distance")
|
112 |
+
|
113 |
lifecycle_inputs = {
|
|
|
|
|
114 |
"washing_temperature": washing_temperature,
|
115 |
"washing_cycles": washing_cycles,
|
116 |
"use_dryer": use_dryer,
|
117 |
+
"transport_mode": transport_mode,
|
118 |
+
"transport_distance": transport_distance,
|
119 |
}
|
120 |
+
|
121 |
+
return product_weight, composition, lifecycle_inputs
|
122 |
|
123 |
+
# Main interface
|
124 |
+
if uploaded_file and fiber_impact_data and transport_impact_data and washing_impact_data:
|
125 |
+
comparison_mode = st.sidebar.checkbox("Enable Comparison Mode")
|
126 |
+
|
|
|
|
|
|
|
|
|
127 |
if comparison_mode:
|
128 |
+
# Input for two scenarios
|
129 |
+
col1, col2 = st.columns(2)
|
130 |
+
with col1:
|
131 |
+
st.subheader("Scenario 1")
|
132 |
+
product_weight_1, composition_1, lifecycle_inputs_1 = get_inputs("Scenario 1")
|
133 |
+
with col2:
|
134 |
+
st.subheader("Scenario 2")
|
135 |
+
product_weight_2, composition_2, lifecycle_inputs_2 = get_inputs("Scenario 2")
|
136 |
+
|
137 |
+
# Calculations for both scenarios
|
138 |
+
water_fp_1, energy_fp_1, carbon_fp_1 = calculate_footprints(product_weight_1, composition_1, lifecycle_inputs_1)
|
139 |
+
water_fp_2, energy_fp_2, carbon_fp_2 = calculate_footprints(product_weight_2, composition_2, lifecycle_inputs_2)
|
140 |
+
|
141 |
+
# Combined visualization
|
142 |
+
st.subheader("Comparison Results")
|
143 |
+
comparison_data = pd.DataFrame({
|
144 |
+
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
|
145 |
+
"Scenario 1": [water_fp_1, energy_fp_1, carbon_fp_1],
|
146 |
+
"Scenario 2": [water_fp_2, energy_fp_2, carbon_fp_2],
|
147 |
+
})
|
148 |
+
fig = px.bar(
|
149 |
+
comparison_data,
|
150 |
+
x="Footprint Type",
|
151 |
+
y=["Scenario 1", "Scenario 2"],
|
152 |
+
barmode="group",
|
153 |
+
title="Comparison of Footprints"
|
154 |
+
)
|
155 |
+
st.plotly_chart(fig)
|
156 |
else:
|
157 |
+
# Input for single scenario
|
158 |
+
st.subheader("Single Scenario")
|
159 |
+
product_weight, composition, lifecycle_inputs = get_inputs("Single Scenario")
|
160 |
+
water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, lifecycle_inputs)
|
161 |
+
|
162 |
+
# Display results
|
163 |
+
st.subheader("Results")
|
164 |
+
st.markdown(f"- **Water Footprint**: {water_fp:.2f} kL")
|
165 |
+
st.markdown(f"- **Energy Footprint**: {energy_fp:.2f} MJ")
|
166 |
+
st.markdown(f"- **Carbon Footprint**: {carbon_fp:.2f} kg CO2e")
|
167 |
+
|
168 |
+
# Visualization
|
169 |
+
result_data = pd.DataFrame({
|
170 |
+
"Footprint Type": ["Water (kL)", "Energy (MJ)", "Carbon (kg CO2e)"],
|
171 |
"Value": [water_fp, energy_fp, carbon_fp],
|
172 |
})
|
173 |
+
fig = px.line(result_data, x="Footprint Type", y="Value", markers=True, title="Footprint Trends")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
st.plotly_chart(fig)
|
|
|
175 |
else:
|
176 |
+
st.info("Please upload a dataset to proceed.")
|