This repository contains multiple components for tracking CO2 emissions from computing.
CodeCarbon works by measuring electricity consumption on the computer and convert it to CO2 emissions using electricity carbon intensity from the grid where the computer is located.
It's a mono-repo for a Python package, a centralized API and an online dashboard.
The package also have a command line to monitor computer's emissions without wrinting Python code.
Here's what you need to know to navigate and contribute effectively.
-
📦 codecarbon/ - Core Python package
- Main library for tracking carbon emissions
- CLI tools and utilities
- Core emission tracking functionality
-
🔌 carbonserver/ - API Backend
- FastAPI-based REST API
- PostgreSQL database integration
- User management and data persistence
- Run with:
uv run task dockeroruv run task dashboard
-
📊 dashboard/viz - Python Dashboard (Dash-based)
- Interactive dashboard using Dash/Plotly
- Visualizes emission data from CSV files
- Run with:
uv run task carbonboard
-
🌐 webapp/ - Next.js Web Dashboard
- Modern React/Next.js web application
- Connects to the API backend
- Run with:
cd webapp && pnpm dev
- tests/ - Unit tests for the core package
- docs/ - Zensical documentation (build with
uv run task docs) - examples/ - Usage examples and demos
- deploy/ - Deployment configurations and scripts
- Python 3.7+ (preferably 3.11+)
- uv (Python project manager):
curl -LsSf https://astral.sh/uv/install.sh | sh - Node.js & pnpm (for webapp):
npm install -g pnpmorcurl -fsSL https://get.pnpm.io/install.sh | sh - - Docker (for API development)
-
Core Package Development:
# Run tests uv run task test-package # Run specific test uv run pytest tests/test_emissions_tracker.py # Lint and format uv run task lint uv run task format
-
API Development:
# Start API with Docker (recommended) uv run task docker # Or run locally (requires PostgreSQL setup) uv run task dashboard # Test API uv run task test-api-unit uv run task test-api-integ
-
Dashboard Development:
# Python dashboard uv run task carbonboard # Next.js webapp cd webapp pnpm install pnpm dev
- Unit tests:
tests/test_*.py- Test individual components - Integration tests: Include real hardware testing with
CODECARBON_ALLOW_MULTIPLE_RUNS=True - Run specific tests:
uv run pytest tests/test_specific.py
- Unit tests:
uv run task test-api-unit- Test business logic - Integration tests:
uv run task test-api-integ- Test with database - See:
carbonserver/tests/TESTING.mdfor detailed testing guide
- Stress testing: Use
7z bfor CPU or gpu-burn for GPU testing - CLI testing: Use examples in
examples/directory - Monitor with:
nvidia-smifor GPU metrics comparison
- Check existing tests in
tests/for similar functionality - Add unit tests first (test-driven development)
- Update documentation if public interface changes
- Follow coding style: Use
uv run task formatanduv run task lint
- Follow FastAPI patterns - see routers in
carbonserver/carbonserver/api/routers/ - Use dependency injection - see
carbonserver/container.py - Database migrations - use Alembic (see
carbonserver/carbonserver/database/alembic/) - Test with Postman - collection in
carbonserver/tests/postman/
- Python Dashboard: Uses Dash + Plotly, see
codecarbon/viz - Next.js Dashboard: Modern React components in
webapp/src/ - Connect to API: Set
CODECARBON_API_URL=http://localhost:8008for local development
# List all available tasks
uv run task -l
# Main tasks:
# - test-package: Core package testing
# - lint: Code linting and style checks
# - format: Code formatting
# - test-api-unit: API unit tests
# - test-api-integ: API integration tests
# - dashboard: Run API locally
# - docker: Start API with Docker
# - carbonboard: Python dashboard
# - docs: Build documentation- pyproject.toml: Main project configuration
- .codecarbon.config: Runtime configuration for API connection
- docker-compose.yml: Local development with PostgreSQL
- CONTRIBUTING.md: Detailed contribution guidelines and setup
- README.md: Project overview and quickstart
- carbonserver/README.md: API architecture and database schema
- webapp/README.md: Next.js dashboard setup
- carbonserver/tests/TESTING.md: Comprehensive testing guide
The repository includes VS Code launch configurations in docs/how-to/contributing.md for:
- Debugging current Python file
- Running pytest with debugger
- Testing codecarbon CLI monitor
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ codecarbon │ │ carbonserver │ │ dashboards │
│ (package) │───▶│ (API) │◀───│ (2 versions) │
│ │ │ │ │ │
│ • CLI tools │ │ • FastAPI │ │ • Dash (Python) │
│ • Tracking core │ │ • PostgreSQL │ │ • Next.js (Web) │
│ • Data output │ │ • Authentication│ │ • Visualization │
└─────────────────┘ └─────────────────┘ └─────────────────┘
The package can work standalone (offline mode) or connect to the API for cloud features and dashboard visualization.
- Start with tests: Run existing tests first to understand current state
- Use examples: Check
examples/directory for usage patterns - Environment isolation: Use uv for dependency management and virtual environments
- Incremental development: Test frequently with
uv run task test-package - Check CI: Ensure your changes pass the same checks as GitHub Actions
- Read architecture docs: Understand the emission calculation methodology in docs/
Remember: CodeCarbon is about measuring computing emissions, in the goal of helping to reduce them. Every contribution helps make computing more sustainable! 🌱