Skip to content

Commit dd4f961

Browse files
committed
🎨 Style(streamlit_reportview.py): use Papth lib for all paths in the streamlit reports
1 parent 7eec0ea commit dd4f961

File tree

3 files changed

+21
-42
lines changed

3 files changed

+21
-42
lines changed

‎src/vuegen/report.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def create_and_save_pyvis_network(self, G: nx.Graph, output_file: str) -> Networ
382382
net.show_buttons(filter_=["physics"])
383383

384384
# Save the network as an HTML file
385-
net.save_graph(output_file)
385+
net.save_graph(str(output_file))
386386
self.logger.info(f"PyVis network created and saved as: {output_file}.")
387387
return net
388388

‎src/vuegen/streamlit_reportview.py‎

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class StreamlitReportView(r.WebAppReportView):
1515
"""
1616

1717
BASE_DIR = "streamlit_report"
18-
SECTIONS_DIR = os.path.join(BASE_DIR, "sections")
19-
STATIC_FILES_DIR = os.path.join(BASE_DIR, "static")
18+
SECTIONS_DIR = Path(BASE_DIR) / "sections"
19+
STATIC_FILES_DIR = Path(BASE_DIR) / "static"
2020
REPORT_MANAG_SCRIPT = "report_manager.py"
2121

2222
def __init__(
@@ -94,7 +94,7 @@ def generate_report(
9494
# Create a folder for each section
9595
subsection_page_vars = []
9696
section_name_var = section.title.replace(" ", "_")
97-
section_dir_path = os.path.join(output_dir, section_name_var)
97+
section_dir_path = Path(output_dir) / section_name_var
9898

9999
if create_folder(section_dir_path):
100100
self.report.logger.debug(
@@ -130,7 +130,7 @@ def generate_report(
130130

131131
# Write the navigation and general content to a Python file
132132
with open(
133-
os.path.join(output_dir, self.REPORT_MANAG_SCRIPT), "w"
133+
Path(output_dir) / self.REPORT_MANAG_SCRIPT, "w"
134134
) as nav_manager:
135135
nav_manager.write("\n".join(report_manag_content))
136136
self.report.logger.info(
@@ -163,7 +163,7 @@ def run_report(self, output_dir: str = SECTIONS_DIR) -> None:
163163
[
164164
"streamlit",
165165
"run",
166-
os.path.join(output_dir, self.REPORT_MANAG_SCRIPT),
166+
Path(output_dir) / self.REPORT_MANAG_SCRIPT,
167167
],
168168
check=True,
169169
)
@@ -181,12 +181,12 @@ def run_report(self, output_dir: str = SECTIONS_DIR) -> None:
181181
f"To run the Streamlit app, use the following command:"
182182
)
183183
self.report.logger.info(
184-
f"streamlit run {os.path.join(output_dir, self.REPORT_MANAG_SCRIPT)}"
184+
f"streamlit run {Path(output_dir) / self.REPORT_MANAG_SCRIPT}"
185185
)
186186
msg = (
187187
f"\nAll the scripts to build the Streamlit app are available at: {output_dir}\n\n"
188188
f"To run the Streamlit app, use the following command:\n\n"
189-
f"\tstreamlit run {os.path.join(output_dir, self.REPORT_MANAG_SCRIPT)}"
189+
f"\tstreamlit run {Path(output_dir) / self.REPORT_MANAG_SCRIPT}"
190190
)
191191
print(msg)
192192

@@ -243,7 +243,7 @@ def _generate_home_section(
243243

244244
try:
245245
# Create folder for the home page
246-
home_dir_path = os.path.join(output_dir, "Home")
246+
home_dir_path = Path(output_dir) / "Home"
247247
if create_folder(home_dir_path):
248248
self.report.logger.debug(f"Created home directory: {home_dir_path}")
249249
else:
@@ -268,7 +268,7 @@ def _generate_home_section(
268268
home_content.append("st.markdown(footer, unsafe_allow_html=True)\n")
269269

270270
# Write the home page content to a Python file
271-
home_page_path = os.path.join(home_dir_path, "Homepage.py")
271+
home_page_path = Path(home_dir_path) / "Homepage.py"
272272
with open(home_page_path, "w") as home_page:
273273
home_page.write("\n".join(home_content))
274274
self.report.logger.info(f"Home page content written to '{home_page_path}'.")
@@ -309,11 +309,7 @@ def _generate_sections(self, output_dir: str) -> None:
309309
)
310310
try:
311311
# Create subsection file
312-
subsection_file_path = os.path.join(
313-
output_dir,
314-
section_name_var,
315-
subsection.title.replace(" ", "_") + ".py",
316-
)
312+
subsection_file_path = Path(output_dir) / section_name_var / f"{subsection.title.replace(' ', '_')}.py"
317313

318314
# Generate content and imports for the subsection
319315
subsection_content, subsection_imports = (
@@ -461,9 +457,7 @@ def _generate_plot_content(
461457
networkx_graph, html_plot_file = networkx_graph
462458
else:
463459
# Otherwise, create and save a new pyvis network from the netowrkx graph
464-
html_plot_file = os.path.join(
465-
static_dir, f"{plot.title.replace(' ', '_')}.html"
466-
)
460+
html_plot_file = Path(static_dir) / f"{plot.title.replace(' ', '_')}.html"
467461
pyvis_graph = plot.create_and_save_pyvis_network(
468462
networkx_graph, html_plot_file
469463
)
@@ -532,7 +526,7 @@ def _generate_plot_code(self, plot) -> str:
532526
plot_json = json.loads(response.text)\n"""
533527
else: # If it's a local file
534528
plot_code = f"""
535-
with open('{os.path.join(plot.file_path)}', 'r') as plot_file:
529+
with open('{Path(plot.file_path)}', 'r') as plot_file:
536530
plot_json = json.load(plot_file)\n"""
537531

