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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -15
app.py CHANGED
@@ -8,7 +8,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():
@@ -24,8 +24,9 @@ def install_custom_font():
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):
@@ -39,27 +40,44 @@ def load_data(url):
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)
 
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():
 
24
  # 執行字型安裝
25
  install_custom_font()
26
 
27
+ # 下載 ESG 與 董事席次 CSV 資料
28
  esg_url = "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv"
29
+ board_url = "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
30
 
31
  @st.cache_data
32
  def load_data(url):
 
40
  st.error("無法獲取資料,錯誤代碼: " + str(response.status_code))
41
  return None
42
 
43
+ # 加載數據
44
+ df_esg = load_data(esg_url)
45
+ df_board = load_data(board_url)
46
 
47
  # Streamlit 標題
48
+ st.title("📊 ESG & 董事席次數據分析儀表板")
49
 
50
+ # 選擇數據類型
51
+ data_option = st.sidebar.radio("📂 選擇數據類型", ["ESG 數據", "董事席次數據"])
 
 
52
 
53
+ if data_option == "ESG 數據" and df_esg is not None:
54
+ st.subheader("📌 ESG 數據預覽")
55
+ st.write(df_esg.head(10))
56
+
57
+ # 讓用戶篩選 ESG 數據
58
+ st.subheader("🔍 ESG 數據篩選")
59
+ filter_col = st.selectbox("選擇要篩選的欄位", df_esg.columns)
60
+ unique_values = df_esg[filter_col].unique()
61
+ selected_value = st.selectbox("選擇篩選值", unique_values)
62
+ filtered_df = df_esg[df_esg[filter_col] == selected_value]
63
+
64
+ st.write(filtered_df.head())
65
+
66
+ elif data_option == "董事席次數據" and df_board is not None:
67
+ st.subheader("📌 董事席次數據預覽")
68
+ st.write(df_board.head(10))
69
+
70
+ # 讓用戶篩選董事席次數據
71
+ st.subheader("🔍 董事席次數據篩選")
72
+ filter_col = st.selectbox("選擇要篩選的欄位", df_board.columns)
73
+ unique_values = df_board[filter_col].unique()
74
  selected_value = st.selectbox("選擇篩選值", unique_values)
75
+ filtered_df = df_board[df_board[filter_col] == selected_value]
76
 
77
  st.write(filtered_df.head())
78
 
79
+ # 確保數據包含數值型欄位
80
+ if filtered_df is not None:
81
  numeric_cols = filtered_df.select_dtypes(include=['number']).columns
82
  if len(numeric_cols) > 0:
83
  col_choice = st.selectbox("選擇要視覺化的數值欄位", numeric_cols)