Update app.py
Browse files
app.py
CHANGED
|
@@ -1772,34 +1772,34 @@ def create_plot(df, chart_type, x_column, y_column, group_column=None, size_colu
|
|
| 1772 |
# =========================================
|
| 1773 |
# (與 V4 相同)
|
| 1774 |
def export_data(df, format_type):
|
| 1775 |
-
|
| 1776 |
-
|
| 1777 |
-
|
| 1778 |
-
|
| 1779 |
-
|
| 1780 |
-
|
| 1781 |
-
|
| 1782 |
-
|
| 1783 |
|
| 1784 |
def download_figure(fig, format_type="PNG"):
|
| 1785 |
-
|
| 1786 |
-
|
| 1787 |
-
|
| 1788 |
-
|
| 1789 |
-
|
| 1790 |
-
|
| 1791 |
-
|
| 1792 |
-
|
| 1793 |
-
|
| 1794 |
-
|
| 1795 |
-
|
| 1796 |
|
| 1797 |
# =========================================
|
| 1798 |
# == 智能推薦函數 (Recommendation Function) ==
|
| 1799 |
# =========================================
|
| 1800 |
# (與 V4 相同)
|
| 1801 |
def recommend_chart_settings(df):
|
| 1802 |
-
|
| 1803 |
if df is None or df.empty: recommendation["message"] = "ℹ️ 請先上傳或輸入數據。"; return recommendation
|
| 1804 |
columns = df.columns.tolist(); num_cols = df.select_dtypes(include=np.number).columns.tolist(); cat_cols = df.select_dtypes(include=['object', 'category', 'string']).columns.tolist()
|
| 1805 |
date_cols = [col for col in columns if pd.api.types.is_datetime64_any_dtype(df[col]) or ('日期' in col or '時間' in col)]
|
|
|
|
| 1772 |
# =========================================
|
| 1773 |
# (與 V4 相同)
|
| 1774 |
def export_data(df, format_type):
|
| 1775 |
+
if df is None or df.empty: return None, "❌ 沒有數據可以導出。"
|
| 1776 |
+
try:
|
| 1777 |
+
if format_type == "CSV": filename = "exported_data.csv"; df.to_csv(filename, index=False, encoding='utf-8-sig')
|
| 1778 |
+
elif format_type == "Excel": filename = "exported_data.xlsx"; df.to_excel(filename, index=False)
|
| 1779 |
+
elif format_type == "JSON": filename = "exported_data.json"; df.to_json(filename, orient="records", indent=4, force_ascii=False)
|
| 1780 |
+
else: return None, f"❌ 不支持的導出格式: {format_type}"
|
| 1781 |
+
return filename, f"✅ 數據已成功準備為 {format_type} 格式,點擊下方鏈接下載。"
|
| 1782 |
+
except Exception as e: print(f"導出數據時出錯: {e}"); traceback.print_exc(); return None, f"❌ 導出數據時出錯: {e}"
|
| 1783 |
|
| 1784 |
def download_figure(fig, format_type="PNG"):
|
| 1785 |
+
if fig is None or not fig.data: return None, "❌ 沒有圖表可以導出。"
|
| 1786 |
+
try:
|
| 1787 |
+
format_lower = format_type.lower(); filename = f"chart_export.{format_lower}"
|
| 1788 |
+
import kaleido # 確保導入
|
| 1789 |
+
fig.write_image(filename, format=format_lower)
|
| 1790 |
+
return filename, f"✅ 圖表已成功準備為 {format_type} 格式,點擊下方鏈接下載。"
|
| 1791 |
+
except ImportError: error_msg = "❌ 導出圖表失敗:需要 Kaleido 套件。請在環境中安裝 `pip install -U kaleido`。"; print(error_msg); return None, error_msg
|
| 1792 |
+
except ValueError as ve:
|
| 1793 |
+
if "kaleido" in str(ve).lower(): error_msg = "❌ 導出圖表失敗:Kaleido 套件無法運行。請檢查其依賴項或嘗試重新安裝。"; print(f"{error_msg}\n{ve}"); traceback.print_exc(); return None, error_msg
|
| 1794 |
+
else: print(f"導出圖表時出錯 (ValueError): {ve}"); traceback.print_exc(); return None, f"❌ 導出圖表時出錯: {ve}"
|
| 1795 |
+
except Exception as e: print(f"導出圖表時發生未預期錯誤: {e}"); traceback.print_exc(); return None, f"❌ 導出圖表時發生未預期錯誤: {e}"
|
| 1796 |
|
| 1797 |
# =========================================
|
| 1798 |
# == 智能推薦函數 (Recommendation Function) ==
|
| 1799 |
# =========================================
|
| 1800 |
# (與 V4 相同)
|
| 1801 |
def recommend_chart_settings(df):
|
| 1802 |
+
recommendation = {"chart_type": None, "x_column": None, "y_column": None, "group_column": "無", "agg_function": None, "message": "無法提供推薦。"}
|
| 1803 |
if df is None or df.empty: recommendation["message"] = "ℹ️ 請先上傳或輸入數據。"; return recommendation
|
| 1804 |
columns = df.columns.tolist(); num_cols = df.select_dtypes(include=np.number).columns.tolist(); cat_cols = df.select_dtypes(include=['object', 'category', 'string']).columns.tolist()
|
| 1805 |
date_cols = [col for col in columns if pd.api.types.is_datetime64_any_dtype(df[col]) or ('日期' in col or '時間' in col)]
|