Skip to content

Commit 30b531c

Browse files
committed
♻️ factor out component creation
- very similar to _combine_components in streamlit_report - imports has to be a list of list -> figure this out - added debug message for skipping components in static reports-> should maybe be a warining?
1 parent 8c20abd commit 30b531c

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

src/vuegen/quarto_reportview.py

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ def __init__(
6464
r.ReportType.PPTX,
6565
}
6666

67+
self.components_fct_map = {
68+
r.ComponentType.PLOT: self._generate_plot_content,
69+
r.ComponentType.DATAFRAME: self._generate_dataframe_content,
70+
r.ComponentType.MARKDOWN: self._generate_markdown_content,
71+
r.ComponentType.HTML: self._generate_html_content,
72+
}
73+
6774
def generate_report(self, output_dir: Path = BASE_DIR) -> None:
6875
"""
6976
Generates the qmd file of the quarto report. It creates code for rendering each section and its subsections with all components.
@@ -399,6 +406,39 @@ def _create_yaml_header(self) -> str:
399406

400407
return yaml_header
401408

409+
def _combine_components(self, components: list[dict]) -> tuple[list, list]:
410+
"""combine a list of components."""
411+
412+
all_contents = []
413+
all_imports = []
414+
415+
for component in components:
416+
# Write imports if not already done
417+
component_imports = self._generate_component_imports(component)
418+
self.report.logger.debug("component_imports: %s", component_imports)
419+
all_imports.append(component_imports) # ! different than for streamlit
420+
421+
# Handle different types of components
422+
fct = self.components_fct_map.get(component.component_type, None)
423+
if fct is None:
424+
self.report.logger.warning(
425+
f"Unsupported component type '{component.component_type}' "
426+
)
427+
elif (
428+
component.component_type == r.ComponentType.MARKDOWN
429+
and component.title.lower() == "description"
430+
):
431+
self.report.logger.debug("Skipping description.md markdown of section.")
432+
elif (
433+
component.component_type == r.ComponentType.HTML
434+
and self.is_report_static
435+
):
436+
self.report.logger.debug("Skipping HTML component for static report.")
437+
else:
438+
content = fct(component)
439+
all_contents.extend(content)
440+
return all_contents, all_imports
441+
402442
def _generate_subsection(
403443
self,
404444
subsection,
@@ -422,38 +462,20 @@ def _generate_subsection(
422462
- list of imports for the subsection (List[str])
423463
"""
424464
subsection_content = []
425-
subsection_imports = []
426465

427466
# Add subsection header and description
428467
subsection_content.append(f"## {subsection.title}")
429468
if subsection.description:
430469
subsection_content.append(f"""{subsection.description}\n""")
431470

432471
if is_report_revealjs:
433-
subsection_content.append(f"::: {{.panel-tabset}}\n")
434-
435-
for component in subsection.components:
436-
component_imports = self._generate_component_imports(component)
437-
subsection_imports.append(component_imports)
472+
subsection_content.append("::: {{.panel-tabset}}\n")
438473

439-
if component.component_type == r.ComponentType.PLOT:
440-
subsection_content.extend(self._generate_plot_content(component))
441-
elif component.component_type == r.ComponentType.DATAFRAME:
442-
subsection_content.extend(self._generate_dataframe_content(component))
443-
elif (
444-
component.component_type == r.ComponentType.MARKDOWN
445-
and component.title.lower() != "description"
446-
):
447-
subsection_content.extend(self._generate_markdown_content(component))
448-
elif (
449-
component.component_type == r.ComponentType.HTML
450-
and not self.is_report_static
451-
):
452-
subsection_content.extend(self._generate_html_content(component))
453-
else:
454-
self.report.logger.warning(
455-
f"Unsupported component type '{component.component_type}' in subsection: {subsection.title}"
456-
)
474+
(
475+
all_components,
476+
subsection_imports,
477+
) = self._combine_components(subsection.components)
478+
subsection_content.extend(all_components)
457479

458480
if is_report_revealjs:
459481
subsection_content.append(":::\n")

0 commit comments

Comments
 (0)