diff --git a/src/pages/acheteur.py b/src/pages/acheteur.py index 1b3f202..54c3ada 100644 --- a/src/pages/acheteur.py +++ b/src/pages/acheteur.py @@ -6,6 +6,7 @@ from dash import Input, Output, State, callback, dash_table, dcc, html, register from src.figures import point_on_map from src.utils import ( add_org_links_in_dict, + format_montant, format_number, get_annuaire_data, get_departement_region, @@ -180,6 +181,7 @@ def get_acheteur_marches_data(url, acheteur_year: str) -> pl.LazyFrame: "objet", "dateNotification", "titulaire_id", + "titulaire_typeIdentifiant", "titulaire_nom", "montant", "codeCPV", @@ -210,6 +212,9 @@ def get_last_marches_table(data) -> html.Div: "dureeMois", ] + dff = pl.DataFrame(data) + dff = format_montant(dff) + data = dff.to_dicts() data = add_org_links_in_dict(data, "titulaire") table = html.Div( @@ -231,7 +236,7 @@ def get_last_marches_table(data) -> html.Div: "hideable": False, } for i in columns - if i not in ["titulaire_id"] + if i not in ["titulaire_id", "titulaire_typeIdentifiant"] ], page_size=10, style_cell_conditional=[ diff --git a/src/pages/home.py b/src/pages/home.py index 48dd24e..ede5c74 100644 --- a/src/pages/home.py +++ b/src/pages/home.py @@ -8,6 +8,7 @@ from src.utils import ( add_org_links, add_resource_link, filter_table_data, + format_montant, format_number, lf, meta_content, @@ -184,6 +185,9 @@ def update_table(page_current, page_size, filter_query, sort_by, data_timestamp) # Ajout des liens vers les fichiers Open Data dff = add_resource_link(dff) + # Formatage des montants + dff = format_montant(dff) + # Liste finale de colonnes columns = [ { diff --git a/src/pages/titulaire.py b/src/pages/titulaire.py index b0d1aa6..7ea1a2a 100644 --- a/src/pages/titulaire.py +++ b/src/pages/titulaire.py @@ -6,6 +6,7 @@ from dash import Input, Output, State, callback, dash_table, dcc, html, register from src.figures import point_on_map from src.utils import ( add_org_links_in_dict, + format_montant, format_number, get_annuaire_data, get_departement_region, @@ -213,7 +214,11 @@ def get_last_marches_table(data) -> html.Div: "dureeMois", ] - # Ajout des liens vers les acheteurs + dff = pl.DataFrame(data) + dff = format_montant(dff) + data = dff.to_dicts() + # Idéalement on utiliserait add_org_links(), mais le résultat attendu + # est différent de home.py (Tableau) data = add_org_links_in_dict(data, "acheteur") table = html.Div( diff --git a/src/utils.py b/src/utils.py index d56b031..d49a7f5 100644 --- a/src/utils.py +++ b/src/utils.py @@ -119,6 +119,34 @@ def format_number(number) -> str: return number +def format_montant(dff: pl.DataFrame) -> pl.DataFrame: + def format_function(expr, scale=None): + # https://stackoverflow.com/a/78636786 + expr = expr.cast(pl.String).str.splitn(".", 2) + + num = expr.struct[0] + frac = expr.struct[1] + + # Ajout des espaces + num = ( + num.str.reverse() + .str.replace_all(r"\d{3}", "$0 ") + .str.reverse() + .str.replace(r"^ ", "") + ) + + frac: pl.Expr = ( + pl.when(frac.is_not_null() & ~frac.is_in(["0"])) + .then("," + frac) + .otherwise(pl.lit("")) + ) + + return num + frac + pl.lit(" €") + + dff = dff.with_columns(pl.col("montant").pipe(format_function).alias("montant")) + return dff + + def get_annuaire_data(siret: str) -> dict: url = f"https://recherche-entreprises.api.gouv.fr/search?q={siret}" response = get(url)