Skip to content

Commit 07bb550

Browse files
committed
🐛 Fix: create a function on utils to create relative path and apply it in streamlit and quarto reports
1 parent 3112c9a commit 07bb550

File tree

4 files changed

+98
-276
lines changed

4 files changed

+98
-276
lines changed

basic_example_vuegen_demo_notebook_config.yaml

Lines changed: 0 additions & 184 deletions
This file was deleted.

src/vuegen/quarto_reportview.py

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pandas as pd
99

1010
from . import report as r
11-
from .utils import create_folder, is_url, sort_imports
11+
from .utils import create_folder, is_url, sort_imports, get_relative_file_path
1212

1313

1414
class QuartoReportView(r.ReportView):
@@ -574,7 +574,7 @@ def _generate_plot_content(self, plot) -> List[str]:
574574
plot_content.append(self._generate_plot_code(plot))
575575
if self.is_report_static:
576576
plot_content.append(
577-
f"""fig_plotly.write_image("{static_plot_path.resolve().as_posix()}")\n```\n"""
577+
f"""fig_plotly.write_image("{static_plot_path.relative_to("quarto_report").as_posix()}")\n```\n"""
578578
)
579579
plot_content.append(self._generate_image_content(static_plot_path))
580580
else:
@@ -583,7 +583,7 @@ def _generate_plot_content(self, plot) -> List[str]:
583583
plot_content.append(self._generate_plot_code(plot))
584584
if self.is_report_static:
585585
plot_content.append(
586-
f"""fig_altair.save("{static_plot_path.resolve().as_posix()}")\n```\n"""
586+
f"""fig_altair.save("{static_plot_path.relative_to("quarto_report").as_posix()}")\n```\n"""
587587
)
588588
plot_content.append(self._generate_image_content(static_plot_path))
589589
else:
@@ -655,8 +655,9 @@ def _generate_plot_code(self, plot, output_file="") -> str:
655655
response.raise_for_status()
656656
plot_json = response.text\n"""
657657
else: # If it's a local file
658+
plot_rel_path = get_relative_file_path(plot.file_path, base_path="..")
658659
plot_code += f"""
659-
with open('{(Path("..") / plot.file_path).as_posix()}', 'r') as plot_file:
660+
with open('{plot_rel_path.as_posix()}', 'r') as plot_file:
660661
plot_json = json.load(plot_file)\n"""
661662
# Add specific code for each visualization tool
662663
if plot.plot_type == r.PlotType.PLOTLY:
@@ -734,16 +735,17 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
734735
)
735736

736737
# Build the file path (URL or local file)
737-
file_path = (
738-
dataframe.file_path
739-
if is_url(dataframe.file_path)
740-
else Path("..") / dataframe.file_path
741-
)
738+
if is_url(dataframe.file_path):
739+
df_file_path = dataframe.file_path
740+
else:
741+
df_file_path = get_relative_file_path(
742+
dataframe.file_path, base_path=".."
743+
)
742744

743745
# Load the DataFrame using the correct function
744746
read_function = read_function_mapping[file_extension]
745747
dataframe_content.append(
746-
f"""df = pd.{read_function.__name__}('{file_path.as_posix()}')\n"""
748+
f"""df = pd.{read_function.__name__}('{df_file_path.as_posix()}')\n"""
747749
)
748750

749751
# Display the dataframe
@@ -798,9 +800,10 @@ def _generate_markdown_content(self, markdown) -> List[str]:
798800
markdown_content = response.text\n"""
799801
)
800802
else: # If it's a local file
803+
md_rel_path = get_relative_file_path(markdown.file_path, base_path="..")
801804
markdown_content.append(
802805
f"""
803-
with open('{(Path("..") / markdown.file_path).as_posix()}', 'r') as markdown_file:
806+
with open('{md_rel_path.as_posix()}', 'r') as markdown_file:
804807
markdown_content = markdown_file.read()\n"""
805808
)
806809

@@ -822,6 +825,39 @@ def _generate_markdown_content(self, markdown) -> List[str]:
822825
)
823826
return markdown_content
824827

