Bonne configuration des boutons de téléchargemetn
This commit is contained in:
+105
-52
@@ -6,14 +6,16 @@ from dash import Input, Output, State, callback, dcc, html, register_page
|
||||
from src.callbacks import get_top_org_table
|
||||
from src.figures import DataTable, point_on_map
|
||||
from src.utils import (
|
||||
add_links_in_dict,
|
||||
df,
|
||||
filter_table_data,
|
||||
format_number,
|
||||
format_values,
|
||||
get_annuaire_data,
|
||||
get_button_properties,
|
||||
get_default_hidden_columns,
|
||||
get_departement_region,
|
||||
meta_content,
|
||||
setup_table_columns,
|
||||
prepare_table_data,
|
||||
sort_table_data,
|
||||
)
|
||||
|
||||
register_page(
|
||||
@@ -26,13 +28,22 @@ register_page(
|
||||
order=5,
|
||||
)
|
||||
|
||||
# 21690123100011
|
||||
datatable = html.Div(
|
||||
className="marches_table",
|
||||
children=DataTable(
|
||||
dtid="titulaire_datatable",
|
||||
page_action="custom",
|
||||
filter_action="custom",
|
||||
sort_action="custom",
|
||||
page_size=10,
|
||||
hidden_columns=get_default_hidden_columns(page="titulaire"),
|
||||
),
|
||||
)
|
||||
|
||||
layout = [
|
||||
dcc.Store(id="titulaire_data", storage_type="memory"),
|
||||
dcc.Location(id="url", refresh="callback-nav"),
|
||||
html.Div(
|
||||
className="container",
|
||||
children=[
|
||||
html.Div(
|
||||
className="wrapper",
|
||||
@@ -86,9 +97,9 @@ layout = [
|
||||
html.P(id="titulaire_acheteurs_differents"),
|
||||
html.Button(
|
||||
"Téléchargement au format Excel",
|
||||
id="btn-download-titulaire-data",
|
||||
id="btn-download-data-titulaire",
|
||||
),
|
||||
dcc.Download(id="download-titulaire-data"),
|
||||
dcc.Download(id="download-data-titulaire"),
|
||||
],
|
||||
),
|
||||
html.Div(className="org_map", id="titulaire_map"),
|
||||
@@ -103,7 +114,26 @@ layout = [
|
||||
),
|
||||
# récupérer les données de l'acheteur sur l'api annuaire
|
||||
html.H3("Derniers marchés publics remportés"),
|
||||
html.Div(id="titulaire_last_marches", children=""),
|
||||
dcc.Loading(
|
||||
overlay_style={"visibility": "visible", "filter": "blur(2px)"},
|
||||
id="loading-home",
|
||||
type="default",
|
||||
children=[
|
||||
html.Div(
|
||||
[
|
||||
html.P("lignes", id="titulaire_nb_rows"),
|
||||
html.Button(
|
||||
"Téléchargement désactivé au-delà de 65 000 lignes",
|
||||
id="btn-download-filtered-data-titulaire",
|
||||
disabled=True,
|
||||
),
|
||||
dcc.Download(id="titulaire-download-filtered-data"),
|
||||
],
|
||||
className="table-menu",
|
||||
),
|
||||
datatable,
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -178,68 +208,55 @@ def update_titulaire_stats(data):
|
||||
|
||||
@callback(
|
||||
Output(component_id="titulaire_data", component_property="data"),
|
||||
Output("btn-download-data-titulaire", "disabled"),
|
||||
Output("btn-download-data-titulaire", "children"),
|
||||
Output("btn-download-data-titulaire", "title"),
|
||||
Input(component_id="url", component_property="pathname"),
|
||||
Input(component_id="titulaire_year", component_property="value"),
|
||||
)
|
||||
def get_titulaire_marches_data(url, titulaire_year: str) -> list[dict]:
|
||||
def get_titulaire_marches_data(url, titulaire_year: str) -> tuple:
|
||||
titulaire_siret = url.split("/")[-1]
|
||||
lff = df.lazy()
|
||||
lff = lff.filter(
|
||||
(pl.col("titulaire_id") == titulaire_siret)
|
||||
& (pl.col("titulaire_typeIdentifiant") == "SIRET")
|
||||
)
|
||||
lff = lff.select(
|
||||
"id",
|
||||
"uid",
|
||||
"objet",
|
||||
"dateNotification",
|
||||
"acheteur_id",
|
||||
"acheteur_nom",
|
||||
"distance",
|
||||
"montant",
|
||||
"codeCPV",
|
||||
"dureeMois",
|
||||
)
|
||||
if titulaire_year and titulaire_year != "Toutes":
|
||||
lff = lff.filter(
|
||||
pl.col("dateNotification").cast(pl.String).str.starts_with(titulaire_year)
|
||||
)
|
||||
lff = lff.sort(["dateNotification", "uid"], descending=True, nulls_last=True)
|
||||
lff = lff.fill_null("")
|
||||
|
||||
data = lff.collect(engine="streaming").to_dicts()
|
||||
return data
|
||||
dff: pl.DataFrame = lff.collect(engine="streaming")
|
||||
download_disabled, download_text, download_title = get_button_properties(dff.height)
|
||||
|
||||
data = dff.to_dicts()
|
||||
return data, download_disabled, download_text, download_title
|
||||
|
||||
|
||||
@callback(
|
||||
Output(component_id="titulaire_last_marches", component_property="children"),
|
||||
Input(component_id="titulaire_data", component_property="data"),
|
||||
Output("titulaire_datatable", "data"),
|
||||
Output("titulaire_datatable", "columns"),
|
||||
Output("titulaire_datatable", "tooltip_header"),
|
||||
Output("titulaire_datatable", "data_timestamp"),
|
||||
Output("titulaire_nb_rows", "children"),
|
||||
Output("btn-download-filtered-data-titulaire", "disabled"),
|
||||
Output("btn-download-filtered-data-titulaire", "children"),
|
||||
Output("btn-download-filtered-data-titulaire", "title"),
|
||||
Input("titulaire_data", "data"),
|
||||
Input("titulaire_datatable", "page_current"),
|
||||
Input("titulaire_datatable", "page_size"),
|
||||
Input("titulaire_datatable", "filter_query"),
|
||||
Input("titulaire_datatable", "sort_by"),
|
||||
State("titulaire_datatable", "data_timestamp"),
|
||||
)
|
||||
def get_last_marches_table(data) -> html.Div:
|
||||
dff = pl.DataFrame(data)
|
||||
dff = dff.cast(pl.String)
|
||||
dff = dff.fill_null("")
|
||||
dff = format_values(dff)
|
||||
columns, tooltip = setup_table_columns(
|
||||
dff, hideable=False, exclude=["acheteur_id", "id"]
|
||||
def get_last_marches_data(
|
||||
data, page_current, page_size, filter_query, sort_by, data_timestamp
|
||||
) -> list[dict]:
|
||||
return prepare_table_data(
|
||||
data, data_timestamp, filter_query, page_current, page_size, sort_by
|
||||
)
|
||||
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_links_in_dict(data, "acheteur")
|
||||
|
||||
table = html.Div(
|
||||
className="marches_table",
|
||||
children=DataTable(
|
||||
dtid="titulaire_data_table",
|
||||
data=data,
|
||||
page_action="native",
|
||||
filter_action="native",
|
||||
columns=columns,
|
||||
tooltip_header=tooltip,
|
||||
page_size=10,
|
||||
),
|
||||
)
|
||||
return table
|
||||
|
||||
|
||||
@callback(
|
||||
@@ -251,8 +268,8 @@ def get_top_acheteurs(data):
|
||||
|
||||
|
||||
@callback(
|
||||
Output("download-titulaire-data", "data"),
|
||||
Input("btn-download-titulaire-data", "n_clicks"),
|
||||
Output("download-data-titulaire", "data"),
|
||||
Input("btn-download-data-titulaire", "n_clicks"),
|
||||
State(component_id="titulaire_data", component_property="data"),
|
||||
State(component_id="titulaire_nom", component_property="children"),
|
||||
State(component_id="titulaire_year", component_property="value"),
|
||||
@@ -273,3 +290,39 @@ def download_titulaire_data(
|
||||
|
||||
date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
|
||||
return dcc.send_bytes(to_bytes, filename=f"decp_{titulaire_nom}_{date}.xlsx")
|
||||
|
||||
|
||||
@callback(
|
||||
Output("titulaire-download-filtered-data", "data"),
|
||||
State("titulaire_data", "data"),
|
||||
Input("btn-download-filtered-data-titulaire", "n_clicks"),
|
||||
State("titulaire_nom", "children"),
|
||||
State("titulaire_datatable", "filter_query"),
|
||||
State("titulaire_datatable", "sort_by"),
|
||||
State("titulaire_datatable", "hidden_columns"),
|
||||
prevent_initial_call=True,
|
||||
)
|
||||
def download_filtered_titulaire_data(
|
||||
data, n_clicks, titulaire_nom, filter_query, sort_by, hidden_columns: list = None
|
||||
):
|
||||
lff: pl.LazyFrame = pl.LazyFrame(
|
||||
data
|
||||
) # start from the full titulaire data, not from paginated table data
|
||||
|
||||
# Les colonnes masquées sont supprimées
|
||||
if hidden_columns:
|
||||
lff = lff.drop(hidden_columns)
|
||||
|
||||
if filter_query:
|
||||
lff = filter_table_data(lff, filter_query)
|
||||
|
||||
if len(sort_by) > 0:
|
||||
lff = sort_table_data(lff, sort_by)
|
||||
|
||||
def to_bytes(buffer):
|
||||
lff.collect(engine="streaming").write_excel(buffer, worksheet="DECP")
|
||||
|
||||
date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
|
||||
return dcc.send_bytes(
|
||||
to_bytes, filename=f"decp_filtrées_{titulaire_nom}_{date}.xlsx"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user