Skip to content

Commit 1c2e358

Browse files
committed
Change return of _generate_imports method from StreamlitReportView and QuartoReportView classes from str to a list of str
1 parent ba197f9 commit 1c2e358

File tree

2 files changed

+36
-41
lines changed

2 files changed

+36
-41
lines changed

report/quarto_reportview.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nfrom itables import show\nimport 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

report/streamlit_reportview.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,17 @@ def _generate_sections(self, output_dir: str) -> None:
167167
if section.subsections:
168168
# Iterate through subsections and integrate them into the section file
169169
for subsection in section.subsections:
170-
# Create subsection content and imports for the report
171-
imports = []
172-
imports.append('import streamlit as st')
173-
174170
# Create subsection file
175171
subsection_file_path = os.path.join(output_dir, section_name_var, section_name_var + "_" + subsection.name.replace(" ", "_") + ".py")
176172

177-
# Generate content for the subsection
173+
# Generate content and imports for the subsection
178174
subsection_content, subsection_imports = self._generate_subsection(subsection)
179-
imports.extend(subsection_imports)
175+
176+
# Flatten the subsection_imports into a single list
177+
flattened_subsection_imports = [imp for sublist in subsection_imports for imp in sublist]
180178

181179
# Remove duplicated imports
182-
unique_imports = set()
183-
184-
# Split each string by newlines and update the set
185-
for imp in imports:
186-
unique_imports.update(imp.split('\n'))
180+
unique_imports = list(set(flattened_subsection_imports))
187181

188182
# Write everything to the subsection file
189183
with open(subsection_file_path, 'w') as subsection_file:
@@ -363,8 +357,8 @@ def _generate_markdown_content(self, markdown) -> List[str]:
363357
st.markdown(markdown_content, unsafe_allow_html=True)\n""")
364358

365359
return markdown_content
366-
367-
def _generate_component_imports(self, component: r.Component) -> str:
360+
361+
def _generate_component_imports(self, component: r.Component) -> List[str]:
368362
"""
369363
Generate necessary imports for a component of the report.
370364
@@ -377,28 +371,29 @@ def _generate_component_imports(self, component: r.Component) -> str:
377371
378372
Returns
379373
-------
380-
str
381-
A str of import statements for the component.
374+
list : List[str]
375+
A list of import statements for the component.
382376
"""
383377
# Dictionary to hold the imports for each component type
384378
components_imports = {
385379
'plot': {
386-
r.VisualizationTool.ALTAIR: 'import json\nimport altair as alt',
387-
r.VisualizationTool.PLOTLY: 'import json'
380+
r.VisualizationTool.ALTAIR: ['import json', 'import altair as alt'],
381+
r.VisualizationTool.PLOTLY: ['import json']
388382
},
389-
'dataframe': 'import pandas as pd'
383+
'dataframe': ['import pandas as pd']
390384
}
391-
# Iterate over sections and subsections to determine needed imports
385+
392386
component_type = component.component_type
387+
component_imports = ['import streamlit as st']
393388

394389
# Add relevant imports based on component type and visualization tool
395390
if component_type == r.ComponentType.PLOT:
396391
visualization_tool = getattr(component, 'visualization_tool', None)
397392
if visualization_tool in components_imports['plot']:
398-
return components_imports['plot'][visualization_tool]
393+
component_imports.extend(components_imports['plot'][visualization_tool])
399394

400395
elif component_type == r.ComponentType.DATAFRAME:
401-
return components_imports['dataframe']
396+
component_imports.extend(components_imports['dataframe'])
402397

403-
# If no relevant import is found, return an empty string
404-
return ''
398+
# Return the list of import statements
399+
return component_imports

0 commit comments

Comments
 (0)