Build a Flask-powered web application that displays air quality data from the OpenAQ API v3. The dashboard will store data in a SQLite database and filter for potentially risky PM2.5 levels.
# create a virtual environment
python -m venv venv
# For Linux/Mac: activate the virtual environment
source venv/bin/activate
# For Windows: activate the virtual environment
venv/Scripts/activate
# Install dependencies
pip install -r requirements.txt- Visit OpenAQ API Key page
- Sign up for a free account
- Generate an API key
- Create a
.envfile in the root directory and add the API key:
OPEN_AQ_API_KEY=your_api_key_here- Use
aq_dashboard.pyas your starting point - Follow the step-by-step instructions in the assignment
- Complete all TODO items
your-project/
├── aq_dashboard.py # Your completed solution
├── openaq.py # OpenAQ API wrapper (download from course)
├── test_aq_dashboard.py # Test file
├── requirements.txt # Dependencies
└── db.sqlite3 # SQLite database (created automatically)- Basic Flask application
- Database configuration
- API initialization
- Three-step data retrieval process:
- Get locations:
api.locations() - Get sensors:
api.sensors_by_location_id(location_id) - Get measurements:
api.measurements_by_sensor_id(sensor_id)
- Get locations:
- Handle API rate limits and data availability
- Implement fallback sample data
- Create
Recordmodel with id, datetime, value fields - Implement
/refreshroute to pull and store data - Add proper
__repr__method
- Filter records with
value >= 18(potentially risky PM2.5 levels) - Display filtered results on main page
- API Key Required: Authentication via
X-API-Keyheader - No Direct Measurements: Must use locations → sensors → measurements flow
- Rate Limiting: Stricter limits, 429 errors common
- Data Availability: Many sensors lack recent measurements
# Run tests
python -m pytest test_aq_dashboard.py -v
# Expected output: 4 tests passing# Method 1: Direct Python execution
python aq_dashboard.py
# Method 2: Flask CLI
FLASK_APP=aq_dashboard.py flask runVisit http://localhost:5000 to see your dashboard!
- ModuleNotFoundError: Install missing dependencies
- API Key Errors: Ensure you have a valid OpenAQ API key
- Rate Limiting: API v3 has strict limits - use fallback data
- No Data: Many sensors don't have recent measurements
- Enable Flask debug mode:
app.run(debug=True) - Check API responses with print statements
- Use sample data when API is unavailable
- Test database operations in Flask shell
- Flask app runs without errors
- API key authentication works
- Database stores air quality data
- Dashboard filters for risky PM2.5 levels (≥18 μg/m³)
- All tests pass
- Handles API rate limits gracefully
- Add HTML templates for better UI
- Implement data visualization
- Add location-specific filtering
- Deploy to Render.com
- Add data science analysis (averages, trends)
Good luck! 🚀