-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.py
More file actions
167 lines (139 loc) Β· 5.91 KB
/
quickstart.py
File metadata and controls
167 lines (139 loc) Β· 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
"""
ZeroCarbon API - Interactive Quickstart
Run: python quickstart.py
"""
import requests
import json
from typing import Dict, Any
import os
# Auto-detect environment
API_BASE = os.environ.get('API_BASE', 'https://zerocarbon.codes/api')
# Colors for terminal output
class Colors:
RESET = '\033[0m'
GREEN = '\033[32m'
BLUE = '\033[34m'
YELLOW = '\033[33m'
CYAN = '\033[36m'
BOLD = '\033[1m'
def log(message: str, color: str = 'RESET'):
"""Print colored message to console"""
color_code = getattr(Colors, color, Colors.RESET)
print(f"{color_code}{message}{Colors.RESET}")
def api_request(endpoint: str, data: Dict[str, Any]) -> Dict[str, Any]:
"""Make API request to ZeroCarbon"""
try:
response = requests.post(
f"{API_BASE}{endpoint}",
json=data,
headers={'Content-Type': 'application/json'},
timeout=10
)
return response.json()
except Exception as e:
return {"success": False, "error": str(e)}
def demo_electricity():
"""Demo: Calculate electricity emissions"""
log('\nπ Demo 1: Electricity Emissions', 'CYAN')
log('Calculating emissions for 1,000 kWh of electricity in the US...\n')
result = api_request('/v1/emissions/calculate', {
'source': 'electricity',
'value': 1000,
'unit': 'kWh',
'country': 'US'
})
if result.get('success'):
data = result.get('data', {})
log(f"β
Emissions: {data.get('emissions_kg_co2e')} kg CO2e", 'GREEN')
log(f" Equivalent to: {data.get('emissions_kg_co2e', 0) / 411:.1f} gallons of gasoline", 'BLUE')
log(f" Emission factor: {data.get('emission_factor')} kg CO2e per kWh", 'BLUE')
def demo_flight():
"""Demo: Calculate flight emissions"""
log('\nβοΈ Demo 2: Flight Emissions', 'CYAN')
log('Calculating emissions for SFO β JFK flight...\n')
result = api_request('/v1/calculate/flight', {
'from': 'SFO',
'to': 'JFK',
'passengers': 1,
'class': 'economy'
})
if result.get('success'):
data = result.get('data', {})
log(f"β
Emissions: {data.get('emissions_kg_co2e')} kg CO2e", 'GREEN')
log(f" Distance: {data.get('distance_km')} km", 'BLUE')
log(f" Per passenger: {data.get('emissions_per_passenger_kg_co2e')} kg CO2e", 'BLUE')
def demo_vehicle():
"""Demo: Calculate vehicle emissions"""
log('\nπ Demo 3: Vehicle Emissions', 'CYAN')
log('Calculating emissions for 100 miles of driving...\n')
result = api_request('/v1/calculate/fuel', {
'fuel_type': 'gasoline',
'distance': 100,
'unit': 'miles',
'vehicle_type': 'car'
})
if result.get('success'):
data = result.get('data', {})
log(f"β
Emissions: {data.get('emissions_kg_co2e')} kg CO2e", 'GREEN')
log(f" Fuel consumed: {data.get('fuel_consumed_liters')} liters", 'BLUE')
log(f" Emission factor: {data.get('emission_factor')} kg CO2e per liter", 'BLUE')
def interactive_mode():
"""Interactive custom calculation"""
log('\nπ Custom Calculation', 'CYAN')
source = input('Enter emission source (electricity/gasoline/diesel/natural_gas): ').strip()
value = float(input('Enter value: '))
unit = input('Enter unit (kWh/liters/m3): ').strip()
log('\nCalculating...\n')
result = api_request('/v1/emissions/calculate', {
'source': source,
'value': value,
'unit': unit,
'country': 'US'
})
if result.get('success'):
data = result.get('data', {})
log(f"β
Emissions: {data.get('emissions_kg_co2e')} kg CO2e", 'GREEN')
log(f" Source: {data.get('source')}", 'BLUE')
log(f" Scope: {data.get('scope')}", 'BLUE')
else:
log(f"β Error: {result.get('error', 'Unknown error')}", 'YELLOW')
def main():
"""Main function"""
log('\nβββββββββββββββββββββββββββββββββββββββββ', 'BOLD')
log('β ZeroCarbon API - Quick Start Demo β', 'BOLD')
log('βββββββββββββββββββββββββββββββββββββββββ\n', 'BOLD')
log('Welcome! This demo shows how easy it is to calculate carbon emissions.', 'BLUE')
log('No API key needed for testing.\n', 'BLUE')
try:
# Run pre-built demos
demo_electricity()
demo_flight()
demo_vehicle()
# Offer interactive mode
log('\n' + 'β' * 60, 'BLUE')
answer = input('\nWant to try a custom calculation? (y/n): ')
if answer.lower() == 'y':
interactive_mode()
# Show next steps
log('\n' + 'β' * 60, 'GREEN')
log('\nπ That\'s it! You just calculated carbon emissions via API.', 'GREEN')
log('\nπ Next Steps:', 'CYAN')
log(' 1. Get free API key: https://app.zerocarbon.codes/signup', 'BLUE')
log(' 2. Read docs: https://docs.zerocarbon.codes', 'BLUE')
log(' 3. View examples: https://github.com/zerocarbon/examples', 'BLUE')
log(' 4. Join Discord: https://discord.gg/zerocarbon', 'BLUE')
log('\nπ‘ Install SDK: pip install zerocarbon', 'YELLOW')
log('\n' + 'β' * 60 + '\n', 'GREEN')
except Exception as e:
log(f'\nβ Error: {str(e)}', 'YELLOW')
log('\nFalling back to local demo mode...', 'BLUE')
# Fallback with mock data
log('\nπ Electricity Demo (Mock Data)', 'CYAN')
log('β
1,000 kWh β 386.5 kg CO2e', 'GREEN')
log('\nβοΈ Flight Demo (Mock Data)', 'CYAN')
log('β
SFO β JFK β 532 kg CO2e per passenger', 'GREEN')
log('\nπ Vehicle Demo (Mock Data)', 'CYAN')
log('β
100 miles β 38.2 kg CO2e', 'GREEN')
if __name__ == '__main__':
main()