From 48dacc801e8774ce2aa5688211768743317c70e5 Mon Sep 17 00:00:00 2001 From: Riws Date: Tue, 11 Jun 2024 21:12:53 +0200 Subject: [PATCH 1/2] chore: apply black --- app/pages/company/company.py | 261 +++++++++++++++++++---------------- app/viz.py | 43 +++--- 2 files changed, 165 insertions(+), 139 deletions(-) diff --git a/app/pages/company/company.py b/app/pages/company/company.py index 294fdd5..7a14e11 100644 --- a/app/pages/company/company.py +++ b/app/pages/company/company.py @@ -1,6 +1,7 @@ -from taipy.gui import State, Markdown, get_state_id import numpy as np import pandas as pd +from taipy.gui import Markdown, State, get_state_id + from app import algo from app import config as cfg from app.viz import Viz @@ -11,37 +12,34 @@ # Init bindings (used in md file) selected_company = DEFAULT_COMPANY -selector_company:list[str] = [] -selected_year:str = None -selector_year:list[str] = [] -company_sector:str = None -company_upe_name:str = "" +selector_company: list[str] = [] +selected_year: str = None +selector_year: list[str] = [] +company_sector: str = None +company_upe_name: str = "" -df_selected_company:pd.DataFrame = None -df_count_company:pd.DataFrame = None +df_selected_company: pd.DataFrame = None +df_count_company: pd.DataFrame = None # Viz store map[viz_id,viz_dict] # Important for taipy bindings # Use Viz.init on each page with set of viz_id -viz:dict[str,dict] = Viz.init( +viz: dict[str, dict] = Viz.init( ( "company_sector", "company_upe_name", "company_nb_reports", "company_transparency_score", - "fin_transparency_score", "fin_transparency_score_over_time_details", - "fin_key_financials_kpis", "fin_jurisdictions_top_revenue", - "fin_pretax_profit_and_employees_rank", - "fin_pretax_profit_and_profit_per_employee", + "fin_pretax_profit_and_profit_per_employee", ) -) +) + - # Initialize state (Taipy callback function) # Called by main.py/on_init def on_init(state: State): @@ -50,39 +48,41 @@ def on_init(state: State): init_state(state) - # print('COMPANY ON INIT...END') + # print('COMPANY ON INIT...END') def init_state(state: State): with state as s: # Path to image - s.company_image_path = company_image_path - + s.company_image_path = company_image_path + s.selected_company = selected_company # print(f'company state selected_company:{s.selected_company}') - + # Performance: Done once in main.py # s.data = data # print(f'company state data:{s.data.head()}') - - # List companies to populate selector + + # List companies to populate selector s.selector_company = list(np.sort(s.data["mnc"].astype(str).unique())) # print(f'company state selector_company:{s.selector_company}') - + s.viz = viz - + update_full(state) + def update_full(state: State): with state as s: - update_state(s) - update_viz(s) - + update_state(s) + update_viz(s) + + def update_state(state: State): # Create a filtered DataFrame with selected company state.df_selected_company = state.data[state.data["mnc"] == state.selected_company] # print(f'company state df_selected_company:{state.df_selected_company.head()}') - + # List years to populate selector and initialise selected year state.selector_year = state.df_selected_company["year"].unique().astype(str).tolist() # print(f'company state selector_year:{state.selector_year}') @@ -90,129 +90,146 @@ def update_state(state: State): # print(f'company state selected_year:{state.selected_year}') # Calculate number of reports for all companies - state.df_count_company = algo.number_of_tracked_reports_over_time_company(state.df_selected_company) + state.df_count_company = algo.number_of_tracked_reports_over_time_company( + state.df_selected_company + ) # print(f'company state df_count_company:{state.df_count_company.head()}') # Calculate sector, upe_code for selected company state.company_sector = list(state.df_selected_company["sector"].unique())[0] # print(f'company state company_sector:{state.company_sector}') - state.company_upe_name = state.df_selected_company["upe_name"].unique()[0] + state.company_upe_name = state.df_selected_company["upe_name"].unique()[0] # print(f'company state company_upe_name:{state.company_upe_name}') - + def update_viz(state: State): update_viz_company(state) update_viz_year(state) - - + + def update_viz_company(state: State): # print(f'update viz company : {state.selected_company}') - - id ="company_sector" - state.viz[id] = Viz(id=id, - state=state, - data=state.company_sector, - title="Sector" - ).to_state() + + id = "company_sector" + state.viz[id] = Viz( + id=id, state=state, data=state.company_sector, title="Sector" + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - - id ="company_upe_name" - state.viz[id] = Viz(id=id, - state=state, - data=state.company_upe_name, - title="Headquarter" - ).to_state() + + id = "company_upe_name" + state.viz[id] = Viz( + id=id, state=state, data=state.company_upe_name, title="Headquarter" + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - - id="company_nb_reports" - state.viz[id] = Viz(id=id, - state=state, - data = algo.number_of_tracked_reports_company(state.df_selected_company), - title = "Number of reports" - ).to_state() + + id = "company_nb_reports" + state.viz[id] = Viz( + id=id, + state=state, + data=algo.number_of_tracked_reports_company(state.df_selected_company), + title="Number of reports", + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - id="company_transparency_score" - state.viz[id] = Viz(id=id, - state=state, - data=algo.display_transparency_score(state.data, state.selected_company), - title="Transparency Score", - sub_title="average over all reports" - ).to_state() + id = "company_transparency_score" + state.viz[id] = Viz( + id=id, + state=state, + data=algo.display_transparency_score(state.data, state.selected_company), + title="Transparency Score", + sub_title="average over all reports", + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - - # Ex Viz 6 + + # Ex Viz 6 # id="company_transparency_more" - # state.viz[id] = Viz(id=id, - # state=state, + # state.viz[id] = Viz(id=id, + # state=state, # data=state.df_selected_company, # title="More on transparency", # sub_title="" # ).to_state() # # print(f'update viz id:{id} title:{state.viz[id].title}') - - + + def update_viz_year(state: State): # # same order as previous code # print(f'update viz financial : {state.selected_company}') - + # Transparency - id ="fin_transparency_score" - state.viz[id] = Viz(id=id, - state=state, - data=algo.display_transparency_score(state.data, state.selected_company, int(state.selected_year)), - title="Transparency Score", - sub_title=f"selected fiscal year : {state.selected_year}" - ).to_state() + id = "fin_transparency_score" + state.viz[id] = Viz( + id=id, + state=state, + data=algo.display_transparency_score( + state.data, state.selected_company, int(state.selected_year) + ), + title="Transparency Score", + sub_title=f"selected fiscal year : {state.selected_year}", + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - - id ="fin_transparency_score_over_time_details" - state.viz[id] = Viz(id=id, - state=state, - data=algo.display_transparency_score_over_time_details(state.data, state.selected_company), - title="Transparency score over time ", - ).to_state() + + id = "fin_transparency_score_over_time_details" + state.viz[id] = Viz( + id=id, + state=state, + data=algo.display_transparency_score_over_time_details( + state.data, state.selected_company + ), + title="Transparency score over time ", + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - + # Profile - id ="fin_key_financials_kpis" - state.viz[id] = Viz(id=id, - state=state, - data=algo.display_company_key_financials_kpis(state.data, state.selected_company, int(state.selected_year)), - title="Key metrics", - sub_title=f"selected fiscal year : {state.selected_year}" - ).to_state() + id = "fin_key_financials_kpis" + state.viz[id] = Viz( + id=id, + state=state, + data=algo.display_company_key_financials_kpis( + state.data, state.selected_company, int(state.selected_year) + ), + title="Key metrics", + sub_title=f"selected fiscal year : {state.selected_year}", + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - + id = "fin_jurisdictions_top_revenue" - state.viz[id] = Viz(id=id, - state=state, - fig=algo.display_jurisdictions_top_revenue(state.data, state.selected_company, int(state.selected_year)), - title="Distribution of revenues across countries", - sub_title=f"selected fiscal year : {state.selected_year}" - ).to_state() - # print(f'update viz id:{id} title:{state.viz[id].title}') + state.viz[id] = Viz( + id=id, + state=state, + fig=algo.display_jurisdictions_top_revenue( + state.data, state.selected_company, int(state.selected_year) + ), + title="Distribution of revenues across countries", + sub_title=f"selected fiscal year : {state.selected_year}", + ).to_state() + # print(f'update viz id:{id} title:{state.viz[id].title}') # Distribution id = "fin_pretax_profit_and_employees_rank" - state.viz[id] = Viz(id=id, - state=state, - fig=algo.display_pretax_profit_and_employees_rank(state.data, state.selected_company, int(state.selected_year)), - title="% profit and employees by country", - sub_title=f"selected fiscal year : {state.selected_year}" - ).to_state() + state.viz[id] = Viz( + id=id, + state=state, + fig=algo.display_pretax_profit_and_employees_rank( + state.data, state.selected_company, int(state.selected_year) + ), + title="% profit and employees by country", + sub_title=f"selected fiscal year : {state.selected_year}", + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - - + id = "fin_pretax_profit_and_profit_per_employee" - state.viz[id] = Viz(id=id, - state=state, - fig=algo.display_pretax_profit_and_profit_per_employee(state.data, state.selected_company, int(state.selected_year)), - title="% profit and profit / employee by country", - sub_title=f"selected fiscal year : {state.selected_year}" - ).to_state() + state.viz[id] = Viz( + id=id, + state=state, + fig=algo.display_pretax_profit_and_profit_per_employee( + state.data, state.selected_company, int(state.selected_year) + ), + title="% profit and profit / employee by country", + sub_title=f"selected fiscal year : {state.selected_year}", + ).to_state() # print(f'update viz id:{id} title:{state.viz[id].title}') - + # Ex Viz 17 # id = "fin_profit_and_employee_breakdown" # state.viz[id] = Viz(id=id, @@ -222,20 +239,19 @@ def update_viz_year(state: State): # sub_title="domestic vs. havens vs. non havens, selected fiscal year" # ).to_state() ## # print(f'update viz id:{id} title:{state.viz[id].title}') - + # Ex Viz 18 # id = "fin_related_and_unrelated_revenues_breakdown" - # algo_data, algo_fig = algo.display_related_and_unrelated_revenues_breakdown(state.data, state.selected_company, int(state.selected_year)) + # algo_data, algo_fig = algo.display_related_and_unrelated_revenues_breakdown(state.data, state.selected_company, int(state.selected_year)) # state.viz[id] = Viz(id=id, # state=state, # data=algo_data, # fig=algo_fig, # title= "Breakdown of revenue between unrelated and related revenue", - # sub_title=f"domestic vs. havens vs. non havens, selected fiscal year: {state.selected_year}", + # sub_title=f"domestic vs. havens vs. non havens, selected fiscal year: {state.selected_year}", # ).to_state() ## # print(f'update viz id:{id} title:{state.viz[id].title}') - # Ex Viz 19 # id = "fin_tax_haven_used_by_company" # _, algo_data = algo.tax_haven_used_by_company(state.df_selected_company) @@ -243,11 +259,11 @@ def update_viz_year(state: State): # state=state, # data=algo_data, # title="Profits, employees and revenue breakdown by tax haven", - # sub_title=f"selected fiscal year : {state.selected_year}", + # sub_title=f"selected fiscal year : {state.selected_year}", # ).to_state() ## # print(f'update viz id:{id} title:{state.viz[id].title}') - - # Ex Viz 21 + + # Ex Viz 21 # id = "fin_tax_havens_use_evolution" # state.viz[id] = Viz(id=id, # state=state, @@ -256,16 +272,17 @@ def update_viz_year(state: State): # sub_title=f"domestic vs. havens vs. non havens, selected fiscal year: {state.selected_year}", # ).to_state() ## # print(f'update viz id:{id} title:{state.viz[id].title}') - + + # Update data and figures when the selected company changes def on_change_company(state: State): - # print("Chosen company: ", state.selected_company) + # print("Chosen company: ", state.selected_company) update_full(state) # Update data and figures when the selected year changes def on_change_year(state: State): - # print("Chosen year: ", state.selected_year) + # print("Chosen year: ", state.selected_year) update_viz_year(state) diff --git a/app/viz.py b/app/viz.py index e71d62a..c080513 100644 --- a/app/viz.py +++ b/app/viz.py @@ -1,22 +1,32 @@ -from taipy.gui import State, download -import pandas as pd import io + +import pandas as pd import plotly.graph_objects as go +from taipy.gui import State, download + class Viz: - def __init__(self, id:str, state: State, data: pd.DataFrame=None, fig:go.Figure = None, title:str=None, sub_title:str=None): - self.id= id + def __init__( + self, + id: str, + state: State, + data: pd.DataFrame = None, + fig: go.Figure = None, + title: str = None, + sub_title: str = None, + ): + self.id = id self.state = state self.data = data self.fig = fig self.title = id if title is None else title # Workaround used for responsive to preserve layout # "sub_title": "------------ --------- ---------" - self.sub_title = "------------ --------- ---------" if sub_title is None else sub_title + self.sub_title = "------------ --------- ---------" if sub_title is None else sub_title + + def on_action(self): + self._on_download() - def on_action(self): - self._on_download() - def _on_download(self): buffer = io.StringIO() data = self.data @@ -26,24 +36,23 @@ def _on_download(self): buffer.write(self.sub_title + "\n" + str(data)) download(self.state, content=bytes(buffer.getvalue(), "UTF-8"), name="data.csv") - def _to_state(self) -> dict[str]: return { "data": None if self is None else self.data, "fig": None if self is None else self.fig, "title": None if self is None else self.title, "sub_title": None if self is None else self.sub_title, - "on_action": None if self is None else self.on_action + "on_action": None if self is None else self.on_action, } - + def to_state(self) -> dict[str]: return self._to_state() - + def _to_empty() -> dict[str,]: - return Viz._to_state(None) - - def init(viz_set:set[str]) -> dict[str,dict]: - viz:dict[str,dict] = {} + return Viz._to_state(None) + + def init(viz_set: set[str]) -> dict[str, dict]: + viz: dict[str, dict] = {} for viz_id in viz_set: viz[viz_id] = Viz._to_empty() - return viz \ No newline at end of file + return viz From 28233441af03f7c383e87299b47bfafc5cb4ccdf Mon Sep 17 00:00:00 2001 From: Riws Date: Tue, 11 Jun 2024 21:25:33 +0200 Subject: [PATCH 2/2] chore: compliance with python nomenclature --- app/pages/company/company.py | 14 ++++++++------ app/viz.py | 35 ++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/app/pages/company/company.py b/app/pages/company/company.py index 7a14e11..b3ede46 100644 --- a/app/pages/company/company.py +++ b/app/pages/company/company.py @@ -1,3 +1,5 @@ +from typing import Union + import numpy as np import pandas as pd from taipy.gui import Markdown, State, get_state_id @@ -13,19 +15,19 @@ # Init bindings (used in md file) selected_company = DEFAULT_COMPANY selector_company: list[str] = [] -selected_year: str = None +selected_year: Union[str, None] = None selector_year: list[str] = [] -company_sector: str = None +company_sector: Union[str, None] = None company_upe_name: str = "" -df_selected_company: pd.DataFrame = None -df_count_company: pd.DataFrame = None +df_selected_company: Union[pd.DataFrame, None] = None +df_count_company: Union[pd.DataFrame, None] = None # Viz store map[viz_id,viz_dict] # Important for taipy bindings # Use Viz.init on each page with set of viz_id viz: dict[str, dict] = Viz.init( - ( + { "company_sector", "company_upe_name", "company_nb_reports", @@ -36,7 +38,7 @@ "fin_jurisdictions_top_revenue", "fin_pretax_profit_and_employees_rank", "fin_pretax_profit_and_profit_per_employee", - ) + } ) diff --git a/app/viz.py b/app/viz.py index c080513..8230162 100644 --- a/app/viz.py +++ b/app/viz.py @@ -1,4 +1,5 @@ import io +from typing import Any, Optional, Union import pandas as pd import plotly.graph_objects as go @@ -10,10 +11,10 @@ def __init__( self, id: str, state: State, - data: pd.DataFrame = None, - fig: go.Figure = None, - title: str = None, - sub_title: str = None, + data: Union[pd.DataFrame, Any] = None, + fig: Optional[go.Figure] = None, + title: Optional[str] = None, + sub_title: Optional[str] = None, ): self.id = id self.state = state @@ -36,21 +37,29 @@ def _on_download(self): buffer.write(self.sub_title + "\n" + str(data)) download(self.state, content=bytes(buffer.getvalue(), "UTF-8"), name="data.csv") - def _to_state(self) -> dict[str]: + def _to_state(self) -> dict[str, Any]: return { - "data": None if self is None else self.data, - "fig": None if self is None else self.fig, - "title": None if self is None else self.title, - "sub_title": None if self is None else self.sub_title, - "on_action": None if self is None else self.on_action, + "data": self.data, + "fig": self.fig, + "title": self.title, + "sub_title": self.sub_title, + "on_action": self.on_action, } - def to_state(self) -> dict[str]: + def to_state(self) -> dict[str, Any]: return self._to_state() - def _to_empty() -> dict[str,]: - return Viz._to_state(None) + @staticmethod + def _to_empty() -> dict[str, None]: + return { + "data": None, + "fig": None, + "title": None, + "sub_title": None, + "on_action": None, + } + @staticmethod def init(viz_set: set[str]) -> dict[str, dict]: viz: dict[str, dict] = {} for viz_id in viz_set: