|
1 | | -import logging |
2 | 1 | import os |
3 | 2 | import subprocess |
4 | 3 | import sys |
| 4 | +import textwrap |
5 | 5 | from pathlib import Path |
6 | 6 | from typing import List |
7 | 7 |
|
8 | 8 | import networkx as nx |
9 | | -import pandas as pd |
10 | 9 |
|
11 | 10 | from . import report as r |
| 11 | +from . import table_utils |
12 | 12 | from .utils import create_folder, is_url, sort_imports |
13 | 13 |
|
14 | 14 |
|
@@ -646,19 +646,23 @@ def _generate_dataframe_content( |
646 | 646 |
|
647 | 647 | # Append header for DataFrame loading |
648 | 648 | dataframe_content.append( |
649 | | - f"""```{{python}} |
650 | | -#| label: '{dataframe.title} {dataframe.id}' |
651 | | -#| fig-cap: "" |
652 | | -""" |
| 649 | + textwrap.dedent( |
| 650 | + f"""\ |
| 651 | + ```{{python}} |
| 652 | + #| label: '{dataframe.title} {dataframe.id}' |
| 653 | + #| fig-cap: "" |
| 654 | + """ |
| 655 | + ) |
653 | 656 | ) |
654 | 657 | # Mapping of file extensions to read functions |
655 | | - read_function_mapping = { |
656 | | - r.DataFrameFormat.CSV.value_with_dot: pd.read_csv, |
657 | | - r.DataFrameFormat.PARQUET.value_with_dot: pd.read_parquet, |
658 | | - r.DataFrameFormat.TXT.value_with_dot: pd.read_table, |
659 | | - r.DataFrameFormat.XLS.value_with_dot: pd.read_excel, |
660 | | - r.DataFrameFormat.XLSX.value_with_dot: pd.read_excel, |
661 | | - } |
| 658 | + read_function_mapping = table_utils.read_function_mapping |
| 659 | + # { |
| 660 | + # r.DataFrameFormat.CSV.value_with_dot: pd.read_csv, |
| 661 | + # r.DataFrameFormat.PARQUET.value_with_dot: pd.read_parquet, |
| 662 | + # r.DataFrameFormat.TXT.value_with_dot: pd.read_table, |
| 663 | + # r.DataFrameFormat.XLS.value_with_dot: pd.read_excel, |
| 664 | + # r.DataFrameFormat.XLSX.value_with_dot: pd.read_excel, |
| 665 | + # } |
662 | 666 | try: |
663 | 667 | # Check if the file extension matches any DataFrameFormat value |
664 | 668 | file_extension = Path(dataframe.file_path).suffix.lower() |
@@ -722,28 +726,35 @@ def _generate_markdown_content(self, markdown) -> List[str]: |
722 | 726 | try: |
723 | 727 | # Initialize md code with common structure |
724 | 728 | markdown_content.append( |
725 | | - f""" |
726 | | -```{{python}} |
727 | | -#| label: '{markdown.title} {markdown.id}' |
728 | | -#| fig-cap: ""\n""" |
| 729 | + textwrap.dedent( |
| 730 | + f""" |
| 731 | + ```{{python}} |
| 732 | + #| label: '{markdown.title} {markdown.id}' |
| 733 | + #| fig-cap: "" |
| 734 | + """ |
| 735 | + ) |
729 | 736 | ) |
730 | 737 | # If the file path is a URL, generate code to fetch content via requests |
731 | 738 | if is_url(markdown.file_path): |
732 | 739 | markdown_content.append( |
733 | | - f""" |
734 | | -response = requests.get('{markdown.file_path}') |
735 | | -response.raise_for_status() |
736 | | -markdown_content = response.text\n""" |
| 740 | + textwrap.dedent( |
| 741 | + f"""\ |
| 742 | + response = requests.get('{markdown.file_path}') |
| 743 | + response.raise_for_status() |
| 744 | + markdown_content = response.text |
| 745 | + """ |
| 746 | + ) |
737 | 747 | ) |
738 | 748 | else: # If it's a local file |
739 | 749 | markdown_content.append( |
740 | 750 | f""" |
741 | 751 | with open('{(Path("..") / markdown.file_path).as_posix()}', 'r') as markdown_file: |
742 | | - markdown_content = markdown_file.read()\n""" |
| 752 | + markdown_content = markdown_file.read() |
| 753 | +""" |
743 | 754 | ) |
744 | 755 |
|
745 | 756 | # Code to display md content |
746 | | - markdown_content.append(f"""display.Markdown(markdown_content)\n```\n""") |
| 757 | + markdown_content.append("""display.Markdown(markdown_content)\n```\n""") |
747 | 758 |
|
748 | 759 | except Exception as e: |
749 | 760 | self.report.logger.error( |
@@ -859,12 +870,15 @@ def _show_dataframe( |
859 | 870 | dataframe_content = [] |
860 | 871 | if is_report_static: |
861 | 872 | # Generate path for the DataFrame image |
862 | | - df_image = Path(static_dir) / f"{dataframe.title.replace(' ', '_')}.png" |
| 873 | + fpath_df_image = ( |
| 874 | + Path(static_dir) / f"{dataframe.title.replace(' ', '_')}.png" |
| 875 | + ) |
863 | 876 | dataframe_content.append( |
864 | | - f"df.dfi.export('{Path(df_image).resolve().as_posix()}', max_rows=10, max_cols=5, table_conversion='matplotlib')\n```\n" |
| 877 | + f"df.dfi.export('{Path(fpath_df_image).resolve().as_posix()}'," |
| 878 | + " max_rows=10, max_cols=5, table_conversion='matplotlib')\n```\n" |
865 | 879 | ) |
866 | 880 | # Use helper method to add centered image content |
867 | | - dataframe_content.append(self._generate_image_content(df_image)) |
| 881 | + dataframe_content.append(self._generate_image_content(fpath_df_image)) |
868 | 882 | else: |
869 | 883 | # Append code to display the DataFrame interactively |
870 | 884 | dataframe_content.append( |
|
0 commit comments