Update app.py
Browse files
app.py
CHANGED
|
@@ -63,6 +63,7 @@ def read_file_and_update_ui(file_obj, sheet_name):
|
|
| 63 |
"""
|
| 64 |
讀取檔案並更新所有 UI 元件的選項。
|
| 65 |
"""
|
|
|
|
| 66 |
if file_obj is None:
|
| 67 |
return (
|
| 68 |
"請上傳檔案。",
|
|
@@ -76,11 +77,14 @@ def read_file_and_update_ui(file_obj, sheet_name):
|
|
| 76 |
pd.DataFrame()
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
try:
|
|
|
|
| 80 |
if file_obj.name.endswith('.csv'):
|
| 81 |
df = pd.read_csv(file_obj.name)
|
| 82 |
elif file_obj.name.endswith(('.xls', '.xlsx')):
|
| 83 |
if not sheet_name or sheet_name == "無":
|
|
|
|
| 84 |
return (
|
| 85 |
"請選擇一個工作表。",
|
| 86 |
gr.DataFrame(pd.DataFrame(), visible=True),
|
|
@@ -94,6 +98,7 @@ def read_file_and_update_ui(file_obj, sheet_name):
|
|
| 94 |
)
|
| 95 |
df = pd.read_excel(file_obj.name, sheet_name=sheet_name)
|
| 96 |
else:
|
|
|
|
| 97 |
return (
|
| 98 |
"不支援的檔案格式。",
|
| 99 |
gr.DataFrame(pd.DataFrame(), visible=True),
|
|
@@ -106,23 +111,67 @@ def read_file_and_update_ui(file_obj, sheet_name):
|
|
| 106 |
pd.DataFrame()
|
| 107 |
)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
df.columns = [safe_column_name(col) for col in df.columns]
|
| 110 |
|
| 111 |
numeric_cols = df.select_dtypes(include=np.number).columns.tolist()
|
| 112 |
categorical_cols = [col for col in df.columns if df[col].nunique() >= 2]
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
return (
|
| 115 |
"檔案讀取成功!",
|
| 116 |
gr.DataFrame(df.head(10), visible=True),
|
| 117 |
-
gr.Dropdown(choices=categorical_cols, value=categorical_cols[0]
|
| 118 |
-
gr.Dropdown(choices=numeric_cols, value=numeric_cols[0]
|
| 119 |
gr.CheckboxGroup(choices=numeric_cols, interactive=True),
|
| 120 |
gr.CheckboxGroup(choices=numeric_cols, interactive=True),
|
| 121 |
-
gr.Dropdown(choices=categorical_cols, value=categorical_cols[0]
|
| 122 |
-
gr.Dropdown(choices=numeric_cols, value=numeric_cols[0]
|
| 123 |
df
|
| 124 |
)
|
| 125 |
except Exception as e:
|
|
|
|
| 126 |
return (
|
| 127 |
f"讀取檔案時發生錯誤: {e}",
|
| 128 |
gr.DataFrame(pd.DataFrame(), visible=True),
|
|
|
|
| 63 |
"""
|
| 64 |
讀取檔案並更新所有 UI 元件的選項。
|
| 65 |
"""
|
| 66 |
+
# 確保在沒有檔案時返回所有必要的輸出
|
| 67 |
if file_obj is None:
|
| 68 |
return (
|
| 69 |
"請上傳檔案。",
|
|
|
|
| 77 |
pd.DataFrame()
|
| 78 |
)
|
| 79 |
|
| 80 |
+
df = pd.DataFrame() # 初始化 DataFrame
|
| 81 |
try:
|
| 82 |
+
# 根據檔案類型讀取數據
|
| 83 |
if file_obj.name.endswith('.csv'):
|
| 84 |
df = pd.read_csv(file_obj.name)
|
| 85 |
elif file_obj.name.endswith(('.xls', '.xlsx')):
|
| 86 |
if not sheet_name or sheet_name == "無":
|
| 87 |
+
# 返回所有必要的輸出
|
| 88 |
return (
|
| 89 |
"請選擇一個工作表。",
|
| 90 |
gr.DataFrame(pd.DataFrame(), visible=True),
|
|
|
|
| 98 |
)
|
| 99 |
df = pd.read_excel(file_obj.name, sheet_name=sheet_name)
|
| 100 |
else:
|
| 101 |
+
# 返回所有必要的輸出
|
| 102 |
return (
|
| 103 |
"不支援的檔案格式。",
|
| 104 |
gr.DataFrame(pd.DataFrame(), visible=True),
|
|
|
|
| 111 |
pd.DataFrame()
|
| 112 |
)
|
| 113 |
|
| 114 |
+
# 處理空的 DataFrame
|
| 115 |
+
if df.empty:
|
| 116 |
+
return (
|
| 117 |
+
"檔案為空,請檢查您的檔案。",
|
| 118 |
+
gr.DataFrame(pd.DataFrame(), visible=True),
|
| 119 |
+
gr.Dropdown(choices=[], interactive=False),
|
| 120 |
+
gr.Dropdown(choices=[], interactive=False),
|
| 121 |
+
gr.CheckboxGroup(choices=[], interactive=False),
|
| 122 |
+
gr.CheckboxGroup(choices=[], interactive=False),
|
| 123 |
+
gr.Dropdown(choices=[], interactive=False),
|
| 124 |
+
gr.Dropdown(choices=[], interactive=False),
|
| 125 |
+
pd.DataFrame()
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# 數據預處理和欄位分類
|
| 129 |
df.columns = [safe_column_name(col) for col in df.columns]
|
| 130 |
|
| 131 |
numeric_cols = df.select_dtypes(include=np.number).columns.tolist()
|
| 132 |
categorical_cols = [col for col in df.columns if df[col].nunique() >= 2]
|
| 133 |
|
| 134 |
+
# 處理沒有足夠欄位的狀況
|
| 135 |
+
if not numeric_cols:
|
| 136 |
+
return (
|
| 137 |
+
"檔案中沒有數值型欄位。",
|
| 138 |
+
gr.DataFrame(df.head(10), visible=True),
|
| 139 |
+
gr.Dropdown(choices=categorical_cols, interactive=True),
|
| 140 |
+
gr.Dropdown(choices=[], interactive=False),
|
| 141 |
+
gr.CheckboxGroup(choices=[], interactive=False),
|
| 142 |
+
gr.CheckboxGroup(choices=[], interactive=False),
|
| 143 |
+
gr.Dropdown(choices=categorical_cols, interactive=True),
|
| 144 |
+
gr.Dropdown(choices=[], interactive=False),
|
| 145 |
+
df
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
if not categorical_cols:
|
| 149 |
+
return (
|
| 150 |
+
"檔案中沒有足夠的類別型欄位 (需至少兩個不同值)。",
|
| 151 |
+
gr.DataFrame(df.head(10), visible=True),
|
| 152 |
+
gr.Dropdown(choices=[], interactive=False),
|
| 153 |
+
gr.Dropdown(choices=numeric_cols, interactive=True),
|
| 154 |
+
gr.CheckboxGroup(choices=numeric_cols, interactive=True),
|
| 155 |
+
gr.CheckboxGroup(choices=numeric_cols, interactive=True),
|
| 156 |
+
gr.Dropdown(choices=[], interactive=False),
|
| 157 |
+
gr.Dropdown(choices=numeric_cols, interactive=True),
|
| 158 |
+
df
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
# 返回所有必要的輸出
|
| 162 |
return (
|
| 163 |
"檔案讀取成功!",
|
| 164 |
gr.DataFrame(df.head(10), visible=True),
|
| 165 |
+
gr.Dropdown(choices=categorical_cols, value=categorical_cols[0], interactive=True),
|
| 166 |
+
gr.Dropdown(choices=numeric_cols, value=numeric_cols[0], interactive=True),
|
| 167 |
gr.CheckboxGroup(choices=numeric_cols, interactive=True),
|
| 168 |
gr.CheckboxGroup(choices=numeric_cols, interactive=True),
|
| 169 |
+
gr.Dropdown(choices=categorical_cols, value=categorical_cols[0], interactive=True),
|
| 170 |
+
gr.Dropdown(choices=numeric_cols, value=numeric_cols[0], interactive=True),
|
| 171 |
df
|
| 172 |
)
|
| 173 |
except Exception as e:
|
| 174 |
+
# 在任何步驟發生錯誤時,返回所有必要的輸出,並附上錯誤訊息
|
| 175 |
return (
|
| 176 |
f"讀取檔案時發生錯誤: {e}",
|
| 177 |
gr.DataFrame(pd.DataFrame(), visible=True),
|