dindizz commited on
Commit
52dc925
·
verified ·
1 Parent(s): a14d8c8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import requests
3
+ import gradio as gr
4
+ import os
5
+
6
+ # Constants
7
+ API_URL = 'https://api.electricitymap.org/v3/power-breakdown/latest'
8
+ API_TOKEN = os.getenv('ELECTRICITYMAP_API_TOKEN')
9
+
10
+ # Define the data for Shell offices
11
+ data = {
12
+ "Office": ["Shell Centre", "Shell House", "Shell Technology Center", "Shell India Markets Pvt Ltd",
13
+ "Shell Australia", "Shell Canada", "Shell Argentina", "Shell Nigeria", "Shell China",
14
+ "Shell Russia", "Shell Germany", "Shell Netherlands", "Shell South Africa", "Shell Mexico",
15
+ "Shell Brazil"],
16
+ "Location": ["London, United Kingdom", "Singapore", "Houston, Texas, USA", "Bengaluru, India",
17
+ "Perth, Australia", "Calgary, Alberta, Canada", "Buenos Aires, Argentina",
18
+ "Lagos, Nigeria", "Beijing, China", "Moscow, Russia", "Hamburg, Germany",
19
+ "The Hague, Netherlands", "Cape Town, South Africa", "Mexico City, Mexico",
20
+ "Rio de Janeiro, Brazil"],
21
+ "Latitude": [51.5074, 1.3521, 29.7604, 12.9716, -31.9505, 51.0447, -34.6037, 6.5244,
22
+ 39.9042, 55.7558, 53.5511, 52.0705, -33.9249, 19.4326, -22.9068],
23
+ "Longitude": [-0.1278, 103.8198, -95.3698, 77.5946, 115.8605, -114.0719, -58.3816,
24
+ 3.3792, 116.4074, 37.6173, 9.9937, 4.3007, 18.4241, -99.1332, -43.1729]
25
+ }
26
+
27
+ df = pd.DataFrame(data)
28
+
29
+ # Function to make API request
30
+ def get_power_breakdown(lat, lon):
31
+ headers = {'auth-token': API_TOKEN}
32
+ params = {'lat': lat, 'lon': lon}
33
+ response = requests.get(API_URL, headers=headers, params=params)
34
+ if response.status_code == 200:
35
+ return response.json().get('fossilFreePercentage', 'N/A')
36
+ else:
37
+ return 'Error'
38
+
39
+ # Define the function to fetch fossil-free percentage for all Shell offices
40
+ def fetch_fossil_free_percentages():
41
+ results = []
42
+ for index, row in df.iterrows():
43
+ lat, lon = row["Latitude"], row["Longitude"]
44
+ fossil_free_percentage = get_power_breakdown(lat, lon)
45
+ results.append({
46
+ "Office": row["Office"],
47
+ "Location": row["Location"],
48
+ "Latitude": row["Latitude"],
49
+ "Longitude": row["Longitude"],
50
+ "Fossil-Free Percentage": fossil_free_percentage
51
+ })
52
+ return pd.DataFrame(results)
53
+
54
+ # Define Gradio interface
55
+ def show_fossil_free_table():
56
+ result_df = fetch_fossil_free_percentages()
57
+ return result_df
58
+
59
+ gr_interface = gr.Interface(fn=show_fossil_free_table,
60
+ inputs=None,
61
+ outputs="dataframe",
62
+ title="Fossil-Free Percentage for Shell Offices Globally",
63
+ description="Click the button below to see the latest fossil-free energy usage percentages for Shell offices globally.")
64
+
65
+ gr_interface.launch()