From 1171708d9eb79c4f5b1908c2f602801780dc2461 Mon Sep 17 00:00:00 2001 From: Colin Maudry Date: Fri, 13 Jun 2025 10:54:42 +0200 Subject: [PATCH] =?UTF-8?q?Le=20t=C3=A9l=C3=A9chargement=20Excel=20inclut?= =?UTF-8?q?=20toutes=20les=20donn=C3=A9es=20filtr=C3=A9es=20close=20#20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .pre-commit-config.yaml | 1 + pyproject.toml | 1 + src/pages/home.py | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 11f83b8..f981aa9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,6 +15,7 @@ repos: rev: v0.11.12 hooks: - id: ruff + args: [ "--fix" ] - id: ruff args: [ "check", "--select", "I", "--fix" ] - id: ruff-format diff --git a/pyproject.toml b/pyproject.toml index 835bb7f..e154584 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ dependencies = [ "gunicorn", "dash-bootstrap-components", "python-dotenv", + "xlsxwriter" ] [project.optional-dependencies] diff --git a/src/pages/home.py b/src/pages/home.py index 0efd31b..2f2e37c 100644 --- a/src/pages/home.py +++ b/src/pages/home.py @@ -1,5 +1,6 @@ import logging import os +from datetime import datetime from time import sleep import polars as pl @@ -39,6 +40,7 @@ except ComputeError: df: pl.DataFrame = pl.read_parquet(os.getenv("DATA_FILE_PARQUET_PATH")) schema = df.schema +df_filtered = pl.DataFrame() lf: pl.LazyFrame = df.lazy() # Suppression des colonnes inutiles @@ -81,9 +83,9 @@ datatable = dash_table.DataTable( selected_rows=[], # sort_action="native", # sort_mode="multi", - export_format="xlsx", - export_columns="visible", - export_headers="ids", + # export_format="xlsx", + # export_columns="visible", + # export_headers="ids", style_cell_conditional=[ { "if": {"column_id": "objet"}, @@ -137,7 +139,11 @@ layout = [ overlay_style={"visibility": "visible", "filter": "blur(2px)"}, id="loading-1", type="default", - children=datatable, + children=[ + html.Button("Téléchargement", id="btn-download-data"), + dcc.Download(id="download-data"), + datatable, + ], ), ] @@ -152,6 +158,8 @@ layout = [ ) def update_table(page_current, page_size, filter_query, data_timestamp): print(" + + + + + + + + + + + + + + + + + + ") + global df_filtered + # 1. Apply Filters lff: pl.LazyFrame = lf # start from the original data if filter_query: @@ -194,8 +202,28 @@ def update_table(page_current, page_size, filter_query, data_timestamp): # Remplacement des valeurs numériques par des chaînes de caractères lff = numbers_to_strings(lff) - dff = lff.slice(start_row, page_size).collect() + dff = lff.collect() + + df_filtered = dff.clone() + + dff = dff.slice(start_row, page_size) # print("dff_sliced:", lff.select("titulaire.typeId")) dff = dff.to_dicts() - return dff, data_timestamp + 1 # update data, update timestamp + return ( + dff, + data_timestamp + 1, + ) # update data, update timestamp + + +@callback( + Output("download-data", "data"), + Input("btn-download-data", "n_clicks"), + prevent_initial_call=True, +) +def download_data(n_clicks): + def to_bytes(buffer): + df_filtered.write_excel(buffer, worksheet="DECP") + + date = datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + return dcc.send_bytes(to_bytes, filename=f"decp_{date}.xlsx")