Skip to content

Commit 9e8f17b

Browse files
committed
✨ add components to streamlit home site
- first section is home section recording it's compents - needs to be treated separately
1 parent fd802ae commit 9e8f17b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/vuegen/config_manager.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,27 @@ def create_yamlconfig_fromdir(
301301
# Sort sections by their number prefix
302302
sorted_sections = self._sort_paths_by_numprefix(list(base_dir_path.iterdir()))
303303

304+
main_section_config = {
305+
"title": self._create_title_fromdir("home_components"),
306+
"description": "Components added to homepage.",
307+
"components": [],
308+
}
309+
yaml_config["sections"].append(main_section_config)
310+
304311
# Generate sections and subsections config
305312
for section_dir in sorted_sections:
306313
if section_dir.is_dir():
307314
yaml_config["sections"].append(
308315
self._create_sect_config_fromdir(section_dir)
309316
)
317+
# could be single plots?
318+
else:
319+
file_in_main_section_dir = section_dir
320+
component_config = self._create_component_config_fromfile(
321+
file_in_main_section_dir
322+
)
323+
if component_config is not None:
324+
main_section_config["components"].append(component_config)
310325

311326
return yaml_config, base_dir_path
312327

@@ -372,6 +387,10 @@ def _create_section(self, section_data: dict) -> r.Section:
372387
description=section_data.get("description"),
373388
)
374389

390+
for component_data in section_data.get("components", []):
391+
component = self._create_component(component_data)
392+
section.components.append(component)
393+
375394
# Create subsections
376395
for subsection_data in section_data.get("subsections", []):
377396
subsection = self._create_subsection(subsection_data)

src/vuegen/report.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ class Section:
718718
id: int = field(init=False)
719719
title: str
720720
subsections: List["Subsection"] = field(default_factory=list)
721+
components: List["Component"] = field(default_factory=list)
721722
description: Optional[str] = None
722723

723724
def __post_init__(self):

src/vuegen/streamlit_reportview.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,14 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
127127
report_manag_content.append("\nsections_pages = {}")
128128

129129
# Generate the home page and update the report manager content
130+
# ! top level files (compontents) are added to the home page
130131
self._generate_home_section(
131-
output_dir=output_dir, report_manag_content=report_manag_content
132+
output_dir=output_dir,
133+
report_manag_content=report_manag_content,
134+
home_section=self.report.sections[0],
132135
)
133136

134-
for section in self.report.sections:
137+
for section in self.report.sections[1:]: # skip home section components
135138
# Create a folder for each section
136139
subsection_page_vars = []
137140
section_name_var = section.title.replace(" ", "_")
@@ -293,7 +296,10 @@ def _format_text(
293296
return f"""st.markdown('''<{tag} style='text-align: {text_align}; color: {color};'>{text}</{tag}>''', unsafe_allow_html=True)"""
294297

295298
def _generate_home_section(
296-
self, output_dir: str, report_manag_content: list
299+
self,
300+
output_dir: str,
301+
report_manag_content: list,
302+
home_section: r.Section,
297303
) -> None:
298304
"""
299305
Generates the homepage for the report and updates the report manager content.
@@ -306,6 +312,13 @@ def _generate_home_section(
306312
A list to store the content that will be written to the report manager file.
307313
"""
308314
self.report.logger.debug("Processing home section.")
315+
all_components = []
316+
subsection_imports = []
317+
if home_section.components:
318+
# some assert on title?
319+
all_components, subsection_imports, _ = self._combine_components(
320+
home_section.components
321+
)
309322

310323
try:
311324
# Create folder for the home page
@@ -320,6 +333,8 @@ def _generate_home_section(
320333
# Create the home page content
321334
home_content = []
322335
home_content.append(f"import streamlit as st")
336+
if subsection_imports:
337+
home_content.extend(subsection_imports)
323338
if self.report.description:
324339
home_content.append(
325340
self._format_text(text=self.report.description, type="paragraph")
@@ -329,6 +344,10 @@ def _generate_home_section(
329344
f"\nst.image('{self.report.graphical_abstract}', use_column_width=True)"
330345
)
331346

347+
# add components content to page (if any)
348+
if all_components:
349+
home_content.extend(all_components)
350+
332351
# Define the footer variable and add it to the home page content
333352
home_content.append("footer = '''" + generate_footer() + "'''\n")
334353
home_content.append("st.markdown(footer, unsafe_allow_html=True)\n")
@@ -361,7 +380,7 @@ def _generate_sections(self, output_dir: str) -> None:
361380
self.report.logger.info("Starting to generate sections for the report.")
362381

363382
try:
364-
for section in self.report.sections:
383+
for section in self.report.sections[1:]:
365384
section_name_var = section.title.replace(" ", "_")
366385
self.report.logger.debug(
367386
f"Processing section '{section.id}': '{section.title}' - {len(section.subsections)} subsection(s)"

0 commit comments

Comments
 (0)