Skip to content

Commit 3112c9a

Browse files
committed
🐛 Fix: add try-except block to solve rel path error for all components
1 parent 4bf5f33 commit 3112c9a

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/vuegen/streamlit_reportview.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,14 @@ def _generate_plot_code(self, plot) -> str:
675675
response.raise_for_status()
676676
plot_json = json.loads(response.text)\n"""
677677
else: # If it's a local file
678+
try:
679+
plot_rel_path = Path(plot.file_path).relative_to(Path.cwd())
680+
except ValueError:
681+
plot_rel_path = (
682+
Path(plot.file_path).resolve().relative_to(Path.cwd().resolve())
683+
)
678684
plot_code = f"""
679-
with open('{Path(plot.file_path).relative_to(Path.cwd()).as_posix()}', 'r') as plot_file:
685+
with open('{plot_rel_path.as_posix()}', 'r') as plot_file:
680686
plot_json = json.load(plot_file)\n"""
681687

682688
# Add specific code for each visualization tool
@@ -745,8 +751,16 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
745751

746752
# Load the DataFrame using the correct function
747753
read_function = read_function_mapping[file_extension]
754+
try:
755+
df_rel_path = Path(dataframe.file_path).relative_to(Path.cwd())
756+
except ValueError:
757+
df_rel_path = (
758+
Path(dataframe.file_path)
759+
.resolve()
760+
.relative_to(Path.cwd().resolve())
761+
)
748762
dataframe_content.append(
749-
f"""df = pd.{read_function.__name__}('{Path(dataframe.file_path).relative_to(Path.cwd()).as_posix()}')\n"""
763+
f"""df = pd.{read_function.__name__}('{df_rel_path.as_posix()}')\n"""
750764
)
751765

752766
# Displays a DataFrame using AgGrid with configurable options.
@@ -823,9 +837,17 @@ def _generate_markdown_content(self, markdown) -> List[str]:
823837
markdown_content = response.text\n"""
824838
)
825839
else: # If it's a local file
840+
try:
841+
md_rel_path = Path(markdown.file_path).relative_to(Path.cwd())
842+
except ValueError:
843+
md_rel_path = (
844+
Path(markdown.file_path)
845+
.resolve()
846+
.relative_to(Path.cwd().resolve())
847+
)
826848
markdown_content.append(
827849
f"""
828-
with open('{Path(markdown.file_path).relative_to(Path.cwd()).as_posix()}', 'r') as markdown_file:
850+
with open('{md_rel_path.as_posix()}', 'r') as markdown_file:
829851
markdown_content = markdown_file.read()\n"""
830852
)
831853
# Code to display md content
@@ -881,11 +903,16 @@ def _generate_html_content(self, html) -> List[str]:
881903
response.raise_for_status()
882904
html_content = response.text\n"""
883905
)
884-
else:
885-
# If it's a local file
906+
else: # If it's a local file
907+
try:
908+
html_rel_path = Path(html.file_path).relative_to(Path.cwd())
909+
except ValueError:
910+
html_rel_path = (
911+
Path(html.file_path).resolve().relative_to(Path.cwd().resolve())
912+
)
886913
html_content.append(
887914
f"""
888-
with open('{Path(html.file_path).relative_to(Path.cwd()).as_posix()}', 'r', encoding='utf-8') as html_file:
915+
with open('{html_rel_path.as_posix()}', 'r', encoding='utf-8') as html_file:
889916
html_content = html_file.read()\n"""
890917
)
891918

0 commit comments

Comments
 (0)