From 502371cdc632462f50ad359bb80d0be12e37dd01 Mon Sep 17 00:00:00 2001 From: Tom Bland Date: Thu, 2 Nov 2023 12:50:09 +0000 Subject: [PATCH] Move figure functions into a separate python file (#57) * Move figure functions to new file * Add docstring --- app/figures.py | 112 ++++++++++++++++++++++++++++++++++++ app/pages/supplydemand.py | 117 ++------------------------------------ 2 files changed, 118 insertions(+), 111 deletions(-) create mode 100644 app/figures.py diff --git a/app/figures.py b/app/figures.py new file mode 100644 index 0000000..6a3d720 --- /dev/null +++ b/app/figures.py @@ -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 diff --git a/app/pages/supplydemand.py b/app/pages/supplydemand.py index eda9d33..bd40d54 100644 --- a/app/pages/supplydemand.py +++ b/app/pages/supplydemand.py @@ -3,11 +3,16 @@ 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__) @@ -15,116 +20,6 @@ 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)