ZainMalik0925 commited on
Commit
9ac640d
·
verified ·
1 Parent(s): 44ffb48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -23
app.py CHANGED
@@ -105,6 +105,8 @@ transport_distance = st.sidebar.number_input("Transport Distance (km)", min_valu
105
 
106
  # Function to calculate footprints
107
  def calculate_footprints(weight, composition, lifecycle_inputs):
 
 
108
  # Initialize footprints
109
  water_footprint = 0
110
  energy_footprint = 0
@@ -114,28 +116,36 @@ def calculate_footprints(weight, composition, lifecycle_inputs):
114
  for fiber, percentage in composition.items():
115
  if fiber in fiber_impact_data:
116
  data = fiber_impact_data[fiber]
 
117
  fraction = percentage / 100
118
  water_footprint += data["Water"] * weight * fraction
119
  energy_footprint += data["Energy"] * weight * fraction
120
  carbon_footprint += data["Carbon"] * weight * fraction
121
 
 
 
122
  # Transportation impacts
123
  transport_factor = {
124
  "Plane": 1.102,
125
  "Ship": 0.011,
126
  "Train": 0.05,
127
  "Truck": 0.25,
128
- }[lifecycle_inputs["transport_mode"]]
129
-
130
  transport_emissions = transport_factor * lifecycle_inputs["transport_distance"] * weight
131
  carbon_footprint += transport_emissions
132
 
 
 
133
  # Washing and drying impacts
134
  washing_energy = {"Cold": 0.02, "30°C": 0.1, "40°C": 0.2, "60°C": 0.5}
135
  dryer_energy = 0.5 if lifecycle_inputs["use_dryer"] else 0
136
- carbon_footprint += (washing_energy[lifecycle_inputs["washing_temperature"]] * lifecycle_inputs["washing_cycles"] * 0.05)
 
 
137
  energy_footprint += dryer_energy * lifecycle_inputs["washing_cycles"]
138
 
 
 
139
  return water_footprint, energy_footprint, carbon_footprint
140
 
141
  # User inputs as a dictionary
@@ -157,27 +167,30 @@ composition = {
157
  }
158
 
159
  # Run Calculations
160
- if fiber_impact_data and total_percentage == 100:
161
- water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, user_inputs)
 
162
 
163
- # Display results
164
- st.subheader("Calculated Results")
165
- st.markdown(f"""
166
  - **Water Footprint**: {water_fp:.2f} liters
167
  - **Energy Footprint**: {energy_fp:.2f} MJ
168
  - **Carbon Footprint**: {carbon_fp:.2f} kgCO2e
169
- """)
170
-
171
- # 3D Visualization
172
- fig = go.Figure()
173
- fig.add_trace(go.Bar(
174
- x=["Water Footprint", "Energy Footprint", "Carbon Footprint"],
175
- y=[water_fp, energy_fp, carbon_fp],
176
- text=[f"{water_fp:.2f} L", f"{energy_fp:.2f} MJ", f"{carbon_fp:.2f} kgCO2e"],
177
- textposition='auto',
178
- marker=dict(color=["blue", "orange", "green"])
179
- ))
180
- fig.update_layout(title="Footprint Breakdown", xaxis_title="Footprint Type", yaxis_title="Value")
181
- st.plotly_chart(fig)
182
- else:
183
- st.error("Ensure dataset is loaded and composition sums to 100%.")
 
 
 
105
 
106
  # Function to calculate footprints
107
  def calculate_footprints(weight, composition, lifecycle_inputs):
108
+ st.write("Inputs to calculate_footprints:", weight, composition, lifecycle_inputs) # Log inputs for debugging
109
+
110
  # Initialize footprints
111
  water_footprint = 0
112
  energy_footprint = 0
 
116
  for fiber, percentage in composition.items():
117
  if fiber in fiber_impact_data:
118
  data = fiber_impact_data[fiber]
119
+ st.write(f"Processing {fiber} with percentage {percentage}%") # Debugging
120
  fraction = percentage / 100
121
  water_footprint += data["Water"] * weight * fraction
122
  energy_footprint += data["Energy"] * weight * fraction
123
  carbon_footprint += data["Carbon"] * weight * fraction
124
 
125
+ st.write("Footprints after fiber contributions:", water_footprint, energy_footprint, carbon_footprint) # Debug
126
+
127
  # Transportation impacts
128
  transport_factor = {
129
  "Plane": 1.102,
130
  "Ship": 0.011,
131
  "Train": 0.05,
132
  "Truck": 0.25,
133
+ }.get(lifecycle_inputs["transport_mode"], 0)
 
134
  transport_emissions = transport_factor * lifecycle_inputs["transport_distance"] * weight
135
  carbon_footprint += transport_emissions
136
 
137
+ st.write("Carbon footprint after transport:", carbon_footprint) # Debug
138
+
139
  # Washing and drying impacts
140
  washing_energy = {"Cold": 0.02, "30°C": 0.1, "40°C": 0.2, "60°C": 0.5}
141
  dryer_energy = 0.5 if lifecycle_inputs["use_dryer"] else 0
142
+ carbon_footprint += (
143
+ washing_energy[lifecycle_inputs["washing_temperature"]] * lifecycle_inputs["washing_cycles"] * 0.05
144
+ )
145
  energy_footprint += dryer_energy * lifecycle_inputs["washing_cycles"]
146
 
147
+ st.write("Final footprints:", water_footprint, energy_footprint, carbon_footprint) # Debug
148
+
149
  return water_footprint, energy_footprint, carbon_footprint
150
 
151
  # User inputs as a dictionary
 
167
  }
168
 
169
  # Run Calculations
170
+ try:
171
+ if fiber_impact_data and total_percentage == 100:
172
+ water_fp, energy_fp, carbon_fp = calculate_footprints(product_weight, composition, user_inputs)
173
 
174
+ # Display results
175
+ st.subheader("Calculated Results")
176
+ st.markdown(f"""
177
  - **Water Footprint**: {water_fp:.2f} liters
178
  - **Energy Footprint**: {energy_fp:.2f} MJ
179
  - **Carbon Footprint**: {carbon_fp:.2f} kgCO2e
180
+ """)
181
+
182
+ # Visualization
183
+ fig = go.Figure()
184
+ fig.add_trace(go.Bar(
185
+ x=["Water Footprint", "Energy Footprint", "Carbon Footprint"],
186
+ y=[water_fp, energy_fp, carbon_fp],
187
+ text=[f"{water_fp:.2f} L", f"{energy_fp:.2f} MJ", f"{carbon_fp:.2f} kgCO2e"],
188
+ textposition="auto",
189
+ marker=dict(color=["blue", "orange", "green"])
190
+ ))
191
+ fig.update_layout(title="Footprint Breakdown", xaxis_title="Footprint Type", yaxis_title="Value")
192
+ st.plotly_chart(fig)
193
+ else:
194
+ st.error("Ensure dataset is loaded and the composition sums to 100%.")
195
+ except Exception as e:
196
+ st.error(f"An error occurred during calculations: {e}")