538532
# Add specific code for each visualization tool
@@ -585,7 +579,7 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
585579

586580
try:
587581
# Check if the file extension matches any DataFrameFormat value
588-
file_extension = os.path.splitext(dataframe.file_path)[1].lower()
582+
file_extension = Path(dataframe.file_path).suffix.lower()
589583
if not any(
590584
file_extension == fmt.value_with_dot for fmt in r.DataFrameFormat
591585
):
@@ -675,7 +669,7 @@ def _generate_markdown_content(self, markdown) -> List[str]:
675669
else: # If it's a local file
676670
markdown_content.append(
677671
f"""
678-
with open('{os.path.join("..", markdown.file_path)}', 'r') as markdown_file:
672+
with open('{Path("..") / markdown.file_path}', 'r') as markdown_file:
679673
markdown_content = markdown_file.read()\n"""
680674
)
681675
# Code to display md content
@@ -735,7 +729,7 @@ def _generate_html_content(self, html) -> List[str]:
735729
# If it's a local file
736730
html_content.append(
737731
f"""
738-
with open('{os.path.join("..", html.file_path)}', 'r', encoding='utf-8') as html_file:
732+
with open('{Path("..") / html.file_path}', 'r', encoding='utf-8') as html_file:
739733
html_content = html_file.read()\n"""
740734
)
741735

‎src/vuegen/utils.py‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,20 @@
2424

2525

2626
## CHECKS
27-
def check_path(filepath: str) -> bool:
27+
def check_path(filepath: Path) -> bool:
2828
"""
2929
Checks if the given file or folder path exists.
3030
3131
Parameters
3232
---------
33-
filepath : str
33+
filepath : Path
3434
The file or folder path to check.
3535
3636
Returns
3737
-------
3838
bool
3939
True if the path exists, False otherwise.
40-
41-
Raises
42-
------
43-
AssertionError
44-
If the filepath is not a valid string.
4540
"""
46-
# Assert that the filepath is a string
47-
assert isinstance(filepath, str), f"Filepath must be a string: {filepath}"
4841

4942
# Check if the path exists
5043
return os.path.exists(os.path.abspath(filepath))
@@ -87,13 +80,13 @@ def assert_enum_value(
8780
)
8881

8982

90-
def is_url(filepath: str) -> bool:
83+
def is_url(filepath: Path) -> bool:
9184
"""
9285
Check if the provided path is a valid URL.
9386
9487
Parameters
9588
----------
96-
filepath : str
89+
filepath : Path
9790
The filepath to check.
9891
9992
Returns
@@ -102,17 +95,9 @@ def is_url(filepath: str) -> bool:
10295
True if the input path is a valid URL, meaning it contains both a scheme
10396
(e.g., http, https, ftp) and a network location (e.g., example.com).
10497
Returns False if either the scheme or the network location is missing or invalid.
105-
106-
Raises
107-
------
108-
AssertionError
109-
If the filepath is not a valid string.
11098
"""
111-
# Assert that the filepath is a string
112-
assert isinstance(filepath, str), f"Filepath must be a string: {filepath}"
113-
11499
# Parse the url and return validation
115-
parsed_url = urlparse(filepath)
100+
parsed_url = urlparse(str(filepath))
116101
return bool(parsed_url.scheme and parsed_url.netloc)
117102

118103

0 commit comments

Comments
 (0)