Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import requests | |
import plotly.express as px | |
import matplotlib.font_manager as fm | |
import matplotlib as mpl | |
import io | |
import time | |
# 確保正確的中文字符編碼 | |
st.set_page_config(page_title="🌳台灣中小企業ESG數據分析與揭露儀錶板🌲", page_icon=":chart_with_upwards_trend:", layout="wide") | |
# 定義 URL | |
urls = { | |
"溫室氣體": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv", | |
"能源": "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv", | |
"董事會揭露": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv" | |
} | |
# 下載並加載 CSV 文件到 DataFrame 的函數 | |
def load_data(url): | |
response = requests.get(url) | |
response.encoding = 'utf-8' | |
df = pd.read_csv(io.StringIO(response.text), encoding='utf-8') | |
df = df.fillna(0) | |
return df | |
# Streamlit 應用 | |
st.title("台灣企業ESG數據分析與揭露") | |
st.subheader("以溫室氣體 X 再生能源 X 董事會資訊: https://www.tejwin.com/insight/carbon-footprint-verification/") | |
st.subheader("ESG投資: https://www.fhtrust.com.tw/ESG/operating") | |
# 允許用戶選擇數據集 | |
dataset_choice = st.selectbox("選擇要顯示的數據集", list(urls.keys())) | |
# 加載選定的數據集 | |
selected_df = load_data(urls[dataset_choice]) | |
# 顯示爬取的資料 | |
st.write("### 爬取的資料預覽") | |
st.dataframe(selected_df.head()) | |
# 過濾出數值類型的列,排除 '出表日期' 和 '報告年度' | |
numeric_columns = selected_df.select_dtypes(include=['float64', 'int64']).columns | |
numeric_columns = [col for col in numeric_columns if col not in ['出表日期', '報告年度']] | |
# 允許用戶選擇用於繪製圖表的列 | |
column_choice = st.selectbox("選擇欄位來繪製圖表", numeric_columns) | |
# 添加一個生成圖表的按鈕 | |
if st.button("生成圖表"): | |
# 顯示進度條 | |
progress_bar = st.progress(0) | |
for i in range(100): | |
time.sleep(0.01) | |
progress_bar.progress(i + 1) | |
# 創建一個標籤頁布局 | |
tab1, tab2 = st.tabs(["圓餅圖", "長條圖"]) | |
with tab1: | |
# 使用 plotly 創建圓餅圖 | |
fig_pie = px.pie( | |
selected_df, | |
names='公司名稱', | |
values=column_choice, | |
title=f"{dataset_choice} - {column_choice} 圓餅圖", | |
color_discrete_sequence=px.colors.qualitative.Pastel | |
) | |
fig_pie.update_traces(textposition='inside', textinfo='percent+label') | |
# 將 legend 移到圖表的下方 | |
fig_pie.update_layout( | |
font=dict(size=12), | |
legend=dict( | |
orientation="h", # 橫向排列 | |
yanchor="top", # 固定在圖表的頂部 | |
y=-0.3, # 將 legend 移到圖表下方,調整這個值控制距離 | |
xanchor="center", | |
x=0.5 | |
), | |
height=700, # 增加圖表高度 | |
margin=dict(t=50, b=50, l=50, r=50) # 邊距調整 | |
) | |
st.plotly_chart(fig_pie, use_container_width=True) | |
with tab2: | |
# 使用 plotly 創建長條圖 | |
fig_bar = px.bar(selected_df, x='公司名稱', y=column_choice, | |
title=f"{dataset_choice} - {column_choice} 長條圖", | |
color='公司名稱', | |
color_discrete_sequence=px.colors.qualitative.Pastel) | |
fig_bar.update_layout( | |
xaxis_title="企業", | |
yaxis_title=column_choice, | |
font=dict(size=12), | |
xaxis_tickangle=-45, | |
showlegend=False, | |
height=600 # 增加圖表高度 | |
) | |
st.plotly_chart(fig_bar, use_container_width=True) | |
st.success("圖表生成完成!") | |
# 下載並設置自定義字體以顯示中文字符 | |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download" | |
font_response = requests.get(font_url) | |
with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file: | |
font_file.write(font_response.content) | |
fm.fontManager.addfont("TaipeiSansTCBeta-Regular.ttf") | |
mpl.rc('font', family='Taipei Sans TC Beta') | |