Skip to content

Commit ba55251

Browse files
committed
🎨 table_utils module, textwrapping and naming vars
- clean-up and understanding the workflow
1 parent ec43047 commit ba55251

File tree

3 files changed

+72
-34
lines changed

3 files changed

+72
-34
lines changed

src/vuegen/quarto_reportview.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import logging
21
import os
32
import subprocess
43
import sys
4+
import textwrap
55
from pathlib import Path
66
from typing import List
77

88
import networkx as nx
9-
import pandas as pd
109

1110
from . import report as r
11+
from . import table_utils
1212
from .utils import create_folder, is_url, sort_imports
1313

1414

@@ -646,19 +646,23 @@ def _generate_dataframe_content(
646646

647647
# Append header for DataFrame loading
648648
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+
)
653656
)
654657
# 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+
# }
662666
try:
663667
# Check if the file extension matches any DataFrameFormat value
664668
file_extension = Path(dataframe.file_path).suffix.lower()
@@ -722,28 +726,35 @@ def _generate_markdown_content(self, markdown) -> List[str]:
722726
try:
723727
# Initialize md code with common structure
724728
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+
)
729736
)
730737
# If the file path is a URL, generate code to fetch content via requests
731738
if is_url(markdown.file_path):
732739
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+
)
737747
)
738748
else: # If it's a local file
739749
markdown_content.append(
740750
f"""
741751
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+
"""
743754
)
744755

745756
# 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""")
747758

748759
except Exception as e:
749760
self.report.logger.error(
@@ -859,12 +870,15 @@ def _show_dataframe(
859870
dataframe_content = []
860871
if is_report_static:
861872
# 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+
)
863876
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"
865879
)
866880
# 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))
868882
else:
869883
# Append code to display the DataFrame interactively
870884
dataframe_content.append(

src/vuegen/streamlit_reportview.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import re
32
import subprocess
43
import sys
54
from pathlib import Path
@@ -9,6 +8,7 @@
98
from streamlit.web import cli as stcli
109

1110
from . import report as r
11+
from . import table_utils
1212
from .utils import create_folder, generate_footer, is_url
1313
from .utils.variables import make_valid_identifier
1414

@@ -630,13 +630,7 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
630630
)
631631

632632
# Mapping of file extensions to read functions
633-
read_function_mapping = {
634-
r.DataFrameFormat.CSV.value_with_dot: pd.read_csv,
635-
r.DataFrameFormat.PARQUET.value_with_dot: pd.read_parquet,
636-
r.DataFrameFormat.TXT.value_with_dot: pd.read_table,
637-
r.DataFrameFormat.XLS.value_with_dot: pd.read_excel,
638-
r.DataFrameFormat.XLSX.value_with_dot: pd.read_excel,
639-
}
633+
read_function_mapping = table_utils.read_function_mapping
640634

641635
try:
642636
# Check if the file extension matches any DataFrameFormat value

src/vuegen/table_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pandas as pd
2+
3+
from . import report as r
4+
5+
# Mapping of file extensions to read functions
6+
read_function_mapping = {
7+
r.DataFrameFormat.CSV.value_with_dot: pd.read_csv,
8+
r.DataFrameFormat.PARQUET.value_with_dot: pd.read_parquet,
9+
r.DataFrameFormat.TXT.value_with_dot: pd.read_table,
10+
r.DataFrameFormat.XLS.value_with_dot: pd.read_excel,
11+
r.DataFrameFormat.XLSX.value_with_dot: pd.read_excel,
12+
}
13+
14+
15+
def get_sheet_names(
16+
file_path: str,
17+
) -> list[str]:
18+
"""Get the sheet names of an Excel file.
19+
20+
Parameters
21+
----------
22+
file_path : str
23+
Path to the Excel file.
24+
25+
Returns
26+
-------
27+
list[str]
28+
List of sheet names.
29+
"""
30+
return pd.ExcelFile(file_path).sheet_names

0 commit comments

Comments
 (0)