Top 10 titulaires sur les pages acheteur #55
This commit is contained in:
@@ -211,6 +211,11 @@ summary > h3 {
|
||||
grid-row: 2;
|
||||
}
|
||||
|
||||
.org_top {
|
||||
grid-column: 1/3;
|
||||
grid-row: 3;
|
||||
}
|
||||
|
||||
/* Vue marché */
|
||||
|
||||
.marche_infos p {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import polars as pl
|
||||
from dash import dash_table, html
|
||||
|
||||
from utils import add_links_in_dict, format_montant, setup_table_columns
|
||||
|
||||
|
||||
def get_top_org_table(data, org_type: str):
|
||||
dff = pl.DataFrame(data)
|
||||
if dff.height == 0:
|
||||
return html.Div()
|
||||
|
||||
dff = dff.select(["uid", f"{org_type}_id", f"{org_type}_nom", "montant"])
|
||||
dff_nb = dff.group_by(f"{org_type}_id", f"{org_type}_nom").agg(
|
||||
pl.len().alias("Attributions"), pl.sum("montant").alias("montant")
|
||||
)
|
||||
dff_nb = dff_nb.sort(by="Attributions", descending=True)
|
||||
dff_nb = dff_nb.cast(pl.String)
|
||||
dff_nb = dff_nb.fill_null("")
|
||||
dff_nb = format_montant(dff_nb, column="montant")
|
||||
columns, tooltip = setup_table_columns(
|
||||
dff_nb, hideable=False, exclude=[f"{org_type}_id"]
|
||||
)
|
||||
data = dff_nb.to_dicts()
|
||||
data = add_links_in_dict(data, f"{org_type}")
|
||||
|
||||
print(dff_nb)
|
||||
|
||||
return dash_table.DataTable(
|
||||
data=data,
|
||||
markdown_options={"html": True},
|
||||
page_action="native",
|
||||
page_size=10,
|
||||
columns=columns,
|
||||
tooltip_header=tooltip,
|
||||
)
|
||||
+25
-6
@@ -3,6 +3,7 @@ import datetime
|
||||
import polars as pl
|
||||
from dash import Input, Output, State, callback, dash_table, dcc, html, register_page
|
||||
|
||||
from src.callbacks import get_top_org_table
|
||||
from src.figures import point_on_map
|
||||
from src.utils import (
|
||||
add_links_in_dict,
|
||||
@@ -89,6 +90,13 @@ layout = [
|
||||
],
|
||||
),
|
||||
html.Div(className="org_map", id="acheteur_map"),
|
||||
html.Div(
|
||||
className="org_top",
|
||||
children=[
|
||||
html.H3("Top titulaires"),
|
||||
html.Div(className="marches_table", id="top10_titulaires"),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
# récupérer les données de l'acheteur sur l'api annuaire
|
||||
@@ -146,22 +154,22 @@ def update_acheteur_infos(url):
|
||||
Input(component_id="acheteur_data", component_property="data"),
|
||||
)
|
||||
def update_acheteur_stats(data):
|
||||
df = pl.DataFrame(data)
|
||||
if df.height == 0:
|
||||
df = pl.DataFrame(schema=df.collect_schema())
|
||||
df_marches = df.unique("id")
|
||||
dff = pl.DataFrame(data)
|
||||
if dff.height == 0:
|
||||
dff = pl.DataFrame(schema=df.collect_schema())
|
||||
df_marches = dff.unique("id")
|
||||
nb_marches = format_number(df_marches.height)
|
||||
# somme_marches = format_number(int(df_marches.select(pl.sum("montant")).item()))
|
||||
marches_attribues = [html.Strong(nb_marches), " marchés et accord-cadres attribués"]
|
||||
# + ", pour un total de ", html.Strong(somme_marches + " €")]
|
||||
del df_marches
|
||||
|
||||
nb_titulaires = df.unique("titulaire_id").height
|
||||
nb_titulaires = dff.unique("titulaire_id").height
|
||||
nb_titulaires = [
|
||||
html.Strong(format_number(nb_titulaires)),
|
||||
" titulaires (SIRET) différents",
|
||||
]
|
||||
del df
|
||||
del dff
|
||||
|
||||
return marches_attribues, nb_titulaires
|
||||
|
||||
@@ -203,8 +211,11 @@ def get_acheteur_marches_data(url, acheteur_year: str) -> list[dict]:
|
||||
)
|
||||
def get_last_marches_table(data) -> html.Div:
|
||||
dff = pl.DataFrame(data)
|
||||
if dff.height == 0:
|
||||
return html.Div(html.P("Aucun marché trouvé."))
|
||||
dff = dff.cast(pl.String)
|
||||
dff = dff.fill_null("")
|
||||
print("1", dff.columns)
|
||||
dff = format_montant(dff)
|
||||
columns, tooltip = setup_table_columns(
|
||||
dff,
|
||||
@@ -252,6 +263,14 @@ def get_last_marches_table(data) -> html.Div:
|
||||
return table
|
||||
|
||||
|
||||
@callback(
|
||||
Output(component_id="top10_titulaires", component_property="children"),
|
||||
Input(component_id="acheteur_data", component_property="data"),
|
||||
)
|
||||
def get_top_titulaires(data):
|
||||
return get_top_org_table(data, "titulaire")
|
||||
|
||||
|
||||
@callback(
|
||||
Output("download-acheteur-data", "data"),
|
||||
Input("btn-download-acheteur-data", "n_clicks"),
|
||||
|
||||
+7
-5
@@ -77,15 +77,16 @@ def add_links(dff: pl.DataFrame):
|
||||
return dff
|
||||
|
||||
|
||||
def add_links_in_dict(data: list, org_type: str) -> list:
|
||||
def add_links_in_dict(data: list[dict], org_type: str) -> list:
|
||||
new_data = []
|
||||
for marche in data:
|
||||
org_id = marche[org_type + "_id"]
|
||||
marche[org_type + "_nom"] = (
|
||||
f'<a href="/{org_type}s/{org_id}">{marche[org_type + "_nom"]}</a>'
|
||||
)
|
||||
marche["id"] = f'<a href="/marches/{marche["uid"]}">{marche["id"]}</a>'
|
||||
marche["uid"] = f'<a href="/marches/{marche["uid"]}">{marche["uid"]}</a>'
|
||||
if marche.get("uid"):
|
||||
marche["id"] = f'<a href="/marches/{marche["uid"]}">{marche["id"]}</a>'
|
||||
marche["uid"] = f'<a href="/marches/{marche["uid"]}">{marche["uid"]}</a>'
|
||||
new_data.append(marche)
|
||||
return new_data
|
||||
|
||||
@@ -124,7 +125,7 @@ def format_number(number) -> str:
|
||||
return number
|
||||
|
||||
|
||||
def format_montant(dff: pl.DataFrame) -> pl.DataFrame:
|
||||
def format_montant(dff: pl.DataFrame, column: str = "montant") -> pl.DataFrame:
|
||||
def format_function(expr, scale=None):
|
||||
# https://stackoverflow.com/a/78636786
|
||||
expr = expr.cast(pl.String)
|
||||
@@ -155,7 +156,8 @@ def format_montant(dff: pl.DataFrame) -> pl.DataFrame:
|
||||
|
||||
return montant
|
||||
|
||||
dff = dff.with_columns(pl.col("montant").pipe(format_function).alias("montant"))
|
||||
print("3", dff.columns)
|
||||
dff = dff.with_columns(pl.col(column).pipe(format_function).alias(column))
|
||||
return dff
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user