Skip to content

Commit 9ae1074

Browse files
committed
🐛 set default static dir only on main methods, pass parameter on other hidden methods
- will allow to set static dir on app or cli
1 parent 830b557 commit 9ae1074

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

src/vuegen/quarto_reportview.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ def generate_report(
113113
# Generate content for the subsection
114114
subsection_content, subsection_imports = (
115115
self._generate_subsection(
116-
subsection, is_report_static, is_report_revealjs
116+
subsection,
117+
is_report_static,
118+
is_report_revealjs,
119+
static_dir=static_dir,
117120
)
118121
)
119122
qmd_content.extend(subsection_content)
@@ -352,7 +355,11 @@ def _create_yaml_header(self) -> str:
352355
return yaml_header
353356

354357
def _generate_subsection(
355-
self, subsection, is_report_static, is_report_revealjs
358+
self,
359+
subsection,
360+
is_report_static,
361+
is_report_revealjs,
362+
static_dir: str,
356363
) -> tuple[List[str], List[str]]:
357364
"""
358365
Generate code to render components (plots, dataframes, markdown) in the given subsection,
@@ -366,6 +373,8 @@ def _generate_subsection(
366373
A boolean indicating whether the report is static or interactive.
367374
is_report_revealjs : bool
368375
A boolean indicating whether the report is in revealjs format.
376+
static_dir : str
377+
The folder where the static files will be saved.
369378
Returns
370379
-------
371380
tuple : (List[str], List[str])
@@ -389,11 +398,15 @@ def _generate_subsection(
389398

390399
if component.component_type == r.ComponentType.PLOT:
391400
subsection_content.extend(
392-
self._generate_plot_content(component, is_report_static)
401+
self._generate_plot_content(
402+
component, is_report_static, static_dir=static_dir
403+
)
393404
)
394405
elif component.component_type == r.ComponentType.DATAFRAME:
395406
subsection_content.extend(
396-
self._generate_dataframe_content(component, is_report_static)
407+
self._generate_dataframe_content(
408+
component, is_report_static, static_dir=static_dir
409+
)
397410
)
398411
elif (
399412
component.component_type == r.ComponentType.MARKDOWN
@@ -419,7 +432,7 @@ def _generate_subsection(
419432
return subsection_content, subsection_imports
420433

421434
def _generate_plot_content(
422-
self, plot, is_report_static, static_dir: str = STATIC_FILES_DIR
435+
self, plot, is_report_static, static_dir: str
423436
) -> List[str]:
424437
"""
425438
Generate content for a plot component based on the report type.
@@ -428,8 +441,8 @@ def _generate_plot_content(
428441
----------
429442
plot : Plot
430443
The plot component to generate content for.
431-
static_dir : str, optional
432-
The folder where the static files will be saved (default is STATIC_FILES_DIR).
444+
static_dir : str
445+
The folder where the static files will be saved.
433446
434447
Returns
435448
-------
@@ -561,7 +574,9 @@ def _generate_plot_code(self, plot, output_file="") -> str:
561574
</div>\n"""
562575
return plot_code
563576

564-
def _generate_dataframe_content(self, dataframe, is_report_static) -> List[str]:
577+
def _generate_dataframe_content(
578+
self, dataframe, is_report_static, static_dir: str
579+
) -> List[str]:
565580
"""
566581
Generate content for a DataFrame component based on the report type.
567582
@@ -571,6 +586,8 @@ def _generate_dataframe_content(self, dataframe, is_report_static) -> List[str]:
571586
The dataframe component to add to content.
572587
is_report_static : bool
573588
A boolean indicating whether the report is static or interactive.
589+
static_dir : str
590+
The folder where the static files will be saved.
574591
575592
Returns
576593
-------
@@ -620,7 +637,9 @@ def _generate_dataframe_content(self, dataframe, is_report_static) -> List[str]:
620637
)
621638

622639
# Display the dataframe
623-
dataframe_content.extend(self._show_dataframe(dataframe, is_report_static))
640+
dataframe_content.extend(
641+
self._show_dataframe(dataframe, is_report_static, static_dir=static_dir)
642+
)
624643

625644
except Exception as e:
626645
self.report.logger.error(
@@ -772,7 +791,7 @@ def _generate_image_content(
772791
)
773792

774793
def _show_dataframe(
775-
self, dataframe, is_report_static, static_dir: str = STATIC_FILES_DIR
794+
self, dataframe, is_report_static, static_dir: str
776795
) -> List[str]:
777796
"""
778797
Appends either a static image or an interactive representation of a DataFrame to the content list.
@@ -783,8 +802,8 @@ def _show_dataframe(
783802
The DataFrame object containing the data to display.
784803
is_report_static : bool
785804
Determines if the report is in a static format (e.g., PDF) or interactive (e.g., HTML).
786-
static_dir : str, optional
787-
The folder where the static files will be saved (default is STATIC_FILES_DIR).
805+
static_dir : str
806+
The folder where the static files will be saved.
788807
789808
Returns
790809
-------

src/vuegen/streamlit_reportview.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def generate_report(
144144
)
145145

146146
# Create Python files for each section and its subsections and plots
147-
self._generate_sections(output_dir=output_dir)
147+
self._generate_sections(output_dir=output_dir, static_dir=static_dir)
148148
except Exception as e:
149149
self.report.logger.error(
150150
f"An error occurred while generating the report: {str(e)}"
@@ -308,14 +308,16 @@ def _generate_home_section(
308308
self.report.logger.error(f"Error generating the home section: {str(e)}")
309309
raise
310310

311-
def _generate_sections(self, output_dir: str) -> None:
311+
def _generate_sections(self, output_dir: str, static_dir: str) -> None:
312312
"""
313313
Generates Python files for each section in the report, including subsections and its components (plots, dataframes, markdown).
314314
315315
Parameters
316316
----------
317317
output_dir : str
318318
The folder where section files will be saved.
319+
static_dir : str
320+
The folder where the static files will be saved.
319321
"""
320322
self.report.logger.info("Starting to generate sections for the report.")
321323

@@ -342,7 +344,9 @@ def _generate_sections(self, output_dir: str) -> None:
342344

343345
# Generate content and imports for the subsection
344346
subsection_content, subsection_imports = (
345-
self._generate_subsection(subsection)
347+
self._generate_subsection(
348+
subsection, static_dir=static_dir
349+
)
346350
)
347351

348352
# Flatten the subsection_imports into a single list
@@ -379,7 +383,9 @@ def _generate_sections(self, output_dir: str) -> None:
379383
self.report.logger.error(f"Error generating sections: {str(e)}")
380384
raise
381385

382-
def _generate_subsection(self, subsection) -> tuple[List[str], List[str]]:
386+
def _generate_subsection(
387+
self, subsection, static_dir
388+
) -> tuple[List[str], List[str]]:
383389
"""
384390
Generate code to render components (plots, dataframes, markdown) in the given subsection,
385391
creating imports and content for the subsection based on the component type.
@@ -388,6 +394,8 @@ def _generate_subsection(self, subsection) -> tuple[List[str], List[str]]:
388394
----------
389395
subsection : Subsection
390396
The subsection containing the components.
397+
static_dir : str
398+
The folder where the static files will be saved.
391399
392400
Returns
393401
-------
@@ -416,7 +424,9 @@ def _generate_subsection(self, subsection) -> tuple[List[str], List[str]]:
416424

417425
# Handle different types of components
418426
if component.component_type == r.ComponentType.PLOT:
419-
subsection_content.extend(self._generate_plot_content(component))
427+
subsection_content.extend(
428+
self._generate_plot_content(component, static_dir=static_dir)
429+
)
420430
elif component.component_type == r.ComponentType.DATAFRAME:
421431
subsection_content.extend(self._generate_dataframe_content(component))
422432
# If md files is called "description.md", do not include it in the report
@@ -445,9 +455,7 @@ def _generate_subsection(self, subsection) -> tuple[List[str], List[str]]:
445455
)
446456
return subsection_content, subsection_imports
447457

448-
def _generate_plot_content(
449-
self, plot, static_dir: str = STATIC_FILES_DIR
450-
) -> List[str]:
458+
def _generate_plot_content(self, plot, static_dir: str) -> List[str]:
451459
"""
452460
Generate content for a plot component based on the plot type (static or interactive).
453461
@@ -460,8 +468,8 @@ def _generate_plot_content(
460468
-------
461469
list : List[str]
462470
The list of content lines for the plot.
463-
static_dir : str, optional
464-
The folder where the static files will be saved (default is STATIC_FILES_DIR).
471+
static_dir : str
472+
The folder where the static files will be saved.
465473
"""
466474
plot_content = []
467475
# Add title

0 commit comments

Comments
 (0)