Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92e703d

Browse files
Merge pull request #299 from AswinPKumar01/add-carbon-footprint-calc
Add carbon footprint calculator
2 parents 06925c8 + 5ae07ba commit 92e703d

File tree

7 files changed

+515
-0
lines changed

7 files changed

+515
-0
lines changed

‎C/Carbon-Footprint-Calculator/README.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# **Carbon Footprint Calculator**
2+
3+
An interactive web application that calculates your annual carbon footprint based on your daily activities, including transportation, home energy use, dietary habits, and waste management. The goal is to raise environmental awareness and encourage sustainable living by providing personalized feedback and suggestions for reducing carbon emissions.
4+
5+
---
6+
7+
## **Table of Contents**
8+
9+
- [Features](#features)
10+
- [Prerequisites](#prerequisites)
11+
- [Installation](#installation)
12+
- [Usage](#usage)
13+
- [Project Structure](#project-structure)
14+
- [Technologies Used](#technologies-used)
15+
16+
---
17+
18+
## **Features**
19+
20+
- **Transportation Emissions Calculation**
21+
- Calculates emissions from car travel, public transportation, and flights.
22+
- **Home Energy Use Analysis**
23+
- Accounts for electricity, natural gas, fuel oil, and propane usage.
24+
- **Dietary Impact Assessment**
25+
- Considers the carbon footprint of different diets: meat-heavy, average, vegetarian, and vegan.
26+
- **Waste and Recycling Evaluation**
27+
- Factors in recycling habits and their impact on emissions.
28+
- **User-Friendly Web Interface**
29+
- Interactive forms for data input and clear presentation of results.
30+
- **Personalized Suggestions**
31+
- Offers practical tips to reduce your carbon footprint based on your inputs.
32+
- **Responsive Design**
33+
- Accessible on various devices, including desktops, tablets, and mobile phones.
34+
35+
---
36+
37+
## **Prerequisites**
38+
39+
- **Python 3.7 or higher**
40+
- **pip package manager**
41+
- **Virtual Environment (optional but recommended)**
42+
43+
---
44+
45+
## **Installation**
46+
47+
Follow these steps to set up the project on your local machine.
48+
49+
### **1. Clone the Repository**
50+
51+
```bash
52+
git clone https://github.com/yourusername/carbon-footprint-calculator.git
53+
cd carbon-footprint-calculator
54+
```
55+
56+
### **2. Create a Virtual Environment**
57+
58+
It's recommended to use a virtual environment to manage dependencies.
59+
60+
```bash
61+
python -m venv venv
62+
```
63+
64+
Activate the virtual environment:
65+
66+
- **Windows:**
67+
68+
```bash
69+
venv\Scripts\activate
70+
```
71+
72+
- **macOS/Linux:**
73+
74+
```bash
75+
source venv/bin/activate
76+
```
77+
78+
### **3. Upgrade pip**
79+
80+
```bash
81+
pip install --upgrade pip
82+
```
83+
84+
### **4. Install Dependencies**
85+
86+
```bash
87+
pip install -r requirements.txt
88+
```
89+
90+
Ensure that the installed packages match the versions specified in `requirements.txt`:
91+
92+
```
93+
Flask==2.3.2
94+
Werkzeug==3.0.4
95+
```
96+
97+
### **5. Run the Application**
98+
99+
```bash
100+
python app.py
101+
```
102+
103+
The application will start running on `http://127.0.0.1:5000/`.
104+
105+
---
106+
107+
## **Usage**
108+
109+
### **Access the Application**
110+
111+
Open your web browser and navigate to `http://127.0.0.1:5000/`.
112+
113+
### **Input Your Data**
114+
115+
1. **Transportation Emissions**
116+
117+
- Enter your average weekly miles driven by car.
118+
- Provide your car's fuel efficiency (mpg).
119+
- Input miles traveled by bus and train.
120+
- Specify the number of flights taken per year.
121+
122+
2. **Home Energy Use**
123+
124+
- Enter your average monthly electricity usage in kWh.
125+
- Provide natural gas, fuel oil, and propane usage if applicable.
126+
127+
3. **Dietary Impact**
128+
129+
- Select your diet type from the options provided.
130+
131+
4. **Waste and Recycling**
132+
- Indicate whether you recycle paper, plastic, glass, and metal.
133+
134+
### **Calculate and View Results**
135+
136+
- Click the **Calculate** button to submit your data.
137+
- The results page will display:
138+
- A breakdown of your annual emissions by category.
139+
- Total annual emissions in pounds and metric tons of CO2.
140+
- Comparison with the national average.
141+
- Personalized suggestions to reduce your carbon footprint.
142+
143+
### **Calculate Again**
144+
145+
- Use the **Calculate Again** button to return to the input form and reassess with different data.
146+
147+
---
148+
149+
## **Project Structure**
150+
151+
```
152+
carbon-footprint-calculator/
153+
├── app.py
154+
├── requirements.txt
155+
├── templates/
156+
│ ├── base.html
157+
│ ├── index.html
158+
│ └── results.html
159+
├── static/
160+
│ ├── style.css
161+
├── README.md
162+
```
163+
164+
- **app.py**: The main Flask application file containing routes and logic.
165+
- **requirements.txt**: Lists the Python dependencies and their versions.
166+
- **templates/**: Contains HTML templates for rendering web pages.
167+
- **base.html**: The base template that includes common elements.
168+
- **index.html**: The home page with the input form.
169+
- **results.html**: Displays calculation results and suggestions.
170+
- **static/**: Stores static files like CSS and images.
171+
- **style.css**: Custom styles for the application.
172+
- **screenshots/**: Contains images used in the README.
173+
- **README.md**: Comprehensive guide and documentation for the project.
174+
175+
---
176+
177+
## **Technologies Used**
178+
179+
- **Programming Language**: Python 3
180+
- **Web Framework**: Flask
181+
- **Frontend Libraries**:
182+
- **Bootstrap 5**: For responsive design and styling.
183+
- **HTML/CSS**: Web page structure and styles.
184+
185+
### **Troubleshooting**
186+
187+
#### **ImportError: cannot import name 'url_quote' from 'werkzeug.urls'**
188+
189+
If you encounter the following error:
190+
191+
```
192+
ImportError: cannot import name 'url_quote' from 'werkzeug.urls'
193+
```
194+
195+
This is due to a version incompatibility between Flask and Werkzeug. Ensure that you have compatible versions installed:
196+
197+
- **Flask**: `2.3.2` or higher
198+
- **Werkzeug**: `3.0.4` or compatible with your Flask version
199+
200+
#### **Steps to Resolve**
201+
202+
1. **Update `requirements.txt`**:
203+
204+
```
205+
Flask==2.3.2
206+
Werkzeug==3.0.4
207+
```
208+
209+
2. **Reinstall Dependencies**:
210+
211+
```bash
212+
pip install -r requirements.txt
213+
```
214+
215+
3. **Verify Installed Versions**:
216+
217+
```bash
218+
pip show Flask
219+
pip show Werkzeug
220+
```
221+
222+
## **Contact**
223+
224+
For any questions or suggestions, feel free to reach out:
225+
226+
- **Email**: aswinpkumar03@gmail.com
227+
- **GitHub**: [AswinPKumar01](https://github.com/AswinPKumar01)
228+
229+
---
230+
231+
Thank you for using the Carbon Footprint Calculator! Together, let's make a positive impact on the environment.

‎C/Carbon-Footprint-Calculator/app.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from flask import Flask, render_template, request
2+
3+
app = Flask(__name__)
4+
5+
@app.route('/')
6+
def index():
7+
return render_template('index.html')
8+
9+
@app.route('/calculate', methods=['POST'])
10+
def calculate():
11+
# Transportation Data
12+
car_miles = float(request.form.get('car_miles', 0))
13+
car_mpg = float(request.form.get('car_mpg', 0))
14+
bus_miles = float(request.form.get('bus_miles', 0))
15+
train_miles = float(request.form.get('train_miles', 0))
16+
flights_per_year = int(request.form.get('flights_per_year', 0))
17+
18+
# Energy Data
19+
electricity_kwh = float(request.form.get('electricity_kwh', 0))
20+
natural_gas_therms = float(request.form.get('natural_gas_therms', 0))
21+
fuel_oil_gallons = float(request.form.get('fuel_oil_gallons', 0))
22+
propane_gallons = float(request.form.get('propane_gallons', 0))
23+
24+
# Diet Data
25+
diet_choice = request.form.get('diet_choice', '2')
26+
27+
# Waste Data
28+
recycle_paper = request.form.get('recycle_paper', 'no')
29+
recycle_plastic = request.form.get('recycle_plastic', 'no')
30+
recycle_glass = request.form.get('recycle_glass', 'no')
31+
recycle_metal = request.form.get('recycle_metal', 'no')
32+
33+
# Calculations
34+
total_transport = get_transportation_emissions(car_miles, car_mpg, bus_miles, train_miles, flights_per_year)
35+
total_energy = get_energy_emissions(electricity_kwh, natural_gas_therms, fuel_oil_gallons, propane_gallons)
36+
total_diet = get_diet_emissions(diet_choice)
37+
total_waste = get_waste_emissions(recycle_paper, recycle_plastic, recycle_glass, recycle_metal)
38+
39+
total_emissions = total_transport + total_energy + total_diet + total_waste
40+
total_emissions_metric_tons = total_emissions * 0.000453592
41+
42+
# National average comparison (approximate)
43+
national_average = 16000 # lbs CO2 per person per year
44+
below_average = total_emissions < national_average
45+
46+
return render_template('results.html',
47+
total_transport=total_transport,
48+
total_energy=total_energy,
49+
total_diet=total_diet,
50+
total_waste=total_waste,
51+
total_emissions=total_emissions,
52+
total_emissions_metric_tons=total_emissions_metric_tons,
53+
below_average=below_average)
54+
55+
def get_transportation_emissions(car_miles, car_mpg, bus_miles, train_miles, flights_per_year):
56+
# Emission factors
57+
gas_emission_factor = 19.6 # lbs CO2 per gallon of gasoline
58+
bus_emission_factor = 0.65 # lbs CO2 per passenger mile
59+
train_emission_factor = 0.41 # lbs CO2 per passenger mile
60+
flight_emission_factor = 1100 # lbs CO2 per 4-hour flight
61+
62+
# Calculations
63+
annual_car_miles = car_miles * 52
64+
annual_gallons = annual_car_miles / car_mpg if car_mpg != 0 else 0
65+
car_emissions = annual_gallons * gas_emission_factor
66+
67+
bus_emissions = bus_miles * 52 * bus_emission_factor
68+
train_emissions = train_miles * 52 * train_emission_factor
69+
flight_emissions = flights_per_year * flight_emission_factor
70+
71+
total_transportation_emissions = car_emissions + bus_emissions + train_emissions + flight_emissions
72+
73+
return total_transportation_emissions
74+
75+
def get_energy_emissions(electricity_kwh, natural_gas_therms, fuel_oil_gallons, propane_gallons):
76+
# Emission factors
77+
electricity_emission_factor = 1.22 # lbs CO2 per kWh
78+
natural_gas_emission_factor = 11.7 # lbs CO2 per therm
79+
fuel_oil_emission_factor = 22.4 # lbs CO2 per gallon
80+
propane_emission_factor = 12.7 # lbs CO2 per gallon
81+
82+
# Calculations
83+
annual_electricity_emissions = electricity_kwh * 12 * electricity_emission_factor
84+
annual_natural_gas_emissions = natural_gas_therms * 12 * natural_gas_emission_factor
85+
annual_fuel_oil_emissions = fuel_oil_gallons * 12 * fuel_oil_emission_factor
86+
annual_propane_emissions = propane_gallons * 12 * propane_emission_factor
87+
88+
total_energy_emissions = (annual_electricity_emissions + annual_natural_gas_emissions +
89+
annual_fuel_oil_emissions + annual_propane_emissions)
90+
91+
return total_energy_emissions
92+
93+
def get_diet_emissions(diet_choice):
94+
# Emission factors (lbs CO2 per year)
95+
diet_emissions_dict = {
96+
'1': 3300, # Meat-heavy diet
97+
'2': 2800, # Average diet
98+
'3': 1500, # Vegetarian
99+
'4': 1000 # Vegan
100+
}
101+
diet_emissions = diet_emissions_dict.get(diet_choice, 2800)
102+
return diet_emissions
103+
104+
def get_waste_emissions(recycle_paper, recycle_plastic, recycle_glass, recycle_metal):
105+
# Baseline emissions
106+
waste_emissions = 692 # lbs CO2 per person per year
107+
108+
# Reductions
109+
if recycle_paper.lower() == 'yes':
110+
waste_emissions -= 79
111+
if recycle_plastic.lower() == 'yes':
112+
waste_emissions -= 35
113+
if recycle_glass.lower() == 'yes':
114+
waste_emissions -= 25
115+
if recycle_metal.lower() == 'yes':
116+
waste_emissions -= 46
117+
118+
return waste_emissions
119+
120+
if __name__ == '__main__':
121+
app.run(debug=True)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask==2.3.2
2+
Werkzeug==3.0.4
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
body {
2+
background-color: #f8f9fa;
3+
}
4+
5+
h1 {
6+
margin-bottom: 30px;
7+
}
8+
9+
.form-label {
10+
font-weight: bold;
11+
}
12+
13+
.btn-primary {
14+
width: 100%;
15+
}
16+
17+
.mt-4 {
18+
margin-top: 2rem;
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Carbon Footprint Calculator</title>
7+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
8+
<!-- Bootstrap CDN for styling -->
9+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
10+
</head>
11+
12+
<body>
13+
<div class="container my-5">
14+
{% block content %}{% endblock %}
15+
</div>
16+
</body>
17+
18+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /