Skip to content

Commit 700217a

Browse files
committed
🎨 docstring and f-string formatting
- leave out most multiline string for now (which need dedenting) - one whitespace update in tests
1 parent a38262e commit 700217a

File tree

6 files changed

+50
-40
lines changed

6 files changed

+50
-40
lines changed

src/vuegen/quarto_reportview.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def __init__(
7676

7777
def generate_report(self, output_dir: Optional[Path] = None) -> None:
7878
"""
79-
Generates the qmd file of the quarto report. It creates code for rendering each section and its subsections with all components.
79+
Generates the qmd file of the quarto report. It creates code for rendering
80+
each section and its subsections with all components.
8081
8182
Parameters
8283
----------
@@ -105,7 +106,8 @@ def generate_report(self, output_dir: Optional[Path] = None) -> None:
105106
)
106107
else:
107108
self.report.logger.info(
108-
f"Output directory for static content already existed: '{self.static_dir}'"
109+
"Output directory for static content already existed: "
110+
f"'{self.static_dir}'"
109111
)
110112

111113
try:
@@ -135,7 +137,8 @@ def generate_report(self, output_dir: Optional[Path] = None) -> None:
135137
self.report.logger.info("Starting to generate sections for the report.")
136138
for section in self.report.sections:
137139
self.report.logger.debug(
138-
f"Processing section: '{section.title}' - {len(section.subsections)} subsection(s)"
140+
f"Processing section: '{section.title}' -"
141+
f" {len(section.subsections)} subsection(s)"
139142
)
140143
# Add section header and description
141144
qmd_content.append(f"# {section.title}")
@@ -170,7 +173,8 @@ def generate_report(self, output_dir: Optional[Path] = None) -> None:
170173
# Iterate through subsections and integrate them into the section file
171174
for subsection in section.subsections:
172175
self.report.logger.debug(
173-
f"Processing subsection: '{subsection.title}' - {len(subsection.components)} component(s)"
176+
f"Processing subsection: '{subsection.title}' - "
177+
f"{len(subsection.components)} component(s)"
174178
)
175179
# Generate content for the subsection
176180
subsection_content, subsection_imports = (
@@ -185,7 +189,8 @@ def generate_report(self, output_dir: Optional[Path] = None) -> None:
185189
) # even easier as it's global
186190
else:
187191
self.report.logger.warning(
188-
f"No subsections found in section: '{section.title}'. To show content in the report, add subsections to the section."
192+
f"No subsections found in section: '{section.title}'. "
193+
"To show content in the report, add subsections to the section."
189194
)
190195
# Add globally set output folder
191196
report_imports.append("from pathlib import Path")
@@ -208,7 +213,7 @@ def generate_report(self, output_dir: Optional[Path] = None) -> None:
208213

209214
# Write the navigation and general content to a Python file
210215
fname_qmd_report = self.output_dir / f"{self.BASE_DIR}.qmd"
211-
with open(fname_qmd_report, "w") as quarto_report:
216+
with open(fname_qmd_report, "w", encoding="utf-8") as quarto_report:
212217
quarto_report.write(yaml_header)
213218
quarto_report.write(
214219
f"""\n```{{python}}
@@ -281,7 +286,8 @@ def run_report(self, output_dir: Optional[Path] = None) -> None:
281286
check=True,
282287
)
283288
self.report.logger.info(
284-
f"Converted '{self.report.title}' '{self.report_type}' report to Jupyter Notebook after execution"
289+
f"Converted '{self.report.title}' '{self.report_type}' "
290+
"report to Jupyter Notebook after execution"
285291
)
286292
self.report.logger.info(
287293
f"'{self.report.title}' '{self.report_type}' report rendered"
@@ -291,15 +297,11 @@ def run_report(self, output_dir: Optional[Path] = None) -> None:
291297
f"Error running '{self.report.title}' {self.report_type} report: {str(e)}"
292298
)
293299
raise
294-
# except FileNotFoundError as e:
295-
# self.report.logger.error(
296-
# f"Quarto is not installed. Please install Quarto to run the report: {str(e)}"
297-
# )
298-
# raise
299300

300301
def _create_yaml_header(self) -> str:
301302
"""
302-
Creates a YAML header for the Quarto report based on the specified eport type and output format.
303+
Creates a YAML header for the Quarto report based on the specified eport type
304+
and output format.
303305
304306
Returns
305307
-------
@@ -339,7 +341,7 @@ def _create_yaml_header(self) -> str:
339341
include-after-body:
340342
text: |
341343
<footer class="footer">
342-
This report was generated with
344+
This report was generated with
343345
<a href="https://github.com/Multiomics-Analytics-Group/vuegen" target="_blank">
344346
<img src="https://raw.githubusercontent.com/Multiomics-Analytics-Group/vuegen/main/docs/images/vuegen_logo.svg" alt="VueGen" width="65px">
345347
</a>
@@ -351,7 +353,7 @@ def _create_yaml_header(self) -> str:
351353
fig-align: center
352354
margin:
353355
- bottom=40mm
354-
include-in-header:
356+
include-in-header:
355357
text: |
356358
\\usepackage{scrlayer-scrpage}
357359
\\usepackage{hyperref}
@@ -386,7 +388,7 @@ def _create_yaml_header(self) -> str:
386388
include-after-body:
387389
text: |
388390
<footer class="footer">
389-
This report was generated with
391+
This report was generated with
390392
<a href="https://github.com/Multiomics-Analytics-Group/vuegen" target="_blank">
391393
<img src="https://raw.githubusercontent.com/Multiomics-Analytics-Group/vuegen/main/docs/images/vuegen_logo.svg" alt="VueGen" width="65px">
392394
</a>
@@ -417,7 +419,7 @@ def _create_yaml_header(self) -> str:
417419
include-after-body:
418420
text: |
419421
<footer class="footer">
420-
This report was generated with
422+
This report was generated with
421423
<a href="https://github.com/Multiomics-Analytics-Group/vuegen" target="_blank">
422424
<img src="../docs/images/vuegen_logo.svg" alt="VueGen" width="65px">
423425
</a>
@@ -480,8 +482,9 @@ def _generate_subsection(
480482
is_report_revealjs,
481483
) -> tuple[List[str], List[str]]:
482484
"""
483-
Generate code to render components (plots, dataframes, markdown) in the given subsection,
484-
creating imports and content for the subsection based on the component type.
485+
Generate code to render components (plots, dataframes, markdown) in the given
486+
subsection, creating imports and content for the subsection based on the component
487+
type.
485488
486489
Parameters
487490
----------
@@ -559,18 +562,16 @@ def _generate_plot_content(self, plot) -> List[str]:
559562
elif plot.plot_type == r.PlotType.PLOTLY:
560563
plot_content.append(self._generate_plot_code(plot))
561564
if self.is_report_static:
562-
plot_content.append(
563-
f"""fig_plotly.write_image("{static_plot_path.relative_to(self.output_dir).as_posix()}")\n```\n"""
564-
)
565+
fpath = static_plot_path.relative_to(self.output_dir).as_posix()
566+
plot_content.append(f"""fig_plotly.write_image("{fpath}")\n```\n""")
565567
plot_content.append(self._generate_image_content(static_plot_path))
566568
else:
567569
plot_content.append("""fig_plotly.show()\n```\n""")
568570
elif plot.plot_type == r.PlotType.ALTAIR:
569571
plot_content.append(self._generate_plot_code(plot))
570572
if self.is_report_static:
571-
plot_content.append(
572-
f"""fig_altair.save("{static_plot_path.relative_to(self.output_dir).as_posix()}")\n```\n"""
573-
)
573+
fpath = static_plot_path.relative_to(self.output_dir).as_posix()
574+
plot_content.append(f"""fig_altair.save("{fpath}")\n```\n""")
574575
plot_content.append(self._generate_image_content(static_plot_path))
575576
else:
576577
plot_content.append("""fig_altair\n```\n""")
@@ -601,7 +602,8 @@ def _generate_plot_content(self, plot) -> List[str]:
601602
self.report.logger.warning(f"Unsupported plot type: {plot.plot_type}")
602603
except Exception as e:
603604
self.report.logger.error(
604-
f"Error generating content for '{plot.plot_type}' plot '{plot.id}' '{plot.title}': {str(e)}"
605+
f"Error generating content for '{plot.plot_type}' plot '{plot.id}' "
606+
f"'{plot.title}': {str(e)}"
605607
)
606608
raise
607609

@@ -718,7 +720,8 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
718720
file_extension == fmt.value_with_dot for fmt in r.DataFrameFormat
719721
):
720722
self.report.logger.error(
721-
f"Unsupported file extension: {file_extension}. Supported extensions are: {', '.join(fmt.value for fmt in r.DataFrameFormat)}."
723+
f"Unsupported file extension: {file_extension}. Supported extensions"
724+
f" are: {', '.join(fmt.value for fmt in r.DataFrameFormat)}."
722725
)
723726

724727
# Build the file path (URL or local file)
@@ -773,7 +776,8 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
773776
)
774777
)
775778
dataframe_content.append(
776-
f"df = pd.{read_function.__name__}(report_dir / '{df_file_path}', "
779+
f"df = pd.{read_function.__name__}"
780+
f"(report_dir / '{df_file_path}', "
777781
f"sheet_name='{sheet_name}')\n"
778782
)
779783
# Display the dataframe
@@ -783,7 +787,8 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
783787

784788
except Exception as e:
785789
self.report.logger.error(
786-
f"Error generating content for DataFrame: {dataframe.title}. Error: {str(e)}"
790+
f"Error generating content for DataFrame: {dataframe.title}. "
791+
f"Error: {str(e)}"
787792
)
788793
raise
789794
# Add caption if available
@@ -851,7 +856,8 @@ def _generate_markdown_content(self, markdown) -> List[str]:
851856

852857
except Exception as e:
853858
self.report.logger.error(
854-
f"Error generating content for Markdown: {markdown.title}. Error: {str(e)}"
859+
f"Error generating content for Markdown: {markdown.title}. "
860+
f"Error: {str(e)}"
855861
)
856862
raise
857863

@@ -866,7 +872,8 @@ def _generate_markdown_content(self, markdown) -> List[str]:
866872

867873
def _show_dataframe(self, dataframe, suffix: Optional[str] = None) -> List[str]:
868874
"""
869-
Appends either a static image or an interactive representation of a DataFrame to the content list.
875+
Appends either a static image or an interactive representation of a DataFrame
876+
to the content list.
870877
871878
Parameters
872879
----------
@@ -902,7 +909,7 @@ def _show_dataframe(self, dataframe, suffix: Optional[str] = None) -> List[str]:
902909
else:
903910
# Append code to display the DataFrame interactively
904911
dataframe_content.append(
905-
"""show(df, classes="display nowrap compact", lengthMenu=[3, 5, 10])\n```\n"""
912+
'show(df, classes="display nowrap compact", lengthMenu=[3, 5, 10])\n```\n'
906913
)
907914

908915
return dataframe_content
@@ -914,7 +921,8 @@ def _generate_html_content(self, html) -> List[str]:
914921
Parameters
915922
----------
916923
html : Html
917-
The HTML component to add to the report. This could be a local file path or a URL.
924+
The HTML component to add to the report. This could be a local file path
925+
or a URL.
918926
919927
Returns
920928
-------
@@ -955,7 +963,8 @@ def _generate_image_content(
955963
self, image_path: str, alt_text: str = "", width: str = "90%"
956964
) -> str:
957965
"""
958-
Adds an image to the content list in an HTML format with a specified width and height.
966+
Adds an image to the content list in an HTML format with a specified width
967+
and height.
959968
960969
Parameters
961970
----------
@@ -989,7 +998,8 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
989998
Parameters
990999
----------
9911000
component : r.Component
992-
The component for which to generate the required imports. The component can be of type:
1001+
The component for which to generate the required imports.
1002+
The component can be of type:
9931003
- PLOT
9941004
- DATAFRAME
9951005
- MARKDOWN

tests/report_examples/Basic_example_vuegen_demo_notebook/html/quarto_report/quarto_report.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ include-in-header:
2626
include-after-body:
2727
text: |
2828
<footer class="footer">
29-
This report was generated with
29+
This report was generated with
3030
<a href="https://github.com/Multiomics-Analytics-Group/vuegen" target="_blank">
3131
<img src="https://raw.githubusercontent.com/Multiomics-Analytics-Group/vuegen/main/docs/images/vuegen_logo.svg" alt="VueGen" width="65px">
3232
</a>

tests/report_examples/Basic_example_vuegen_demo_notebook/jupyter/quarto_report/quarto_report.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ include-in-header:
2626
include-after-body:
2727
text: |
2828
<footer class="footer">
29-
This report was generated with
29+
This report was generated with
3030
<a href="https://github.com/Multiomics-Analytics-Group/vuegen" target="_blank">
3131
<img src="../docs/images/vuegen_logo.svg" alt="VueGen" width="65px">
3232
</a>

tests/report_examples/Basic_example_vuegen_demo_notebook/pdf/quarto_report/quarto_report.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ format:
1111
fig-align: center
1212
margin:
1313
- bottom=40mm
14-
include-in-header:
14+
include-in-header:
1515
text: |
1616
\usepackage{scrlayer-scrpage}
1717
\usepackage{hyperref}

tests/report_examples/Basic_example_vuegen_demo_notebook/revealjs/quarto_report/quarto_report.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ include-in-header:
2727
include-after-body:
2828
text: |
2929
<footer class="footer">
30-
This report was generated with
30+
This report was generated with
3131
<a href="https://github.com/Multiomics-Analytics-Group/vuegen" target="_blank">
3232
<img src="https://raw.githubusercontent.com/Multiomics-Analytics-Group/vuegen/main/docs/images/vuegen_logo.svg" alt="VueGen" width="65px">
3333
</a>

tests/report_examples/Basic_example_vuegen_demo_notebook_cfg/html/quarto_report/quarto_report.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ include-in-header:
2626
include-after-body:
2727
text: |
2828
<footer class="footer">
29-
This report was generated with
29+
This report was generated with
3030
<a href="https://github.com/Multiomics-Analytics-Group/vuegen" target="_blank">
3131
<img src="https://raw.githubusercontent.com/Multiomics-Analytics-Group/vuegen/main/docs/images/vuegen_logo.svg" alt="VueGen" width="65px">
3232
</a>

0 commit comments

Comments
 (0)