Skip to content

Commit

Permalink
Move figure functions into a separate python file (#57)
Browse files Browse the repository at this point in the history
* Move figure functions to new file

* Add docstring
  • Loading branch information
tsmbland authored Nov 2, 2023
1 parent d4b1457 commit 502371c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 111 deletions.
112 changes: 112 additions & 0 deletions app/figures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""Functions for generating plotly express figures."""
import pandas as pd
import plotly.express as px # type: ignore


def generate_gen_split_fig(df: pd.DataFrame) -> px.pie:
"""Creates Plotly figure for Generation Split graph.
Args:
df: Opal data DataFrame
Returns:
Plotly express figure
"""
if len(df.columns) == 1:
gen_split_fig = px.pie()
else:
gen_split_df = df.iloc[-1, 13:23]

gen_split_fig = px.pie(
names=[
"Battery Generation",
"Interconnector Power",
"Offshore Wind Generation",
"Onshore Wind Generation",
"Other Generation",
"Pump Generation",
"Pv Generation",
"Nuclear Generation",
"Hydro Generation",
"Gas Generation",
],
values=gen_split_df,
).update_layout(title_text=df.iloc[-1]["Time"])
return gen_split_fig


def generate_total_gen_fig(df: pd.DataFrame) -> px.line:
"""Creates Plotly figure for Total Generation graph.
Args:
df: Opal data DataFrame
Returns:
Plotly express figure
"""
if len(df.columns) == 1:
total_gen_fig = px.line()
else:
total_gen_fig = px.line(
df,
x="Time",
y=[
"Total Generation",
"Battery Generation",
"Interconnector Power",
"Offshore Wind Generation",
"Onshore Wind Generation",
"Other Generation",
"Pump Generation",
"Pv Generation",
"Nuclear Generation",
"Hydro Generation",
"Gas Generation",
],
).update_layout(yaxis_title="GW")
return total_gen_fig


def generate_total_dem_fig(df: pd.DataFrame) -> px.line:
"""Creates Plotly figure for Total Demand graph.
Args:
df: Opal data DataFrame
Returns:
Plotly express figure
"""
if len(df.columns) == 1:
total_dem_fig = px.line()
else:
total_dem_fig = px.line(
df,
x="Time",
y=[
"Total Demand",
],
).update_layout(yaxis_title="GW")
return total_dem_fig


def generate_system_freq_fig(df: pd.DataFrame) -> px.line:
"""Creates Plotly figure for System Frequency graph.
Args:
df: Opal data DataFrame
Returns:
Plotly express figure
"""
if len(df.columns) == 1:
system_freq_fig = px.line()
else:
system_freq_fig = px.line(
df,
x="Time",
y=[
"Total Generation",
"Total Demand",
],
).update_layout(yaxis_title="GW")
return system_freq_fig
117 changes: 6 additions & 111 deletions app/pages/supplydemand.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,128 +3,23 @@

import dash # type: ignore
import pandas as pd
import plotly.express as px # type: ignore
from dash import Input, Output, callback, dcc, html # type: ignore
from dash.exceptions import PreventUpdate # type: ignore

from .. import datahub_api as datahub
from ..figures import (
generate_gen_split_fig,
generate_system_freq_fig,
generate_total_dem_fig,
generate_total_gen_fig,
)

dash.register_page(__name__)

##################
interval = 7000
##################


def generate_gen_split_fig(df: pd.DataFrame) -> px.pie:
"""Creates Plotly figure for Generation Split graph.
Args:
df: Opal data DataFrame
Returns:
Plotly express figure
"""
if len(df.columns) == 1:
gen_split_fig = px.pie()
else:
gen_split_df = df.iloc[-1, 13:23]

gen_split_fig = px.pie(
names=[
"Battery Generation",
"Interconnector Power",
"Offshore Wind Generation",
"Onshore Wind Generation",
"Other Generation",
"Pump Generation",
"Pv Generation",
"Nuclear Generation",
"Hydro Generation",
"Gas Generation",
],
values=gen_split_df,
).update_layout(title_text=df.iloc[-1]["Time"])
return gen_split_fig


def generate_total_gen_fig(df: pd.DataFrame) -> px.line:
"""Creates Plotly figure for Total Generation graph.
Args:
df: Opal data DataFrame
Returns:
Plotly express figure
"""
if len(df.columns) == 1:
total_gen_fig = px.line()
else:
total_gen_fig = px.line(
df,
x="Time",
y=[
"Total Generation",
"Battery Generation",
"Interconnector Power",
"Offshore Wind Generation",
"Onshore Wind Generation",
"Other Generation",
"Pump Generation",
"Pv Generation",
"Nuclear Generation",
"Hydro Generation",
"Gas Generation",
],
).update_layout(yaxis_title="GW")
return total_gen_fig


def generate_total_dem_fig(df: pd.DataFrame) -> px.line:
"""Creates Plotly figure for Total Demand graph.
Args:
df: Opal data DataFrame
Returns:
Plotly express figure
"""
if len(df.columns) == 1:
total_dem_fig = px.line()
else:
total_dem_fig = px.line(
df,
x="Time",
y=[
"Total Demand",
],
).update_layout(yaxis_title="GW")
return total_dem_fig


def generate_system_freq_fig(df: pd.DataFrame) -> px.line:
"""Creates Plotly figure for System Frequency graph.
Args:
df: Opal data DataFrame
Returns:
Plotly express figure
"""
if len(df.columns) == 1:
system_freq_fig = px.line()
else:
system_freq_fig = px.line(
df,
x="Time",
y=[
"Total Generation",
"Total Demand",
],
).update_layout(yaxis_title="GW")
return system_freq_fig


df = pd.DataFrame({"Col": [0]})

gen_split_fig = generate_gen_split_fig(df)
Expand Down

0 comments on commit 502371c

Please sign in to comment.