Skip to content

Commit 50b988e

Browse files
committed
🐛 Fix(quarto_reportview.py): avoid showing suplicated sheets and remove Ipython prompts in static reports
1 parent 2b4103b commit 50b988e

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/vuegen/quarto_reportview.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import textwrap
55
from pathlib import Path
6-
from typing import List
6+
from typing import List, Optional
77

88
import networkx as nx
99

@@ -272,10 +272,6 @@ def run_report(self, output_dir: str = BASE_DIR) -> None:
272272
[self.quarto_path, "install", "tinytex", "--no-prompt"],
273273
check=True,
274274
)
275-
subprocess.run(
276-
[self.quarto_path, "install", "chromium", "--no-prompt"],
277-
check=True,
278-
)
279275
try:
280276
subprocess.run(
281277
args,
@@ -723,13 +719,6 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
723719
)
724720
# Mapping of file extensions to read functions
725721
read_function_mapping = table_utils.read_function_mapping
726-
# {
727-
# r.DataFrameFormat.CSV.value_with_dot: pd.read_csv,
728-
# r.DataFrameFormat.PARQUET.value_with_dot: pd.read_parquet,
729-
# r.DataFrameFormat.TXT.value_with_dot: pd.read_table,
730-
# r.DataFrameFormat.XLS.value_with_dot: pd.read_excel,
731-
# r.DataFrameFormat.XLSX.value_with_dot: pd.read_excel,
732-
# }
733722
try:
734723
# Check if the file extension matches any DataFrameFormat value
735724
file_extension = Path(dataframe.file_path).suffix.lower()
@@ -796,8 +785,9 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
796785
f"sheet_name='{sheet_name}')\n"
797786
)
798787
# Display the dataframe
799-
# ! Probably this still does not work for static reports...
800-
dataframe_content.extend(self._show_dataframe(dataframe))
788+
dataframe_content.extend(
789+
self._show_dataframe(dataframe, suffix=sheet_name)
790+
)
801791

802792
except Exception as e:
803793
self.report.logger.error(
@@ -880,14 +870,17 @@ def _generate_markdown_content(self, markdown) -> List[str]:
880870
)
881871
return markdown_content
882872

883-
def _show_dataframe(self, dataframe) -> List[str]:
873+
def _show_dataframe(self, dataframe, suffix: Optional[str] = None) -> List[str]:
884874
"""
885875
Appends either a static image or an interactive representation of a DataFrame to the content list.
886876
887877
Parameters
888878
----------
889879
dataframe : DataFrame
890-
The DataFrame object containing the data to display.
880+
The DataFrame object containing the data to display.
881+
suffix : str, optional
882+
A suffix to append to the DataFrame image file name like a sheet name
883+
or another identifier (default is None).
891884
892885
Returns
893886
-------
@@ -897,9 +890,13 @@ def _show_dataframe(self, dataframe) -> List[str]:
897890
dataframe_content = []
898891
if self.is_report_static:
899892
# Generate path for the DataFrame image
900-
fpath_df_image = (
901-
Path(self.static_dir) / f"{dataframe.title.replace(' ', '_')}.png"
902-
)
893+
fpath_df_image = Path(self.static_dir) / dataframe.title.replace(" ", "_")
894+
if suffix:
895+
fpath_df_image = fpath_df_image.with_stem(
896+
fpath_df_image.stem + f"_{suffix.replace(' ', '_')}"
897+
)
898+
fpath_df_image = fpath_df_image.with_suffix(".png")
899+
903900
dataframe_content.append(
904901
f"df.dfi.export('{Path(fpath_df_image).relative_to('quarto_report').as_posix()}',"
905902
" max_rows=10, max_cols=5, table_conversion='matplotlib')\n```\n"
@@ -1016,10 +1013,13 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
10161013
"import json",
10171014
],
10181015
},
1019-
"dataframe": [
1016+
"static_dataframe": [
10201017
"import pandas as pd",
1021-
"from itables import show, init_notebook_mode",
10221018
"import dataframe_image as dfi",
1019+
],
1020+
"interactive_dataframe": [
1021+
"import pandas as pd",
1022+
"from itables import show, init_notebook_mode",
10231023
"init_notebook_mode(all_interactive=True)",
10241024
],
10251025
"markdown": ["import IPython.display as display", "import requests"],
@@ -1035,7 +1035,10 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
10351035
if plot_type in components_imports["plot"]:
10361036
component_imports.extend(components_imports["plot"][plot_type])
10371037
elif component_type == r.ComponentType.DATAFRAME:
1038-
component_imports.extend(components_imports["dataframe"])
1038+
if self.is_report_static:
1039+
component_imports.extend(components_imports["static_dataframe"])
1040+
else:
1041+
component_imports.extend(components_imports["interactive_dataframe"])
10391042
elif component_type == r.ComponentType.MARKDOWN:
10401043
component_imports.extend(components_imports["markdown"])
10411044

0 commit comments

Comments
 (0)