@@ -675,8 +675,14 @@ def _generate_plot_code(self, plot) -> str:
675675response.raise_for_status()
676676plot_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]:
823837markdown_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]:
881903response.raise_for_status()
882904html_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