Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import requests
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import matplotlib
|
| 8 |
+
zhfont = matplotlib.font_manager.FontProperties(fname='./SourceHanSansTW-Regular.otf')
|
| 9 |
+
|
| 10 |
+
"""
|
| 11 |
+
# 資料視覺化
|
| 12 |
+
(可補敘述).....
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
def visualization(df):
|
| 16 |
+
st.subheader("📈 自動產生的視覺化圖表")
|
| 17 |
+
|
| 18 |
+
# 畫圖(使用前兩欄為例)
|
| 19 |
+
#====以下是一個測試範例, 改成你自己的畫圖流程====
|
| 20 |
+
if df.select_dtypes(include='number').shape[1] >= 2:
|
| 21 |
+
df.iloc[:, :2].plot(kind='line')
|
| 22 |
+
plt.title("前兩欄數值折線圖", fontproperties=zhfont)
|
| 23 |
+
plt.xlabel("Index")
|
| 24 |
+
plt.ylabel("Value")
|
| 25 |
+
plt.tight_layout()
|
| 26 |
+
else:
|
| 27 |
+
st.warning("資料中至少需要兩個數值欄位才能繪圖。")
|
| 28 |
+
#=====================================
|
| 29 |
+
|
| 30 |
+
# 圖片轉成 BytesIO 再顯示
|
| 31 |
+
buf = BytesIO()
|
| 32 |
+
plt.savefig(buf, format="png")
|
| 33 |
+
st.image(buf)
|
| 34 |
+
plt.close()
|
| 35 |
+
|
| 36 |
+
# 🚀 選擇資料輸入方式
|
| 37 |
+
option = st.radio("選擇資料來源:", ["上傳 CSV 檔", "輸入 CSV 網址"])
|
| 38 |
+
|
| 39 |
+
# 📂 檔案上傳
|
| 40 |
+
if option == "上傳 CSV 檔":
|
| 41 |
+
uploaded_file = st.file_uploader("請選擇一個 CSV 檔案", type=["csv"])
|
| 42 |
+
if uploaded_file is not None:
|
| 43 |
+
try:
|
| 44 |
+
data = pd.read_csv(uploaded_file)
|
| 45 |
+
st.success("✅ 成功讀取檔案")
|
| 46 |
+
st.write(data.head())
|
| 47 |
+
visualization(data)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"❌ 無法讀取檔案:{e}")
|
| 50 |
+
|
| 51 |
+
# 🌐 網址讀取
|
| 52 |
+
else:
|
| 53 |
+
url = st.text_input("請輸入 CSV 檔案網址(例如 raw GitHub link)")
|
| 54 |
+
if url:
|
| 55 |
+
try:
|
| 56 |
+
response = requests.get(url)
|
| 57 |
+
response.raise_for_status()
|
| 58 |
+
data = pd.read_csv(BytesIO(response.content))
|
| 59 |
+
st.success("✅ 成功從網址載入資料")
|
| 60 |
+
st.write(data.head())
|
| 61 |
+
visualization(data)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.error(f"❌ 無法讀取網址資料:{e}")
|