Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,16 @@
|
|
1 |
-
import
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
|
|
5 |
import matplotlib as mpl
|
6 |
import matplotlib.font_manager as fm
|
7 |
-
import os
|
8 |
from io import StringIO
|
9 |
|
|
|
|
|
|
|
|
|
10 |
def install_custom_font():
|
11 |
font_path = "TaipeiSansTCBeta-Regular.ttf"
|
12 |
if not os.path.exists(font_path):
|
@@ -17,44 +21,61 @@ def install_custom_font():
|
|
17 |
fm.fontManager.addfont(font_path)
|
18 |
mpl.rc('font', family='Taipei Sans TC Beta')
|
19 |
|
|
|
20 |
install_custom_font()
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
response
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
+
import streamlit as st
|
6 |
import matplotlib as mpl
|
7 |
import matplotlib.font_manager as fm
|
|
|
8 |
from io import StringIO
|
9 |
|
10 |
+
# 設定 Streamlit 頁面
|
11 |
+
st.set_page_config(page_title="ESG 數據分析", layout="wide")
|
12 |
+
|
13 |
+
# 安裝自訂字型
|
14 |
def install_custom_font():
|
15 |
font_path = "TaipeiSansTCBeta-Regular.ttf"
|
16 |
if not os.path.exists(font_path):
|
|
|
21 |
fm.fontManager.addfont(font_path)
|
22 |
mpl.rc('font', family='Taipei Sans TC Beta')
|
23 |
|
24 |
+
# 執行字型安裝
|
25 |
install_custom_font()
|
26 |
|
27 |
+
# 下載 ESG CSV 資料
|
28 |
+
esg_url = "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv"
|
29 |
+
|
30 |
+
@st.cache_data
|
31 |
+
def load_data(url):
|
32 |
+
response = requests.get(url)
|
33 |
+
if response.status_code == 200:
|
34 |
+
data = response.content.decode("utf-8-sig")
|
35 |
+
df = pd.read_csv(StringIO(data))
|
36 |
+
df.fillna(0, inplace=True) # 填補缺失值
|
37 |
+
return df
|
38 |
+
else:
|
39 |
+
st.error("無法獲取資料,錯誤代碼: " + str(response.status_code))
|
40 |
+
return None
|
41 |
+
|
42 |
+
# 加載 ESG 數據
|
43 |
+
df = load_data(esg_url)
|
44 |
+
|
45 |
+
# Streamlit 標題
|
46 |
+
st.title("📊 ESG 數據分析儀表板")
|
47 |
+
|
48 |
+
if df is not None:
|
49 |
+
# 顯示數據預覽
|
50 |
+
st.subheader("📌 數據預覽")
|
51 |
+
st.write(df.head(10))
|
52 |
+
|
53 |
+
# 讓用戶篩選數據
|
54 |
+
st.subheader("🔍 數據篩選")
|
55 |
+
filter_col = st.selectbox("選擇要篩選的欄位", df.columns)
|
56 |
+
unique_values = df[filter_col].unique()
|
57 |
+
selected_value = st.selectbox("選擇篩選值", unique_values)
|
58 |
+
filtered_df = df[df[filter_col] == selected_value]
|
59 |
+
|
60 |
+
st.write(filtered_df.head())
|
61 |
+
|
62 |
+
# 確保數據包含數值型欄位
|
63 |
+
numeric_cols = filtered_df.select_dtypes(include=['number']).columns
|
64 |
+
if len(numeric_cols) > 0:
|
65 |
+
col_choice = st.selectbox("選擇要視覺化的數值欄位", numeric_cols)
|
66 |
+
|
67 |
+
if st.button("生成圖表"):
|
68 |
+
# 繪製長條圖
|
69 |
+
st.subheader("📊 長條圖")
|
70 |
+
fig_bar = px.bar(filtered_df.head(10), x=filtered_df.index[:10], y=col_choice,
|
71 |
+
title=f"{col_choice} 長條圖", color_discrete_sequence=["#1f77b4"])
|
72 |
+
st.plotly_chart(fig_bar, use_container_width=True)
|
73 |
+
|
74 |
+
# 繪製圓餅圖
|
75 |
+
st.subheader("🥧 圓餅圖")
|
76 |
+
fig_pie = px.pie(filtered_df.head(10), names=filtered_df.index[:10], values=col_choice,
|
77 |
+
title=f"{col_choice} 圓餅圖")
|
78 |
+
st.plotly_chart(fig_pie, use_container_width=True)
|
79 |
+
|
80 |
+
else:
|
81 |
+
st.warning("⚠ 沒有數值型欄位可供選擇")
|