Spaces:
Sleeping
Sleeping
import pandas as pd | |
import requests | |
import gradio as gr | |
import os | |
# Constants | |
API_URL = 'https://api.electricitymap.org/v3/power-breakdown/latest' | |
API_TOKEN = os.getenv('ELECTRICITYMAP_API_TOKEN') | |
# Define the data for company headquarters | |
data = { | |
"Company": ["Apple", "Microsoft", "NVIDIA", "Alphabet (Google)", "Amazon", "Saudi Aramco", | |
"Meta Platforms (Facebook)", "TSMC", "Berkshire Hathaway", "Eli Lilly", | |
"Tesla", "Broadcom", "Novo Nordisk", "JPMorgan Chase", "Walmart"], | |
"Location": ["Cupertino, California, USA", "Redmond, Washington, USA", "Santa Clara, California, USA", | |
"Mountain View, California, USA", "Seattle, Washington, USA", "Dhahran, Eastern Province, Saudi Arabia", | |
"Menlo Park, California, USA", "Hsinchu, Taiwan", "Omaha, Nebraska, USA", "Indianapolis, Indiana, USA", | |
"Palo Alto, California, USA", "San Jose, California, USA", "Bagsværd, Denmark", | |
"New York City, New York, USA", "Bentonville, Arkansas, USA"], | |
"Latitude": [37.3349, 47.6424, 37.3706, 37.4219, 47.6062, 26.3032, | |
37.4848, 24.7851, 41.2565, 39.7684, 37.3947, 37.3382, | |
55.7500, 40.7128, 36.3729], | |
"Longitude": [-122.0090, -122.1362, -121.9669, -122.0840, -122.3321, 50.1503, | |
-122.1484, 121.0177, -95.9345, -86.1581, -122.1503, -121.8863, | |
12.4500, -74.0060, -94.2088] | |
} | |
df = pd.DataFrame(data) | |
# Function to make API request | |
def get_power_breakdown(lat, lon): | |
headers = {'auth-token': API_TOKEN} | |
params = {'lat': lat, 'lon': lon} | |
response = requests.get(API_URL, headers=headers, params=params) | |
if response.status_code == 200: | |
return response.json().get('fossilFreePercentage', 'N/A') | |
else: | |
return 'Error' | |
# Define the function to fetch fossil-free percentage for all companies | |
def fetch_fossil_free_percentages(): | |
results = [] | |
for index, row in df.iterrows(): | |
lat, lon = row["Latitude"], row["Longitude"] | |
fossil_free_percentage = get_power_breakdown(lat, lon) | |
results.append({ | |
"Company": row["Company"], | |
"Location": row["Location"], | |
"Latitude": row["Latitude"], | |
"Longitude": row["Longitude"], | |
"Fossil-Free Percentage": fossil_free_percentage | |
}) | |
return pd.DataFrame(results) | |
# Define Gradio interface | |
def show_fossil_free_table(): | |
result_df = fetch_fossil_free_percentages() | |
return result_df | |
gr_interface = gr.Interface(fn=show_fossil_free_table, | |
inputs=None, | |
outputs="dataframe", | |
title="Fossil-Free Percentage by Company Headquarters", | |
description="Click the button below to see the latest fossil-free energy usage percentages for the top market cap company headquarters.") | |
gr_interface.launch() | |