Py.Cafe

gerasimoskounadis/

nii-energy-production-analysis

NII Energy Production Analysis

DocsPricing
  • app.py
  • requirements.txt
app.py
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
68
69
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)