828+
def _show_dataframe(self, dataframe) -> List[str]:
829+
"""
830+
Appends either a static image or an interactive representation of a DataFrame to the content list.
831+
832+
Parameters
833+
----------
834+
dataframe : DataFrame
835+
The DataFrame object containing the data to display.
836+
837+
Returns
838+
-------
839+
list : List[str]
840+
The list of content lines for the DataFrame.
841+
"""
842+
dataframe_content = []
843+
if self.is_report_static:
844+
# Generate path for the DataFrame image
845+
df_image = (
846+
Path(self.static_dir) / f"{dataframe.title.replace(' ', '_')}.png"
847+
)
848+
dataframe_content.append(
849+
f"df.dfi.export('{Path(df_image).relative_to("quarto_report").as_posix()}', max_rows=10, max_cols=5, table_conversion='matplotlib')\n```\n"
850+
)
851+
# Use helper method to add centered image content
852+
dataframe_content.append(self._generate_image_content(df_image))
853+
else:
854+
# Append code to display the DataFrame interactively
855+
dataframe_content.append(
856+
"""show(df, classes="display nowrap compact", lengthMenu=[3, 5, 10])\n```\n"""
857+
)
858+
859+
return dataframe_content
860+
825861
def _generate_html_content(self, html) -> List[str]:
826862
"""
827863
Adds an HTML component to the report.
@@ -843,14 +879,13 @@ def _generate_html_content(self, html) -> List[str]:
843879

844880
try:
845881
# Embed the HTML in an iframe
846-
iframe_src = (
847-
html.file_path
848-
if is_url(html.file_path)
849-
else Path("..") / html.file_path
850-
)
882+
if is_url(html.file_path):
883+
html_file_path = html.file_path
884+
else:
885+
html_file_path = get_relative_file_path(html.file_path, base_path="..")
851886
iframe_code = f"""
852887
<div style="text-align: center;">
853-
<iframe src="{iframe_src}" alt="{html.title}" width="800px" height="630px"></iframe>
888+
<iframe src="{html_file_path.as_posix()}" alt="{html.title}" width="800px" height="630px"></iframe>
854889
</div>\n"""
855890
html_content.append(iframe_code)
856891

@@ -889,47 +924,10 @@ def _generate_image_content(
889924
"""
890925
if is_url(image_path):
891926
src = image_path
892-
return (
893-
f"""![]({src}){{fig-alt={alt_text} width={width} height={height}}}\n"""
894-
)
895927
else:
896-
src = Path(image_path).resolve()
897-
return (
898-
f"""![](/{src}){{fig-alt={alt_text} width={width} height={height}}}\n"""
899-
)
900-
901-
def _show_dataframe(self, dataframe) -> List[str]:
902-
"""
903-
Appends either a static image or an interactive representation of a DataFrame to the content list.
904-
905-
Parameters
906-
----------
907-
dataframe : DataFrame
908-
The DataFrame object containing the data to display.
928+
src = get_relative_file_path(image_path, base_path="..")
909929

910-
Returns
911-
-------
912-
list : List[str]
913-
The list of content lines for the DataFrame.
914-
"""
915-
dataframe_content = []
916-
if self.is_report_static:
917-
# Generate path for the DataFrame image
918-
df_image = (
919-
Path(self.static_dir) / f"{dataframe.title.replace(' ', '_')}.png"
920-
)
921-
dataframe_content.append(
922-
f"df.dfi.export('{Path(df_image).resolve().as_posix()}', max_rows=10, max_cols=5, table_conversion='matplotlib')\n```\n"
923-
)
924-
# Use helper method to add centered image content
925-
dataframe_content.append(self._generate_image_content(df_image))
926-
else:
927-
# Append code to display the DataFrame interactively
928-
dataframe_content.append(
929-
"""show(df, classes="display nowrap compact", lengthMenu=[3, 5, 10])\n```\n"""
930-
)
931-
932-
return dataframe_content
930+
return f"""![]({src.as_posix()}){{fig-alt={alt_text} width={width} height={height}}}\n"""
933931

934932
def _generate_component_imports(self, component: r.Component) -> List[str]:
935933
"""

0 commit comments

Comments
 (0)