|
1 | 1 | import os |
2 | 2 | import subprocess |
3 | 3 | import sys |
| 4 | +import textwrap |
4 | 5 | from pathlib import Path |
5 | 6 | from typing import List |
6 | 7 |
|
@@ -641,13 +642,40 @@ def _generate_dataframe_content(self, dataframe) -> List[str]: |
641 | 642 | self.report.logger.error( |
642 | 643 | f"Unsupported file extension: {file_extension}. Supported extensions are: {', '.join(fmt.value for fmt in r.DataFrameFormat)}." |
643 | 644 | ) |
| 645 | + # ? Does this skip the execution step? |
| 646 | + |
| 647 | + if file_extension in [ |
| 648 | + r.DataFrameFormat.XLS.value_with_dot, |
| 649 | + r.DataFrameFormat.XLSX.value_with_dot, |
| 650 | + ]: |
| 651 | + dataframe_content.append("selected_sheet = 0") |
| 652 | + sheet_names = table_utils.get_sheet_names(dataframe.file_path) |
| 653 | + if len(sheet_names) > 1: |
| 654 | + # If there are multiple sheets, ask the user to select one |
| 655 | + |
| 656 | + dataframe_content.append( |
| 657 | + textwrap.dedent( |
| 658 | + f"""\ |
| 659 | + sheet_names = table_utils.get_sheet_names("{dataframe.file_path}") |
| 660 | + selected_sheet = st.selectbox("Select a sheet to display", options=sheet_names) |
| 661 | + """ |
| 662 | + ) |
| 663 | + ) |
644 | 664 |
|
645 | 665 | # Load the DataFrame using the correct function |
646 | 666 | read_function = read_function_mapping[file_extension] |
647 | | - dataframe_content.append( |
648 | | - f"""df = pd.{read_function.__name__}('{dataframe.file_path}')\n""" |
649 | | - ) |
650 | | - |
| 667 | + if file_extension in [ |
| 668 | + r.DataFrameFormat.XLS.value_with_dot, |
| 669 | + r.DataFrameFormat.XLSX.value_with_dot, |
| 670 | + ]: |
| 671 | + dataframe_content.append( |
| 672 | + f"""df = pd.{read_function.__name__}('{dataframe.file_path}', sheet_name=selected_sheet)\n""" |
| 673 | + ) |
| 674 | + else: |
| 675 | + dataframe_content.append( |
| 676 | + f"""df = pd.{read_function.__name__}('{dataframe.file_path}')\n""" |
| 677 | + ) |
| 678 | + # ! iterate over sheets in DataFrame |
651 | 679 | # Displays a DataFrame using AgGrid with configurable options. |
652 | 680 | dataframe_content.append( |
653 | 681 | """ |
@@ -1065,6 +1093,7 @@ def _generate_component_imports(self, component: r.Component) -> List[str]: |
1065 | 1093 | "dataframe": [ |
1066 | 1094 | "import pandas as pd", |
1067 | 1095 | "from st_aggrid import AgGrid, GridOptionsBuilder", |
| 1096 | + "from vuegen import table_utils", |
1068 | 1097 | ], |
1069 | 1098 | "markdown": ["import requests"], |
1070 | 1099 | "chatbot": ["import time", "import json", "import requests"], |
|
0 commit comments