1
1
"""
2
2
Report generation helper module.
3
3
"""
4
- from base64 import b64encode
5
4
from datetime import datetime
6
5
from typing import Optional
7
6
8
7
import datapane as dp
9
- import importlib_resources as ir
10
8
import pandas as pd
11
- from jinja2 import Environment , FileSystemLoader
12
9
from shapash .explainer .smart_explainer import SmartExplainer
13
10
14
11
from eurybia import SmartDrift
15
12
from eurybia .report .project_report import DriftReport
16
13
17
14
18
- def _load_custom_template (report : dp .Report ) -> dp .Report :
19
- """
20
- This function feeds a customised html template to Datapane
21
-
22
- Parameters
23
- ----------
24
- report : datapane.Report
25
- Report object
26
- Returns
27
- ----------
28
- datapane.Report
29
- """
30
- report ._local_writer .assets = ir .files ("eurybia.assets" )
31
- logo_img = (report ._local_writer .assets / "logo_eurybia_dp.png" ).read_bytes ()
32
- report ._local_writer .logo = f"data:image/png;base64,{ b64encode (logo_img ).decode ('ascii' )} "
33
- template_loader = FileSystemLoader (report ._local_writer .assets )
34
- template_env = Environment (loader = template_loader )
35
- template_env .globals ["include_raw" ] = dp .client .api .report .core .include_raw
36
- report ._local_writer .template = template_env .get_template ("report_template.html" )
37
- return report
38
-
39
-
40
- def _get_index (dr : DriftReport , project_info_file : str , config : Optional [dict ]) -> dp .Page :
15
+ def _get_index (dr : DriftReport , project_info_file : str , config_report : Optional [dict ]) -> dp .Page :
41
16
"""
42
17
This function generates and returns a Datapane page containing the Eurybia report index
43
18
@@ -47,7 +22,7 @@ def _get_index(dr: DriftReport, project_info_file: str, config: Optional[dict])
47
22
DriftReport object
48
23
project_info_file : str
49
24
Path to the file used to display some information about the project in the report.
50
- config : dict, optional
25
+ config_report : dict, optional
51
26
Report configuration options.
52
27
Returns
53
28
----------
@@ -66,8 +41,12 @@ def _get_index(dr: DriftReport, project_info_file: str, config: Optional[dict])
66
41
# Title and logo
67
42
index_block += [dp .Group (dp .HTML (eurybia_logo ), dp .Text (f"# { dr .title_story } " ), columns = 2 )]
68
43
69
- if config is not None and "title_description" in config .keys () and config ["title_description" ] != "" :
70
- raw_title = config ["title_description" ]
44
+ if (
45
+ config_report is not None
46
+ and "title_description" in config_report .keys ()
47
+ and config_report ["title_description" ] != ""
48
+ ):
49
+ raw_title = config_report ["title_description" ]
71
50
index_block += [dp .Text (f"## { raw_title } " )]
72
51
index_str = "## Eurybia Report contents \n "
73
52
@@ -317,8 +296,8 @@ def _get_datadrift(dr: DriftReport) -> dp.Page:
317
296
Features are sorted according to their respective importance in the datadrift classifier.
318
297
For categorical features, the possible values are sorted by descending difference between the two datasets."""
319
298
),
320
- dp .Select (blocks = plot_dataset_analysis ),
321
- dp .Select (blocks = table_dataset_analysis ),
299
+ dp .Select (blocks = plot_dataset_analysis , type = dp . SelectType . DROPDOWN ),
300
+ dp .Select (blocks = table_dataset_analysis , type = dp . SelectType . DROPDOWN ),
322
301
]
323
302
if dr .smartdrift .deployed_model is not None :
324
303
blocks += [
@@ -350,7 +329,7 @@ def _get_datadrift(dr: DriftReport) -> dp.Page:
350
329
This representation constitutes a support to understand the drift when the analysis of the dataset is unclear.
351
330
In the drop-down menu, features are sorted by importance in the data drift detection."""
352
331
),
353
- dp .Select (blocks = plot_datadrift_contribution ),
332
+ dp .Select (blocks = plot_datadrift_contribution , type = dp . SelectType . DROPDOWN ),
354
333
]
355
334
if dr .smartdrift .historical_auc is not None :
356
335
blocks += [
@@ -387,7 +366,7 @@ def _get_modeldrift(dr: DriftReport) -> dp.Page:
387
366
else :
388
367
for i in range (len (labels )):
389
368
plot_modeldrift .append (dp .Plot (fig_list [i ], label = labels [i ]))
390
- modeldrift_plot = dp .Select (blocks = plot_modeldrift , label = "reference_columns" )
369
+ modeldrift_plot = dp .Select (blocks = plot_modeldrift , label = "reference_columns" , type = dp . SelectType . DROPDOWN )
391
370
else :
392
371
modeldrift_plot = dp .Text ("## Smartdrift.data_modeldrift is None" )
393
372
blocks = [
@@ -409,7 +388,7 @@ def execute_report(
409
388
explainer : SmartExplainer ,
410
389
project_info_file : str ,
411
390
output_file : str ,
412
- config : Optional [dict ] = None ,
391
+ config_report : Optional [dict ] = None ,
413
392
):
414
393
"""
415
394
Creates the report
@@ -422,33 +401,30 @@ def execute_report(
422
401
Compiled shapash explainer.
423
402
project_info_file : str
424
403
Path to the file used to display some information about the project in the report.
425
- config : dict, optional
404
+ config_report : dict, optional
426
405
Report configuration options.
427
406
output_file : str
428
407
Path to the HTML file to write
429
408
"""
430
409
431
- if config is None :
432
- config = {}
410
+ if config_report is None :
411
+ config_report = {}
433
412
434
413
dr = DriftReport (
435
414
smartdrift = smartdrift ,
436
415
explainer = explainer , # rename to match kwarg
437
416
project_info_file = project_info_file ,
438
- config = config ,
417
+ config_report = config_report ,
439
418
)
440
419
441
420
pages = []
442
- pages .append (_get_index (dr , project_info_file , config ))
421
+ pages .append (_get_index (dr , project_info_file , config_report ))
443
422
if project_info_file is not None :
444
423
pages .append (_get_project_info (dr ))
445
424
pages .append (_get_consistency_analysis (dr ))
446
425
pages .append (_get_datadrift (dr ))
447
426
if dr .smartdrift .data_modeldrift is not None :
448
427
pages .append (_get_modeldrift (dr ))
449
428
450
- report = dp .Report (blocks = pages )
451
- report = _load_custom_template (report )
452
- report ._save (
453
- path = output_file , open = False , formatting = dp .ReportFormatting (light_prose = False , width = dp .ReportWidth .MEDIUM )
454
- )
429
+ report = dp .View (blocks = pages )
430
+ dp .save_report (report , path = output_file , open = False , name = "report.html" )
0 commit comments