Skip to content

Commit 6c87855

Browse files
committed
Create BASE_DIR and STATIC_FILES_DIR global variables to define paths and include all static outputs into a specific folder in the streamlit and quart reports
1 parent 94ad77f commit 6c87855

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

report/quarto_reportview.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,24 @@ class ReportFormat(StrEnum):
1616
class QuartoReportView(r.ReportView):
1717
"""
1818
A ReportView subclass for generating Quarto reports.
19-
20-
Methods
21-
-------
22-
generate_report(output_dir)
23-
Generates the qmd file of the quarto report. It creates code for rendering each section and its subsections with all components.
24-
run_report(output_dir)
25-
Runs the generated quarto report.
2619
"""
2720

28-
def __init__(self, identifier: int, name: str, report: r.Report, report_type: r.ReportType,
21+
BASE_DIR = 'quarto_report'
22+
STATIC_FILES_DIR = os.path.join(BASE_DIR, 'static')
23+
24+
def __init__(self, id: int, name: str, report: r.Report, report_type: r.ReportType,
2925
columns: Optional[List[str]], report_format: ReportFormat):
30-
super().__init__(identifier, name=name, report=report, report_type = report_type, columns=columns)
26+
super().__init__(id, name=name, report=report, report_type = report_type, columns=columns)
3127
self.report_format = report_format
3228

33-
def generate_report(self, output_dir: str = 'quarto_report/') -> None:
29+
def generate_report(self, output_dir: str = BASE_DIR) -> None:
3430
"""
3531
Generates the qmd file of the quarto report. It creates code for rendering each section and its subsections with all components.
3632
3733
Parameters
3834
----------
3935
output_dir : str, optional
40-
The folder where the generated report files will be saved (default is 'quarto_report/').
36+
The folder where the generated report files will be saved (default is BASE_DIR).
4137
"""
4238
# Create the output folder if it does not exist
4339
if not os.path.exists(output_dir):
@@ -93,7 +89,7 @@ def generate_report(self, output_dir: str = 'quarto_report/') -> None:
9389
```\n\n""")
9490
quarto_report.write("\n".join(qmd_content))
9591

96-
def run_report(self, output_dir: str = 'quarto_report') -> None:
92+
def run_report(self, output_dir: str = BASE_DIR) -> None:
9793
"""
9894
Runs the generated quarto report.
9995
@@ -216,7 +212,7 @@ def _generate_subsection(self, subsection, is_report_static, is_report_revealjs)
216212

217213
return subsection_content, subsection_imports
218214

219-
def _generate_plot_content(self, plot, is_report_static) -> List[str]:
215+
def _generate_plot_content(self, plot, is_report_static, output_dir: str = STATIC_FILES_DIR) -> List[str]:
220216
"""
221217
Generate content for a plot component based on the report type.
222218
@@ -231,15 +227,23 @@ def _generate_plot_content(self, plot, is_report_static) -> List[str]:
231227
-------
232228
list : List[str]
233229
The list of content lines for the plot.
230+
output_dir : str, optional
231+
The folder where the static files will be saved (default is STATIC_FILES_DIR).
234232
"""
233+
# Create the output folder if it does not exist
234+
if not os.path.exists(output_dir):
235+
os.mkdir(output_dir)
236+
235237
plot_content = []
236238
plot_content.append(f'### {plot.title}')
237239
if plot.plot_type == r.PlotType.INTERACTIVE:
238240
# Define plot path
239241
if is_report_static:
240-
static_plot_path = f"quarto_report/{plot.name.replace(' ', '_')}.png"
242+
#static_plot_path = f"{output_dir}/{plot.name.replace(' ', '_')}.png"
243+
static_plot_path = os.path.join(output_dir, f"{plot.name.replace(' ', '_')}.png")
241244
else:
242-
html_plot_file = f"quarto_report/{plot.name.replace(' ', '_')}.html"
245+
#html_plot_file = f"{output_dir}/{plot.name.replace(' ', '_')}.html"
246+
html_plot_file = os.path.join(output_dir, f"{plot.name.replace(' ', '_')}.html")
243247

244248
if plot.int_visualization_tool == r.IntVisualizationTool.PLOTLY:
245249
plot_content.append(self._generate_plot_code(plot))
@@ -402,7 +406,7 @@ def _generate_image_content(self, image_path: str, alt_text: str = "", width: in
402406
return f"""
403407
![{alt_text}]({os.path.join('..', image_path)}){{ width={width}px height={height}px fig-align="center"}}\n"""
404408

405-
def _show_dataframe(self, dataframe, is_report_static) -> List[str]:
409+
def _show_dataframe(self, dataframe, is_report_static, output_dir: str = STATIC_FILES_DIR) -> List[str]:
406410
"""
407411
Appends either a static image or an interactive representation of a DataFrame to the content list.
408412
@@ -417,11 +421,14 @@ def _show_dataframe(self, dataframe, is_report_static) -> List[str]:
417421
-------
418422
list : List[str]
419423
The list of content lines for the DataFrame.
424+
output_dir : str, optional
425+
The folder where the static files will be saved (default is STATIC_FILES_DIR).
420426
"""
421427
dataframe_content = []
422428
if is_report_static:
423429
# Generate path for the DataFrame image
424-
df_image = f"quarto_report/{dataframe.name.replace(' ', '_')}.png"
430+
#df_image = f"quarto_report/{dataframe.name.replace(' ', '_')}.png"
431+
df_image = os.path.join(output_dir, f"{dataframe.name.replace(' ', '_')}.png")
425432
dataframe_content.append(f"dfi.export(df, '{os.path.join('..', df_image)}', max_rows=10, max_cols=5)\n```\n")
426433
# Use helper method to add centered image content
427434
dataframe_content.append(self._generate_image_content(df_image, dataframe.name))

report/streamlit_reportview.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,24 @@
66

77
class StreamlitReportView(r.WebAppReportView):
88
"""
9-
A Streamlit-based implementation of a report view interface.
10-
11-
Methods
12-
-------
13-
generate_report(output_dir)
14-
Generates the Streamlit report and saves the report files to the specified folder.
15-
run_report(output_dir)
16-
Runs the generated Streamlit report.
17-
_fornat_text(text, type, level, color)
18-
Generates a Streamlit markdown text string with the specified type (header, paragraph), level and color.
19-
_generate_home_section(output_dir, report_manag_content)
20-
Generates the homepage for the report and updates the report manager content.
21-
_generate_sections(output_dir)
22-
Generates Python files for each section in the report, including subsections and its components (plots, dataframes, markdown).
23-
_generate_subsection(subsection, imports_written, content)
24-
Creates components (plots, dataframes, markdown, etc) for a given subsection.
9+
A Streamlit-based implementation of the WebAppReportView abstract base class.
2510
"""
2611

27-
def __init__(self, identifier: int, name: str, report: r.Report, report_type: r.ReportType, columns: Optional[List[str]]):
28-
super().__init__(identifier, name=name, report=report, report_type = r.ReportType.STREAMLIT, columns=columns)
12+
BASE_DIR = 'streamlit_report'
13+
SECTIONS_DIR = os.path.join(BASE_DIR, 'sections')
14+
STATIC_FILES_DIR = os.path.join(BASE_DIR, 'static')
15+
16+
def __init__(self, id: int, name: str, report: r.Report, report_type: r.ReportType, columns: Optional[List[str]]):
17+
super().__init__(id, name=name, report=report, report_type = report_type, columns=columns)
2918

30-
def generate_report(self, output_dir: str = 'streamlit_report/sections') -> None:
19+
def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
3120
"""
3221
Generates the Streamlit report and creates Python files for each section and its subsections and plots.
3322
3423
Parameters
3524
----------
3625
output_dir : str, optional
37-
The folder where the generated report files will be saved (default is 'streamlit_report/sections').
26+
The folder where the generated report files will be saved (default is SECTIONS_DIR).
3827
"""
3928
# Create the output folder if it does not exist
4029
if not os.path.exists(output_dir):
@@ -82,14 +71,14 @@ def generate_report(self, output_dir: str = 'streamlit_report/sections') -> None
8271
# Create Python files for each section and its subsections and plots
8372
self._generate_sections(output_dir=output_dir)
8473

85-
def run_report(self, output_dir: str = 'sections') -> None:
74+
def run_report(self, output_dir: str = SECTIONS_DIR) -> None:
8675
"""
8776
Runs the generated Streamlit report.
8877
8978
Parameters
9079
----------
9180
output_dir : str, optional
92-
The folder where the report was generated (default is 'sections').
81+
The folder where the report was generated (default is SECTIONS_DIR).
9382
"""
9483
sys.argv = ["streamlit", "run", os.path.join(output_dir, "report_manager.py")]
9584
sys.exit(stcli.main())
@@ -224,7 +213,7 @@ def _generate_subsection(self, subsection) -> tuple[List[str], List[str]]:
224213
subsection_content.extend(self._generate_markdown_content(component))
225214
return subsection_content, subsection_imports
226215

227-
def _generate_plot_content(self, plot) -> List[str]:
216+
def _generate_plot_content(self, plot, output_dir: str = STATIC_FILES_DIR) -> List[str]:
228217
"""
229218
Generate content for a plot component based on the plot type (static or interactive).
230219
@@ -237,7 +226,13 @@ def _generate_plot_content(self, plot) -> List[str]:
237226
-------
238227
list : List[str]
239228
The list of content lines for the plot.
229+
output_dir : str, optional
230+
The folder where the static files will be saved (default is STATIC_FILES_DIR).
240231
"""
232+
# Create the output folder if it does not exist
233+
if not os.path.exists(output_dir):
234+
os.mkdir(output_dir)
235+
241236
plot_content = []
242237
plot_content.append(self._format_text(text=plot.title, type='header', level=4, color='#2b8cbe'))
243238

@@ -250,7 +245,8 @@ def _generate_plot_content(self, plot) -> List[str]:
250245
elif plot.int_visualization_tool == r.IntVisualizationTool.PYVIS:
251246
# For PyVis, handle the network visualization
252247
G = plot.read_network()
253-
html_plot_file = f"streamlit_report/{plot.name.replace(' ', '_')}.html"
248+
#html_plot_file = f"{output_dir}/{plot.name.replace(' ', '_')}.html"
249+
html_plot_file = os.path.join(output_dir, f"{plot.name.replace(' ', '_')}.html")
254250
net = plot.create_and_save_pyvis_network(G, html_plot_file)
255251
num_nodes = len(net.nodes)
256252
num_edges = len(net.edges)

0 commit comments

Comments
 (0)