import pandas as pd
import plotly.graph_objects as go
from dash import Dash, html, dcc, callback, Output, Input
from urllib.error import HTTPError
base_url = 'https://raw.githubusercontent.com/gerrykou/niien/refs/heads/main/public_data/en/csv'
columns_to_keep = ['ELECTRIC SYSTEM NII', 'RES UNITS ENERGY GENERATION (MWh)', "THERMAL UNITS ENERGY GENERATION (MWh)", "year", "month"]
def read_df_from_github(year, month):
url = f'{base_url}/{year}/{year}-{str(month).zfill(2)}.csv'
print(url)
try:
df = pd.read_csv(url, index_col=None)
df["year"] = year
df["month"] = month
df_clean = df
# df_clean = df[columns_to_keep]
except HTTPError as e:
print(f"HTTP Error {e.code}: {e.reason}")
return
return df_clean
years = [2022, 2023, 2024]
months = [i for i in range(1, 13)]
all_df = []
for year in years:
for month in months:
df = read_df_from_github(year, month)
all_df.append(df)
nii_df = pd.concat(all_df)
nii_df["year-month"] = nii_df["year"].astype(str) + '-' + nii_df['month'].astype(str).str.zfill(2)
app = Dash()
app.layout = [
html.H1(children='NII Energy Production', style={'textAlign':'center'}),
dcc.Dropdown(nii_df["ELECTRIC SYSTEM NII"].unique(), 'AMORGOS', id='dropdown-selection'),
dcc.Graph(id='graph-content')
]
@callback(
Output('graph-content', 'figure'),
Input('dropdown-selection', 'value')
)
def update_graph(value):
dff = nii_df[nii_df["ELECTRIC SYSTEM NII"]==value]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=dff["year-month"],
y=dff["THERMAL UNITS ENERGY GENERATION (MWh)"],
mode='lines', name='Thermal Units'))
fig.add_trace(go.Scatter(
x=dff["year-month"],
y=dff["RES UNITS ENERGY GENERATION (MWh)"],
mode='lines', name='RES Units'))
fig.update_layout(
title="NII Energy Generation",
xaxis_title = "Month-Year",
yaxis_title = "Energy Generation (MWh)")
return fig
if __name__ == '__main__':
app.run(debug=True)