Skip to content

Commit d473f6d

Browse files
committed
♻️ move is_static_report to init for quarto
- set based on report type, and this is set on init - avoid to pass on variable to all functions.
1 parent b43b248 commit d473f6d

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

src/vuegen/quarto_reportview.py

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ def __init__(
4141
self.report.logger.debug(f"PATH: {os.environ['PATH']}")
4242
self.report.logger.debug(f"sys.path: {sys.path}")
4343

44+
self.is_report_static = self.report_type in {
45+
r.ReportType.PDF,
46+
r.ReportType.DOCX,
47+
r.ReportType.ODT,
48+
r.ReportType.PPTX,
49+
}
50+
4451
def generate_report(
4552
self, output_dir: Path = BASE_DIR, static_dir: Path = STATIC_FILES_DIR
4653
) -> None:
@@ -78,12 +85,6 @@ def generate_report(
7885

7986
try:
8087
# Create variable to check if the report is static or revealjs
81-
is_report_static = self.report_type in {
82-
r.ReportType.PDF,
83-
r.ReportType.DOCX,
84-
r.ReportType.ODT,
85-
r.ReportType.PPTX,
86-
}
8788
is_report_revealjs = self.report_type == r.ReportType.REVEALJS
8889

8990
# Define the YAML header for the quarto report
@@ -123,7 +124,6 @@ def generate_report(
123124
subsection_content, subsection_imports = (
124125
self._generate_subsection(
125126
subsection,
126-
is_report_static,
127127
is_report_revealjs,
128128
static_dir=static_dir,
129129
)
@@ -391,7 +391,6 @@ def _create_yaml_header(self) -> str:
391391
def _generate_subsection(
392392
self,
393393
subsection,
394-
is_report_static,
395394
is_report_revealjs,
396395
static_dir: str,
397396
) -> tuple[List[str], List[str]]:
@@ -403,8 +402,6 @@ def _generate_subsection(
403402
----------
404403
subsection : Subsection
405404
The subsection containing the components.
406-
is_report_static : bool
407-
A boolean indicating whether the report is static or interactive.
408405
is_report_revealjs : bool
409406
A boolean indicating whether the report is in revealjs format.
410407
static_dir : str
@@ -432,15 +429,11 @@ def _generate_subsection(
432429

433430
if component.component_type == r.ComponentType.PLOT:
434431
subsection_content.extend(
435-
self._generate_plot_content(
436-
component, is_report_static, static_dir=static_dir
437-
)
432+
self._generate_plot_content(component, static_dir=static_dir)
438433
)
439434
elif component.component_type == r.ComponentType.DATAFRAME:
440435
subsection_content.extend(
441-
self._generate_dataframe_content(
442-
component, is_report_static, static_dir=static_dir
443-
)
436+
self._generate_dataframe_content(component, static_dir=static_dir)
444437
)
445438
elif (
446439
component.component_type == r.ComponentType.MARKDOWN
@@ -449,7 +442,7 @@ def _generate_subsection(
449442
subsection_content.extend(self._generate_markdown_content(component))
450443
elif (
451444
component.component_type == r.ComponentType.HTML
452-
and not is_report_static
445+
and not self.is_report_static
453446
):
454447
subsection_content.extend(self._generate_html_content(component))
455448
else:
@@ -465,9 +458,7 @@ def _generate_subsection(
465458
)
466459
return subsection_content, subsection_imports
467460

468-
def _generate_plot_content(
469-
self, plot, is_report_static, static_dir: str
470-
) -> List[str]:
461+
def _generate_plot_content(self, plot, static_dir: str) -> List[str]:
471462
"""
472463
Generate content for a plot component based on the report type.
473464
@@ -488,7 +479,7 @@ def _generate_plot_content(
488479
plot_content.append(f"### {plot.title}")
489480

490481
# Define plot path
491-
if is_report_static:
482+
if self.is_report_static:
492483
static_plot_path = Path(static_dir) / f"{plot.title.replace(' ', '_')}.png"
493484
else:
494485
html_plot_file = Path(static_dir) / f"{plot.title.replace(' ', '_')}.html"
@@ -501,7 +492,7 @@ def _generate_plot_content(
501492
)
502493
elif plot.plot_type == r.PlotType.PLOTLY:
503494
plot_content.append(self._generate_plot_code(plot))
504-
if is_report_static:
495+
if self.is_report_static:
505496
plot_content.append(
506497
f"""fig_plotly.write_image("{static_plot_path.resolve().as_posix()}")\n```\n"""
507498
)
@@ -510,7 +501,7 @@ def _generate_plot_content(
510501
plot_content.append(f"""fig_plotly.show()\n```\n""")
511502
elif plot.plot_type == r.PlotType.ALTAIR:
512503
plot_content.append(self._generate_plot_code(plot))
513-
if is_report_static:
504+
if self.is_report_static:
514505
plot_content.append(
515506
f"""fig_altair.save("{static_plot_path.resolve().as_posix()}")\n```\n"""
516507
)
@@ -522,7 +513,7 @@ def _generate_plot_content(
522513
if isinstance(networkx_graph, tuple):
523514
# If network_data is a tuple, separate the network and html file path
524515
networkx_graph, html_plot_file = networkx_graph
525-
elif isinstance(networkx_graph, nx.Graph) and not is_report_static:
516+
elif isinstance(networkx_graph, nx.Graph) and not self.is_report_static:
526517
# Get the pyvis object and create html
527518
pyvis_graph = plot.create_and_save_pyvis_network(
528519
networkx_graph, html_plot_file
@@ -535,7 +526,7 @@ def _generate_plot_content(
535526
plot_content.append(f"**Number of edges:** {num_edges}\n")
536527

537528
# Add code to generate network depending on the report type
538-
if is_report_static:
529+
if self.is_report_static:
539530
plot.save_network_image(networkx_graph, static_plot_path, "png")
540531
plot_content.append(self._generate_image_content(static_plot_path))
541532
else:
@@ -619,18 +610,14 @@ def _generate_plot_code(self, plot, output_file="") -> str:
619610
</div>\n"""
620611
return plot_code
621612

622-
def _generate_dataframe_content(
623-
self, dataframe, is_report_static, static_dir: str
624-
) -> List[str]:
613+
def _generate_dataframe_content(self, dataframe, static_dir: str) -> List[str]:
625614
"""
626615
Generate content for a DataFrame component based on the report type.
627616
628617
Parameters
629618
----------
630619
dataframe : DataFrame
631620
The dataframe component to add to content.
632-
is_report_static : bool
633-
A boolean indicating whether the report is static or interactive.
634621
static_dir : str
635622
The folder where the static files will be saved.
636623
@@ -683,7 +670,7 @@ def _generate_dataframe_content(
683670

684671
# Display the dataframe
685672
dataframe_content.extend(
686-
self._show_dataframe(dataframe, is_report_static, static_dir=static_dir)
673+
self._show_dataframe(dataframe, static_dir=static_dir)
687674
)
688675

689676
except Exception as e:
@@ -835,18 +822,14 @@ def _generate_image_content(
835822
f"""![](/{src}){{fig-alt={alt_text} width={width} height={height}}}\n"""
836823
)
837824

838-
def _show_dataframe(
839-
self, dataframe, is_report_static, static_dir: str
840-
) -> List[str]:
825+
def _show_dataframe(self, dataframe, static_dir: str) -> List[str]:
841826
"""
842827
Appends either a static image or an interactive representation of a DataFrame to the content list.
843828
844829
Parameters
845830
----------
846831
dataframe : DataFrame
847832
The DataFrame object containing the data to display.
848-
is_report_static : bool
849-
Determines if the report is in a static format (e.g., PDF) or interactive (e.g., HTML).
850833
static_dir : str
851834
The folder where the static files will be saved.
852835
@@ -856,7 +839,7 @@ def _show_dataframe(
856839
The list of content lines for the DataFrame.
857840
"""
858841
dataframe_content = []
859-
if is_report_static:
842+
if self.is_report_static:
860843
# Generate path for the DataFrame image
861844
df_image = Path(static_dir) / f"{dataframe.title.replace(' ', '_')}.png"
862845
dataframe_content.append(

0 commit comments

Comments
 (0)