Skip to content

Commit 4eaf9a0

Browse files
committed
🎨 pass relative section file paths on using Section dataclasses
- Section could be used everywhere, Subsection is not really necessary. If a Section has subsections, the logic changes. Else nothing changes.
1 parent 6570819 commit 4eaf9a0

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/vuegen/report.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,13 +678,17 @@ class Subsection:
678678
A list of components within the subsection.
679679
description : str, optional
680680
A description of the subsection (default is None).
681+
file_path : str, optional
682+
Relative file path to the section file in sections folder.
683+
Used for building reports (default is None).
681684
"""
682685

683686
_id_counter: ClassVar[int] = 0
684687
id: int = field(init=False)
685688
title: str
686689
components: List["Component"] = field(default_factory=list)
687690
description: Optional[str] = None
691+
file_path: Optional[str] = None
688692

689693
def __post_init__(self):
690694
self.id = self._generate_id()
@@ -695,6 +699,7 @@ def _generate_id(cls) -> int:
695699
return cls._id_counter
696700

697701

702+
# ? Section is a subclass of Subsection (adding subsections). Destinction might not be necessary
698703
@dataclass
699704
class Section:
700705
"""
@@ -710,8 +715,13 @@ class Section:
710715
Title of the section.
711716
subsections : List[Subsection]
712717
A list of subsections within the section.
718+
components : List[Component]
719+
A list of components within the subsection.
713720
description : str, optional
714721
A description of the section (default is None).
722+
file_path : str, optional
723+
Relative file path to the section file in sections folder.
724+
Used for building reports (default is None).
715725
"""
716726

717727
_id_counter: ClassVar[int] = 0
@@ -720,6 +730,7 @@ class Section:
720730
subsections: List["Subsection"] = field(default_factory=list)
721731
components: List["Component"] = field(default_factory=list)
722732
description: Optional[str] = None
733+
file_path: Optional[str] = None
723734

724735
def __post_init__(self):
725736
self.id = self._generate_id()

src/vuegen/streamlit_reportview.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
136136
for section in self.report.sections[1:]: # skip home section components
137137
# Create a folder for each section
138138
subsection_page_vars = []
139-
section_name_var = section.title.replace(" ", "_")
139+
section_name_var = make_valid_identifier(
140+
section.title.replace(" ", "_")
141+
)
140142
section_dir_path = Path(output_dir) / section_name_var
141143

142144
if create_folder(section_dir_path):
@@ -151,9 +153,9 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
151153
if section.components:
152154
subsection_file_path = (
153155
Path(section_name_var)
154-
/ f"0_overview_{section.title.lower()}.py"
156+
/ f"0_overview_{make_valid_identifier(section.title).lower()}.py"
155157
).as_posix() # Make sure it's Posix Paths
156-
158+
section.file_path = subsection_file_path
157159
# Create a Page object for each subsection and add it to the home page content
158160
report_manag_content.append(
159161
f"{section_name_var}_overview = st.Page('{subsection_file_path}', title='Overview {section.title}')"
@@ -173,7 +175,7 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
173175
subsection_file_path = (
174176
Path(section_name_var) / f"{subsection_name_var}.py"
175177
).as_posix() # Make sure it's Posix Paths
176-
178+
subsection.file_path = subsection_file_path
177179
# Create a Page object for each subsection and add it to the home page content
178180
report_manag_content.append(
179181
f"{subsection_name_var} = st.Page('{subsection_file_path}', title='{subsection.title}')"
@@ -402,16 +404,11 @@ def _generate_sections(self, output_dir: str) -> None:
402404
section_content, section_imports, _ = self._combine_components(
403405
section.components
404406
)
405-
_filepath_overview = (
406-
Path(output_dir)
407-
/ section_name_var
408-
/ f"0_overview_{section.title.lower()}.py"
409-
# ! tighly coupled to generate_report fct:
410-
# ! check how to pass file names
411-
)
412-
407+
assert (
408+
section.file_path is not None
409+
), "Missing relative file path to overview page in section"
413410
write_python_file(
414-
fpath=_filepath_overview,
411+
fpath=Path(output_dir) / section.file_path,
415412
imports=section_imports,
416413
contents=section_content,
417414
)
@@ -433,12 +430,10 @@ def _generate_sections(self, output_dir: str) -> None:
433430
try:
434431
# Create subsection file
435432
_subsection_name = make_valid_identifier(subsection.title)
436-
subsection_file_path = (
437-
Path(output_dir)
438-
/ section_name_var
439-
/ f"{_subsection_name}.py"
440-
)
441-
433+
assert (
434+
subsection.file_path is not None
435+
), "Missing relative file path to subsection"
436+
subsection_file_path = Path(output_dir) / subsection.file_path
442437
# Generate content and imports for the subsection
443438
subsection_content, subsection_imports = (
444439
self._generate_subsection(subsection)

0 commit comments

Comments
 (0)