Spaces:
Sleeping
Sleeping
File size: 3,386 Bytes
5b5d44b e4e7aea 9849bd6 8a662b0 5b5d44b 9849bd6 5b5d44b 925ec05 5b5d44b 9849bd6 5b5d44b 9849bd6 925ec05 5b5d44b 925ec05 5b5d44b 925ec05 5b5d44b 925ec05 5b5d44b 925ec05 5b5d44b 925ec05 5521270 925ec05 5521270 925ec05 5521270 925ec05 5521270 5b5d44b 5521270 5b5d44b 5521270 5b5d44b c009e33 5521270 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import os
import requests
import pandas as pd
import plotly.express as px
import streamlit as st
import matplotlib as mpl
import matplotlib.font_manager as fm
from io import StringIO
# 設定 Streamlit 頁面
st.set_page_config(page_title="ESG & 董事席次數據分析", layout="wide")
# 安裝自訂字型
def install_custom_font():
font_path = "TaipeiSansTCBeta-Regular.ttf"
if not os.path.exists(font_path):
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
font_response = requests.get(font_url)
with open(font_path, "wb") as font_file:
font_file.write(font_response.content)
fm.fontManager.addfont(font_path)
mpl.rc('font', family='Taipei Sans TC Beta')
# 執行字型安裝
install_custom_font()
# 下載 ESG 與 董事席次 CSV 資料
esg_url = "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv"
board_url = "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
@st.cache_data
def load_data(url):
response = requests.get(url)
if response.status_code == 200:
data = response.content.decode("utf-8-sig")
df = pd.read_csv(StringIO(data))
df.fillna(0, inplace=True) # 填補缺失值
return df
else:
st.error("無法獲取資料,錯誤代碼: " + str(response.status_code))
return None
# 加載數據
df_esg = load_data(esg_url)
df_board = load_data(board_url)
# Streamlit 標題
st.title("📊 ESG & 董事席次數據分析儀表板")
# 選擇數據類型
data_option = st.sidebar.radio("📂 選擇數據類型", ["ESG 數據", "董事席次數據"])
if data_option == "ESG 數據" and df_esg is not None:
st.subheader("📌 ESG 數據總覽")
st.write(df_esg.describe())
st.subheader("📌 ESG 數據預覽")
st.write(df_esg.head(10))
elif data_option == "董事席次數據" and df_board is not None:
st.subheader("📌 董事席次數據總覽")
st.write(df_board.describe())
st.subheader("📌 董事席次數據預覽")
st.write(df_board.head(10))
# 添加數據篩選
df = df_esg if data_option == "ESG 數據" else df_board
filter_col = st.sidebar.selectbox("選擇要篩選的欄位", df.columns)
unique_values = df[filter_col].unique()
selected_value = st.sidebar.selectbox("選擇篩選值", unique_values)
filtered_df = df[df[filter_col] == selected_value]
st.write(filtered_df.head())
# 確保數據包含數值型欄位
if 'filtered_df' in locals() and filtered_df is not None:
numeric_cols = filtered_df.select_dtypes(include=['number']).columns
if len(numeric_cols) > 0:
col_choice = st.selectbox("選擇要視覺化的數值欄位", numeric_cols)
if st.button("生成圖表"):
# 繪製長條圖
st.subheader("📊 長條圖")
fig_bar = px.bar(filtered_df.head(10), x=filtered_df.index[:10], y=col_choice,
title=f"{col_choice} 長條圖", color_discrete_sequence=["#1f77b4"])
st.plotly_chart(fig_bar, use_container_width=True)
# 繪製圓餅圖
st.subheader("🥧 圓餅圖")
fig_pie = px.pie(filtered_df.head(10), names=filtered_df.index[:10], values=col_choice,
title=f"{col_choice} 圓餅圖")
st.plotly_chart(fig_pie, use_container_width=True)
|