CodeCarbon Copilot Instructions¶
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.
Repository Structure¶
Main Components¶
- π¦ 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
Key Directories¶
- 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
Development Setup¶
Prerequisites¶
- 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)
Quick Start¶
-
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
Testing Strategy¶
Core Package Tests¶
- 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
API Tests¶
- 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
Manual Testing¶
- 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
Common Development Patterns¶
Adding New Features¶
- 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
API Development¶
- 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/
Dashboard Development¶
- 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
Environment Management¶
UV Task Management¶
# 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
Configuration Files¶
- pyproject.toml: Main project configuration
- .codecarbon.config: Runtime configuration for API connection
- docker-compose.yml: Local development with PostgreSQL
Documentation and Help¶
Key Documentation Files¶
- 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
VS Code Debugging¶
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
Getting Help¶
Architecture Overview¶
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β 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.
Tips for Effective Development¶
- 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! π±