dindizz's picture
Create app.py
dc31b5c verified
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
data = {
"State/UT": ["Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa", "Gujarat",
"Haryana", "Himachal Pradesh", "Jharkhand", "Karnataka", "Kerala", "Madhya Pradesh",
"Maharashtra", "Manipur", "Meghalaya", "Mizoram", "Nagaland", "Odisha", "Punjab",
"Rajasthan", "Sikkim", "Tamil Nadu", "Telangana", "Tripura", "Uttar Pradesh",
"Uttarakhand", "West Bengal", "Andaman and Nicobar Islands", "Chandigarh",
"Dadra and Nagar Haveli and Daman and Diu", "Lakshadweep", "Delhi", "Puducherry", "Ladakh"],
"Capital": ["Amaravati", "Itanagar", "Dispur", "Patna", "Raipur", "Panaji", "Gandhinagar", "Chandigarh",
"Shimla", "Ranchi", "Bengaluru", "Thiruvananthapuram", "Bhopal", "Mumbai", "Imphal", "Shillong",
"Aizawl", "Kohima", "Bhubaneswar", "Chandigarh", "Jaipur", "Gangtok", "Chennai", "Hyderabad",
"Agartala", "Lucknow", "Dehradun", "Kolkata", "Port Blair", "Chandigarh", "Daman",
"Kavaratti", "New Delhi", "Puducherry", "Leh"],
"Latitude": [16.5411, 27.0844, 26.1445, 25.5941, 21.2514, 15.4909, 23.2156, 30.7333, 31.1048, 23.3441,
12.9716, 8.5241, 23.2599, 19.076, 24.817, 25.5788, 23.1645, 25.6747, 20.2961, 30.7333,
26.9124, 27.3389, 13.0827, 17.385, 23.8315, 26.8467, 30.3165, 22.5726, 11.6234, 30.7333,
20.3974, 10.5667, 28.6139, 11.9416, 34.1526],
"Longitude": [80.515, 93.6053, 91.7362, 85.1376, 81.6296, 73.8278, 72.6369, 76.7794, 77.1734, 85.3096,
77.5946, 76.9366, 77.4126, 72.8777, 93.9368, 91.8933, 92.9376, 94.1103, 85.8245, 76.7794,
75.7873, 88.6065, 80.2707, 78.4867, 91.2868, 80.9462, 78.0322, 88.3639, 92.7265, 76.7794,
72.8328, 72.6369, 77.209, 79.8083, 77.577]
}
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 states
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({
"State/UT": row["State/UT"],
"Capital": row["Capital"],
"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 State/UT",
description="Click the button below to see the latest fossil-free energy usage percentages for all states and union territories.")
gr_interface.launch()