Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
purchased_goods_values = ["Cement", "Plaster", "Paint", "Timber", "Concrete"]
|
4 |
+
supplier_values = ["Supplier C", "Supplier D", "Supplier E", "Supplier F", "Supplier G"]
|
5 |
+
scope_values = ["Electricity", "Natural Gas"]
|
6 |
+
material_inputs_values = ["Cotton", "Polymer", "Chemical A", "Chemical B"]
|
7 |
+
transport_values = ["Cotton", "Polymer", "Chemical A", "Chemical B"]
|
8 |
+
waste_output_values = ["Waste sent to landfill"]
|
9 |
+
|
10 |
+
def calculate_emissions_supplier_specific(purchased_goods_data):
|
11 |
+
total_emissions = sum([qty * emission_factor for _, _, qty, emission_factor in purchased_goods_data])
|
12 |
+
st.header(f"Total Emissions for Supplier-specific Method: {total_emissions} kg CO2e")
|
13 |
+
return total_emissions
|
14 |
+
|
15 |
+
def calculate_emissions_hybrid(scope1_and_scope2_data, material_inputs_data, transport_data, waste_output_data):
|
16 |
+
scope1_and_scope2_emissions = sum([float(item['Amount (kWh)']) * float(item['Emission factor (kg CO2e/kWh)']) for item in scope1_and_scope2_data])
|
17 |
+
waste_output_emissions = sum([float(item['Amount (kg)']) * float(item['Emission factor (kg CO2e/kg of waste sent to landfill)']) for item in waste_output_data])
|
18 |
+
other_upstream_emissions = sum([float(item['Mass purchased (kg)']) * float(item['Emission factor (kg CO2e/kg)']) for item in material_inputs_data])
|
19 |
+
total_emissions = scope1_and_scope2_emissions + waste_output_emissions + other_upstream_emissions
|
20 |
+
|
21 |
+
transport_emissions_per_item = [
|
22 |
+
float(item['Distance of transport (km)']) * float(item1['Mass purchased (kg)']) * float(item['Vehicle type emission factor (kg CO2e/kg/km)'])
|
23 |
+
for item in transport_data for item1 in material_inputs_data if item["Purchased Goods"] == item1["Purchased Goods"]
|
24 |
+
]
|
25 |
+
|
26 |
+
for i, item in enumerate(transport_data):
|
27 |
+
st.header(f"Emissions for Purchased Item {i + 1}: {transport_emissions_per_item[i]} kg CO2e")
|
28 |
+
|
29 |
+
return total_emissions
|
30 |
+
|
31 |
+
def calculate_emissions_hybrid_pro(tshirt_data, scope_data, waste_output_data):
|
32 |
+
scope1_and_scope2_emissions = sum([float(item['Amount (kWh)']) * float(item['Emission factor (kg CO2e/kWh)']) for item in scope_data])
|
33 |
+
waste_output_emissions = sum([float(item['Amount (kg)']) * float(item['Emission factor (kg CO2e/kg of waste sent to landfill)']) for item in waste_output_data])
|
34 |
+
other_upstream_emissions = sum([float(item['Number of t-shirts purchased']) * float(item['Cradle-to-gate process emission factor (kg CO2e/per t-shirt(excluding scopes)']) for item in tshirt_data])
|
35 |
+
total_emissions = scope1_and_scope2_emissions + waste_output_emissions + other_upstream_emissions
|
36 |
+
st.header(f"Total Emissions for HybridPro Method: {total_emissions} kg CO2e")
|
37 |
+
return total_emissions
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
def main():
|
42 |
+
st.title("CO2 Emission Calculator")
|
43 |
+
|
44 |
+
method_options = ["Supplier Specific Method", "Hybrid Method", "HybridPro Method"]
|
45 |
+
method = st.selectbox("Select Method", method_options)
|
46 |
+
|
47 |
+
if method == "Supplier Specific Method":
|
48 |
+
st.header("Supplier Specific Method")
|
49 |
+
num_items = st.number_input("Number of items", min_value=1, step=1)
|
50 |
+
purchased_goods_data = []
|
51 |
+
for i in range(num_items):
|
52 |
+
goods = st.selectbox(f"Purchased Goods {i + 1}", purchased_goods_values, key=f"goods_{i}")
|
53 |
+
supplier = st.selectbox(f"Supplier {i + 1}", supplier_values, key=f"supplier_{i}")
|
54 |
+
qty = st.number_input(f"Qty Purchased (kg) {i + 1}", min_value=0.0, step=0.01, key=f"qty_{i}")
|
55 |
+
emission_factor = st.number_input(f"Supplier-specific Emission Factor (kg CO2e/kg) {i + 1}", min_value=0.0, step=0.01, key=f"emission_factor_{i}")
|
56 |
+
purchased_goods_data.append((goods, supplier, qty, emission_factor))
|
57 |
+
total_emissions = calculate_emissions_supplier_specific(purchased_goods_data)
|
58 |
+
|
59 |
+
elif method == "Hybrid Method":
|
60 |
+
st.header("Hybrid Method")
|
61 |
+
scope1_and_scope2_data = dynamic_input_fields_with_dropdown("Scope 1 and Scope 2 data from supplier B relating to production of purchased goods", "Enter scope 1 and scope 2 data", scope_values, ["Category","Amount (kWh)", "Emission factor (kg CO2e/kWh)"])
|
62 |
+
material_inputs_data = dynamic_input_fields_with_dropdown("Material inputs of purchased goods", "Enter material input data", material_inputs_values, ["Purchased Goods", "Mass purchased (kg)", "Emission factor (kg CO2e/kg)"])
|
63 |
+
transport_data = dynamic_input_fields_with_dropdown("Transport of material inputs to supplier B", "Enter transport data", transport_values, ["Purchased Goods", "Distance of transport (km)", "Vehicle type emission factor (kg CO2e/kg/km)"])
|
64 |
+
waste_output_data = dynamic_input_fields_with_emission_factor("Waste outputs by supplier B relating to production of purchased goods", "Enter waste output data", waste_output_values, ["Amount (kg)", "Emission factor (kg CO2e/kg of waste sent to landfill)"])
|
65 |
+
total_emissions = calculate_emissions_hybrid(scope1_and_scope2_data, material_inputs_data, transport_data, waste_output_data)
|
66 |
+
|
67 |
+
elif method == "HybridPro Method":
|
68 |
+
scope_data = dynamic_input_fields_with_dropdown("Scope 1 and Scope 2 data from supplier B", "Enter scope data", scope_values, ["Category","Amount (kWh)", "Emission factor (kg CO2e/kWh)"])
|
69 |
+
tshirt_data = dynamic_input_fields_with_emission_factor("T-shirts", "Enter T-shirt data", purchased_goods_values,
|
70 |
+
["Number of t-shirts purchased",
|
71 |
+
"Cradle-to-gate process emission factor (kg CO2e/per t-shirt)","Cradle-to-gate process emission factor (kg CO2e/per t-shirt(excluding scopes)"])
|
72 |
+
waste_output_data = dynamic_input_fields_with_emission_factor("Waste outputs by supplier B", "Enter waste output data", waste_output_values,
|
73 |
+
["Amount (kg)", "Emission factor (kg CO2e/kg of waste sent to landfill)"])
|
74 |
+
total_emissions = calculate_emissions_hybrid_pro(tshirt_data, scope_data, waste_output_data)
|
75 |
+
|
76 |
+
def dynamic_input_fields(label, values, headings):
|
77 |
+
num_items = st.number_input(f"**Number of {label} items**", min_value=1, step=1, key=f"{label}_num_items")
|
78 |
+
input_fields = []
|
79 |
+
for i in range(num_items):
|
80 |
+
st.subheader(f"{label} {i + 1}")
|
81 |
+
input_data = {}
|
82 |
+
for value, heading in zip(values, headings):
|
83 |
+
input_data[value] = st.number_input(f"{heading} {i + 1}", min_value=0, step=0.01, key=f"{label}_{i}_{value}")
|
84 |
+
input_fields.append(input_data)
|
85 |
+
return input_fields
|
86 |
+
|
87 |
+
def dynamic_input_fields_with_dropdown(label, prompt, values, headings):
|
88 |
+
num_items = st.number_input(f"**Number of {label} items**", min_value=1, step=1, key=f"{label}_num_items")
|
89 |
+
input_fields = []
|
90 |
+
for i in range(num_items):
|
91 |
+
st.subheader(f"{label} {i + 1}")
|
92 |
+
input_data = {}
|
93 |
+
input_data[headings[0]] = st.selectbox(f"{headings[0]} {i + 1}", values, key=f"{label}_{i}_{headings[0]}")
|
94 |
+
for heading in headings[1:]:
|
95 |
+
input_data[heading] = st.number_input(f"{heading} {i + 1}", min_value=0.0, step=0.01, key=f"{label}_{i}_{heading}")
|
96 |
+
input_fields.append(input_data)
|
97 |
+
return input_fields
|
98 |
+
|
99 |
+
def dynamic_input_fields_with_emission_factor(label, prompt, values, headings):
|
100 |
+
num_items = st.number_input(f"**Number of {label} items**", min_value=1, step=1, key=f"{label}_num_items")
|
101 |
+
input_fields = []
|
102 |
+
for i in range(num_items):
|
103 |
+
st.subheader(f"{label} {i + 1}")
|
104 |
+
input_data = {}
|
105 |
+
for heading in headings:
|
106 |
+
input_data[heading] = st.number_input(f"{heading} {i + 1}", min_value=0.0, step=0.01, key=f"{label}_{i}_{heading}")
|
107 |
+
input_fields.append(input_data)
|
108 |
+
return input_fields
|
109 |
+
|
110 |
+
if __name__ == "__main__":
|
111 |
+
main()
|