Skip to content

Commit

Permalink
Merge pull request #299 from AswinPKumar01/add-carbon-footprint-calc
Browse files Browse the repository at this point in the history
Add carbon footprint calculator
  • Loading branch information
Techiral authored Oct 6, 2024
2 parents 06925c8 + 5ae07ba commit 92e703d
Show file tree
Hide file tree
Showing 7 changed files with 515 additions and 0 deletions.
231 changes: 231 additions & 0 deletions C/Carbon-Footprint-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
# **Carbon Footprint Calculator**

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.

---

## **Table of Contents**

- [Features](#features)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Usage](#usage)
- [Project Structure](#project-structure)
- [Technologies Used](#technologies-used)

---

## **Features**

- **Transportation Emissions Calculation**
- Calculates emissions from car travel, public transportation, and flights.
- **Home Energy Use Analysis**
- Accounts for electricity, natural gas, fuel oil, and propane usage.
- **Dietary Impact Assessment**
- Considers the carbon footprint of different diets: meat-heavy, average, vegetarian, and vegan.
- **Waste and Recycling Evaluation**
- Factors in recycling habits and their impact on emissions.
- **User-Friendly Web Interface**
- Interactive forms for data input and clear presentation of results.
- **Personalized Suggestions**
- Offers practical tips to reduce your carbon footprint based on your inputs.
- **Responsive Design**
- Accessible on various devices, including desktops, tablets, and mobile phones.

---

## **Prerequisites**

- **Python 3.7 or higher**
- **pip package manager**
- **Virtual Environment (optional but recommended)**

---

## **Installation**

Follow these steps to set up the project on your local machine.

### **1. Clone the Repository**

```bash
git clone https://github.com/yourusername/carbon-footprint-calculator.git
cd carbon-footprint-calculator
```

### **2. Create a Virtual Environment**

It's recommended to use a virtual environment to manage dependencies.

```bash
python -m venv venv
```

Activate the virtual environment:

- **Windows:**

```bash
venv\Scripts\activate
```

- **macOS/Linux:**

```bash
source venv/bin/activate
```

### **3. Upgrade pip**

```bash
pip install --upgrade pip
```

### **4. Install Dependencies**

```bash
pip install -r requirements.txt
```

Ensure that the installed packages match the versions specified in `requirements.txt`:

```
Flask==2.3.2
Werkzeug==3.0.4
```

### **5. Run the Application**

```bash
python app.py
```

The application will start running on `http://127.0.0.1:5000/`.

---

## **Usage**

### **Access the Application**

Open your web browser and navigate to `http://127.0.0.1:5000/`.

### **Input Your Data**

1. **Transportation Emissions**

- Enter your average weekly miles driven by car.
- Provide your car's fuel efficiency (mpg).
- Input miles traveled by bus and train.
- Specify the number of flights taken per year.

2. **Home Energy Use**

- Enter your average monthly electricity usage in kWh.
- Provide natural gas, fuel oil, and propane usage if applicable.

3. **Dietary Impact**

- Select your diet type from the options provided.

4. **Waste and Recycling**
- Indicate whether you recycle paper, plastic, glass, and metal.

### **Calculate and View Results**

- Click the **Calculate** button to submit your data.
- The results page will display:
- A breakdown of your annual emissions by category.
- Total annual emissions in pounds and metric tons of CO₂.
- Comparison with the national average.
- Personalized suggestions to reduce your carbon footprint.

### **Calculate Again**

- Use the **Calculate Again** button to return to the input form and reassess with different data.

---

## **Project Structure**

```
carbon-footprint-calculator/
├── app.py
├── requirements.txt
├── templates/
│ ├── base.html
│ ├── index.html
│ └── results.html
├── static/
│ ├── style.css
├── README.md
```

- **app.py**: The main Flask application file containing routes and logic.
- **requirements.txt**: Lists the Python dependencies and their versions.
- **templates/**: Contains HTML templates for rendering web pages.
- **base.html**: The base template that includes common elements.
- **index.html**: The home page with the input form.
- **results.html**: Displays calculation results and suggestions.
- **static/**: Stores static files like CSS and images.
- **style.css**: Custom styles for the application.
- **screenshots/**: Contains images used in the README.
- **README.md**: Comprehensive guide and documentation for the project.

---

## **Technologies Used**

- **Programming Language**: Python 3
- **Web Framework**: Flask
- **Frontend Libraries**:
- **Bootstrap 5**: For responsive design and styling.
- **HTML/CSS**: Web page structure and styles.

### **Troubleshooting**

#### **ImportError: cannot import name 'url_quote' from 'werkzeug.urls'**

If you encounter the following error:

```
ImportError: cannot import name 'url_quote' from 'werkzeug.urls'
```

This is due to a version incompatibility between Flask and Werkzeug. Ensure that you have compatible versions installed:

- **Flask**: `2.3.2` or higher
- **Werkzeug**: `3.0.4` or compatible with your Flask version

#### **Steps to Resolve**

1. **Update `requirements.txt`**:

```
Flask==2.3.2
Werkzeug==3.0.4
```

2. **Reinstall Dependencies**:

```bash
pip install -r requirements.txt
```

3. **Verify Installed Versions**:

```bash
pip show Flask
pip show Werkzeug
```

## **Contact**

For any questions or suggestions, feel free to reach out:

- **Email**: [email protected]
- **GitHub**: [AswinPKumar01](https://github.com/AswinPKumar01)

---

Thank you for using the Carbon Footprint Calculator! Together, let's make a positive impact on the environment.
121 changes: 121 additions & 0 deletions C/Carbon-Footprint-Calculator/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/calculate', methods=['POST'])
def calculate():
# Transportation Data
car_miles = float(request.form.get('car_miles', 0))
car_mpg = float(request.form.get('car_mpg', 0))
bus_miles = float(request.form.get('bus_miles', 0))
train_miles = float(request.form.get('train_miles', 0))
flights_per_year = int(request.form.get('flights_per_year', 0))

# Energy Data
electricity_kwh = float(request.form.get('electricity_kwh', 0))
natural_gas_therms = float(request.form.get('natural_gas_therms', 0))
fuel_oil_gallons = float(request.form.get('fuel_oil_gallons', 0))
propane_gallons = float(request.form.get('propane_gallons', 0))

# Diet Data
diet_choice = request.form.get('diet_choice', '2')

# Waste Data
recycle_paper = request.form.get('recycle_paper', 'no')
recycle_plastic = request.form.get('recycle_plastic', 'no')
recycle_glass = request.form.get('recycle_glass', 'no')
recycle_metal = request.form.get('recycle_metal', 'no')

# Calculations
total_transport = get_transportation_emissions(car_miles, car_mpg, bus_miles, train_miles, flights_per_year)
total_energy = get_energy_emissions(electricity_kwh, natural_gas_therms, fuel_oil_gallons, propane_gallons)
total_diet = get_diet_emissions(diet_choice)
total_waste = get_waste_emissions(recycle_paper, recycle_plastic, recycle_glass, recycle_metal)

total_emissions = total_transport + total_energy + total_diet + total_waste
total_emissions_metric_tons = total_emissions * 0.000453592

# National average comparison (approximate)
national_average = 16000 # lbs CO2 per person per year
below_average = total_emissions < national_average

return render_template('results.html',
total_transport=total_transport,
total_energy=total_energy,
total_diet=total_diet,
total_waste=total_waste,
total_emissions=total_emissions,
total_emissions_metric_tons=total_emissions_metric_tons,
below_average=below_average)

def get_transportation_emissions(car_miles, car_mpg, bus_miles, train_miles, flights_per_year):
# Emission factors
gas_emission_factor = 19.6 # lbs CO2 per gallon of gasoline
bus_emission_factor = 0.65 # lbs CO2 per passenger mile
train_emission_factor = 0.41 # lbs CO2 per passenger mile
flight_emission_factor = 1100 # lbs CO2 per 4-hour flight

# Calculations
annual_car_miles = car_miles * 52
annual_gallons = annual_car_miles / car_mpg if car_mpg != 0 else 0
car_emissions = annual_gallons * gas_emission_factor

bus_emissions = bus_miles * 52 * bus_emission_factor
train_emissions = train_miles * 52 * train_emission_factor
flight_emissions = flights_per_year * flight_emission_factor

total_transportation_emissions = car_emissions + bus_emissions + train_emissions + flight_emissions

return total_transportation_emissions

def get_energy_emissions(electricity_kwh, natural_gas_therms, fuel_oil_gallons, propane_gallons):
# Emission factors
electricity_emission_factor = 1.22 # lbs CO2 per kWh
natural_gas_emission_factor = 11.7 # lbs CO2 per therm
fuel_oil_emission_factor = 22.4 # lbs CO2 per gallon
propane_emission_factor = 12.7 # lbs CO2 per gallon

# Calculations
annual_electricity_emissions = electricity_kwh * 12 * electricity_emission_factor
annual_natural_gas_emissions = natural_gas_therms * 12 * natural_gas_emission_factor
annual_fuel_oil_emissions = fuel_oil_gallons * 12 * fuel_oil_emission_factor
annual_propane_emissions = propane_gallons * 12 * propane_emission_factor

total_energy_emissions = (annual_electricity_emissions + annual_natural_gas_emissions +
annual_fuel_oil_emissions + annual_propane_emissions)

return total_energy_emissions

def get_diet_emissions(diet_choice):
# Emission factors (lbs CO2 per year)
diet_emissions_dict = {
'1': 3300, # Meat-heavy diet
'2': 2800, # Average diet
'3': 1500, # Vegetarian
'4': 1000 # Vegan
}
diet_emissions = diet_emissions_dict.get(diet_choice, 2800)
return diet_emissions

def get_waste_emissions(recycle_paper, recycle_plastic, recycle_glass, recycle_metal):
# Baseline emissions
waste_emissions = 692 # lbs CO2 per person per year

# Reductions
if recycle_paper.lower() == 'yes':
waste_emissions -= 79
if recycle_plastic.lower() == 'yes':
waste_emissions -= 35
if recycle_glass.lower() == 'yes':
waste_emissions -= 25
if recycle_metal.lower() == 'yes':
waste_emissions -= 46

return waste_emissions

if __name__ == '__main__':
app.run(debug=True)
2 changes: 2 additions & 0 deletions C/Carbon-Footprint-Calculator/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask==2.3.2
Werkzeug==3.0.4
19 changes: 19 additions & 0 deletions C/Carbon-Footprint-Calculator/static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
body {
background-color: #f8f9fa;
}

h1 {
margin-bottom: 30px;
}

.form-label {
font-weight: bold;
}

.btn-primary {
width: 100%;
}

.mt-4 {
margin-top: 2rem;
}
18 changes: 18 additions & 0 deletions C/Carbon-Footprint-Calculator/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Carbon Footprint Calculator</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<!-- Bootstrap CDN for styling -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
<div class="container my-5">
{% block content %}{% endblock %}
</div>
</body>

</html>
Loading

0 comments on commit 92e703d

Please sign in to comment.