Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from io import BytesIO | |
| import requests | |
| import streamlit as st | |
| import matplotlib | |
| zhfont = matplotlib.font_manager.FontProperties(fname='./SourceHanSansTW-Regular.otf') | |
| """ | |
| # 資料視覺化 | |
| (可補敘述)..... | |
| """ | |
| def visualization(df): | |
| st.subheader("📈 自動產生的視覺化圖表") | |
| # 畫圖(使用前兩欄為例) | |
| #====以下是一個測試範例, 改成你自己的畫圖流程==== | |
| if df.select_dtypes(include='number').shape[1] >= 2: | |
| df.iloc[:, :2].plot(kind='line') | |
| plt.title("前兩欄數值折線圖", fontproperties=zhfont) | |
| plt.xlabel("Index") | |
| plt.ylabel("Value") | |
| plt.tight_layout() | |
| else: | |
| st.warning("資料中至少需要兩個數值欄位才能繪圖。") | |
| #===================================== | |
| # 圖片轉成 BytesIO 再顯示 | |
| buf = BytesIO() | |
| plt.savefig(buf, format="png") | |
| st.image(buf) | |
| plt.close() | |
| # 🚀 選擇資料輸入方式 | |
| option = st.radio("選擇資料來源:", ["上傳 CSV 檔", "輸入 CSV 網址"]) | |
| # 📂 檔案上傳 | |
| if option == "上傳 CSV 檔": | |
| uploaded_file = st.file_uploader("請選擇一個 CSV 檔案", type=["csv"]) | |
| if uploaded_file is not None: | |
| try: | |
| data = pd.read_csv(uploaded_file) | |
| st.success("✅ 成功讀取檔案") | |
| st.write(data.head()) | |
| visualization(data) | |
| except Exception as e: | |
| st.error(f"❌ 無法讀取檔案:{e}") | |
| # 🌐 網址讀取 | |
| else: | |
| url = st.text_input("請輸入 CSV 檔案網址(例如 raw GitHub link)") | |
| if url: | |
| try: | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| data = pd.read_csv(BytesIO(response.content)) | |
| st.success("✅ 成功從網址載入資料") | |
| st.write(data.head()) | |
| visualization(data) | |
| except Exception as e: | |
| st.error(f"❌ 無法讀取網址資料:{e}") |