@@ -74,18 +74,19 @@ def generate_report(self, output_dir: str = 'quarto_report/') -> None:
7474 qmd_content .extend (subsection_content )
7575 report_imports .extend (subsection_imports )
7676
77+ # Flatten the subsection_imports into a single list
78+ flattened_report_imports = [imp for sublist in report_imports for imp in sublist ]
79+
7780 # Remove duplicated imports
78- report_unique_imports = set ()
79- for imp in report_imports :
80- report_unique_imports .update (imp .split ('\n ' ))
81+ report_unique_imports = list (set (flattened_report_imports ))
8182
8283 # Format imports
8384 report_formatted_imports = "\n " .join (report_unique_imports )
8485
8586 # Write the navigation and general content to a Python file
8687 with open (os .path .join (output_dir , "quarto_report.qmd" ), 'w' ) as quarto_report :
8788 quarto_report .write (yaml_header )
88- quarto_report .write (f"""```{{python}}
89+ quarto_report .write (f"""\n ```{{python}}
8990#| label: 'Imports'
9091#| echo: false
9192{ report_formatted_imports }
@@ -430,7 +431,7 @@ def _show_dataframe(self, dataframe, is_report_static) -> List[str]:
430431
431432 return dataframe_content
432433
433- def _generate_component_imports (self , component : r .Component ) -> str :
434+ def _generate_component_imports (self , component : r .Component ) -> List [ str ] :
434435 """
435436 Generate necessary imports for a component of the report.
436437
@@ -444,33 +445,32 @@ def _generate_component_imports(self, component: r.Component) -> str:
444445
445446 Returns
446447 -------
447- str
448- A str of import statements for the component.
448+ list : List[ str]
449+ A list of import statements for the component.
449450 """
450451 # Dictionary to hold the imports for each component type
451452 components_imports = {
452453 'plot' : {
453- r .VisualizationTool .ALTAIR : 'import altair as alt' ,
454- r .VisualizationTool .PLOTLY : 'import plotly.io as pio'
454+ r .VisualizationTool .ALTAIR : [ 'import altair as alt' ] ,
455+ r .VisualizationTool .PLOTLY : [ 'import plotly.io as pio' ]
455456 },
456- 'dataframe' : 'import pandas as pd\n from itables import show\n import dataframe_image as dfi' ,
457- 'markdown' : 'import IPython.display as display'
457+ 'dataframe' : [ 'import pandas as pd' , 'from itables import show' , 'import dataframe_image as dfi'] ,
458+ 'markdown' : [ 'import IPython.display as display' ]
458459 }
459460
460461 # Iterate over sections and subsections to determine needed imports
461462 component_type = component .component_type
463+ component_imports = []
462464
463465 # Add relevant imports based on component type and visualization tool
464466 if component_type == r .ComponentType .PLOT :
465467 visualization_tool = getattr (component , 'visualization_tool' , None )
466468 if visualization_tool in components_imports ['plot' ]:
467- return components_imports ['plot' ][visualization_tool ]
468-
469+ component_imports .extend (components_imports ['plot' ][visualization_tool ])
469470 elif component_type == r .ComponentType .DATAFRAME :
470- return components_imports ['dataframe' ]
471-
471+ component_imports .extend (components_imports ['dataframe' ])
472472 elif component_type == r .ComponentType .MARKDOWN :
473- return components_imports ['markdown' ]
473+ component_imports . extend ( components_imports ['markdown' ])
474474
475- # If no relevant import is found, return an empty string
476- return ''
475+ # Return the list of import statements
476+ return component_imports
0 commit comments