File size: 2,950 Bytes
d1f82d4
4ed4318
 
 
 
d1f82d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ed4318
d1f82d4
 
 
 
 
 
 
4ed4318
d1f82d4
4ed4318
d1f82d4
4ed4318
d1f82d4
 
4ed4318
d1f82d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ed4318
d1f82d4
 
 
 
349aad5
4ed4318
d1f82d4
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
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()