Rozeeeee commited on
Commit
5b5d44b
·
verified ·
1 Parent(s): ccb09b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -41
app.py CHANGED
@@ -1,12 +1,16 @@
1
- import streamlit as st
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
- st.title("ESG數據分析")
23
-
24
- # 直接讀取固定 URL 的 CSV 檔案
25
- url = "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv"
26
- response = requests.get(url)
27
- response.encoding = "utf-8"
28
-
29
- df = pd.read_csv(StringIO(response.text))
30
- df_cleaned = df.fillna(0) # 填補缺失值
31
-
32
- st.subheader("📌 數據預覽")
33
- st.write(df_cleaned.head())
34
-
35
- # 讓用戶篩選數據
36
- st.subheader("🔍 數據篩選")
37
- filter_col = st.selectbox("選擇要篩選的欄位", df_cleaned.columns)
38
- unique_values = df_cleaned[filter_col].unique()
39
- selected_value = st.selectbox("選擇篩選值", unique_values)
40
- filtered_df = df_cleaned[df_cleaned[filter_col] == selected_value]
41
-
42
- st.write(filtered_df.head())
43
-
44
- # 讓用戶選擇數值欄位
45
- numeric_cols = filtered_df.select_dtypes(include=['number']).columns
46
- if len(numeric_cols) > 0:
47
- col_choice = st.selectbox("選擇要視覺化的數值欄位", numeric_cols)
48
-
49
- if st.button("生成圖表"):
50
- # 長條圖
51
- st.subheader("📊 長條圖")
52
- fig_bar = px.bar(filtered_df.head(10), x=filtered_df.index[:10], y=col_choice, title=f"{col_choice} 長條圖")
53
- st.plotly_chart(fig_bar)
54
-
55
- # 圓餅圖
56
- st.subheader("🥧 圓餅圖")
57
- fig_pie = px.pie(filtered_df.head(10), names=filtered_df.index[:10], values=col_choice, title=f"{col_choice} 圓餅圖")
58
- st.plotly_chart(fig_pie)
59
- else:
60
- st.warning(" 沒有數值型欄位可供選擇")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("⚠ 沒有數值型欄位可供選擇")