33import report as r
44from enum import StrEnum , auto
55from typing import List , Optional
6+ from helpers .utils import create_folder
67
78class ReportFormat (StrEnum ):
89 HTML = auto ()
@@ -26,21 +27,31 @@ def __init__(self, id: int, name: str, report: r.Report, report_type: r.ReportTy
2627 super ().__init__ (id , name = name , report = report , report_type = report_type , columns = columns )
2728 self .report_format = report_format
2829
29- def generate_report (self , output_dir : str = BASE_DIR ) -> None :
30+ def generate_report (self , output_dir : str = BASE_DIR , static_dir : str = STATIC_FILES_DIR ) -> None :
3031 """
3132 Generates the qmd file of the quarto report. It creates code for rendering each section and its subsections with all components.
3233
3334 Parameters
3435 ----------
3536 output_dir : str, optional
3637 The folder where the generated report files will be saved (default is BASE_DIR).
38+ static_dir : str, optional
39+ The folder where the static files will be saved (default is STATIC_FILES_DIR).
3740 """
3841 self .report .logger .debug (f"Generating '{ self .report_type } ' report with '{ self .report_format } ' format in directory: '{ output_dir } '" )
3942
40- # Create the output folder if it does not exist
41- if not os .path .exists (output_dir ):
42- os .mkdir (output_dir )
43- self .report .logger .debug (f"Created output directory: { output_dir } " )
43+ # Create the output folder
44+ if create_folder (output_dir ):
45+ self .report .logger .debug (f"Created output directory: '{ output_dir } '" )
46+ else :
47+ self .report .logger .debug (f"Output directory already existed: '{ output_dir } '" )
48+
49+ # Create the static folder
50+ if create_folder (static_dir ):
51+ self .report .logger .info (f"Created output directory for static content: '{ static_dir } '" )
52+ else :
53+ self .report .logger .info (f"Output directory for static content already existed: '{ static_dir } '" )
54+
4455 try :
4556 # Create variable to check if the report is static or revealjs
4657 is_report_static = self .report_format in {ReportFormat .PDF , ReportFormat .DOCX , ReportFormat .ODT , ReportFormat .PPTX }
@@ -230,37 +241,31 @@ def _generate_subsection(self, subsection, is_report_static, is_report_revealjs)
230241 self .report .logger .info (f"Generated content and imports for subsection: '{ subsection .name } '" )
231242 return subsection_content , subsection_imports
232243
233- def _generate_plot_content (self , plot , is_report_static , output_dir : str = STATIC_FILES_DIR ) -> List [str ]:
244+ def _generate_plot_content (self , plot , is_report_static , static_dir : str = STATIC_FILES_DIR ) -> List [str ]:
234245 """
235246 Generate content for a plot component based on the report type.
236247
237248 Parameters
238249 ----------
239250 plot : Plot
240251 The plot component to generate content for.
241- is_report_static : bool
242- A boolean indicating whether the report is static or interactive .
252+ static_dir : str, optional
253+ The folder where the static files will be saved (default is STATIC_FILES_DIR) .
243254
244255 Returns
245256 -------
246257 list : List[str]
247258 The list of content lines for the plot.
248- output_dir : str, optional
249- The folder where the static files will be saved (default is STATIC_FILES_DIR).
250259 """
251- # Create the output folder if it does not exist
252- if not os .path .exists (output_dir ):
253- os .mkdir (output_dir )
254-
255260 plot_content = []
256261 plot_content .append (f'### { plot .title } ' )
257262 if plot .plot_type == r .PlotType .INTERACTIVE :
258263 try :
259264 # Define plot path
260265 if is_report_static :
261- static_plot_path = os .path .join (output_dir , f"{ plot .name .replace (' ' , '_' )} .png" )
266+ static_plot_path = os .path .join (static_dir , f"{ plot .name .replace (' ' , '_' )} .png" )
262267 else :
263- html_plot_file = os .path .join (output_dir , f"{ plot .name .replace (' ' , '_' )} .html" )
268+ html_plot_file = os .path .join (static_dir , f"{ plot .name .replace (' ' , '_' )} .html" )
264269
265270 if plot .int_visualization_tool == r .IntVisualizationTool .PLOTLY :
266271 plot_content .append (self ._generate_plot_code (plot ))
@@ -447,7 +452,7 @@ def _generate_image_content(self, image_path: str, alt_text: str = "", width: in
447452 return f"""
448453} ){{ width={ width } px height={ height } px fig-align="center"}}\n """
449454
450- def _show_dataframe (self , dataframe , is_report_static , output_dir : str = STATIC_FILES_DIR ) -> List [str ]:
455+ def _show_dataframe (self , dataframe , is_report_static , static_dir : str = STATIC_FILES_DIR ) -> List [str ]:
451456 """
452457 Appends either a static image or an interactive representation of a DataFrame to the content list.
453458
@@ -457,18 +462,18 @@ def _show_dataframe(self, dataframe, is_report_static, output_dir: str = STATIC_
457462 The DataFrame object containing the data to display.
458463 is_report_static : bool
459464 Determines if the report is in a static format (e.g., PDF) or interactive (e.g., HTML).
465+ static_dir : str, optional
466+ The folder where the static files will be saved (default is STATIC_FILES_DIR).
460467
461468 Returns
462469 -------
463470 list : List[str]
464471 The list of content lines for the DataFrame.
465- output_dir : str, optional
466- The folder where the static files will be saved (default is STATIC_FILES_DIR).
467472 """
468473 dataframe_content = []
469474 if is_report_static :
470475 # Generate path for the DataFrame image
471- df_image = os .path .join (output_dir , f"{ dataframe .name .replace (' ' , '_' )} .png" )
476+ df_image = os .path .join (static_dir , f"{ dataframe .name .replace (' ' , '_' )} .png" )
472477 dataframe_content .append (f"dfi.export(df, '{ os .path .join ('..' , df_image )} ', max_rows=10, max_cols=5)\n ```\n " )
473478 # Use helper method to add centered image content
474479 dataframe_content .append (self ._generate_image_content (df_image , dataframe .name ))
0 commit comments