Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,104 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
import requests
|
5 |
-
import io
|
6 |
import plotly.express as px
|
|
|
|
|
7 |
|
8 |
-
#
|
|
|
|
|
|
|
9 |
def download_and_load_csv(url):
|
10 |
response = requests.get(url)
|
11 |
response.encoding = 'utf-8'
|
12 |
df = pd.read_csv(io.StringIO(response.text), encoding='utf-8')
|
13 |
-
|
14 |
-
# 清理欄位名稱
|
15 |
-
df.columns = df.columns.str.strip().str.replace("\n", "").str.replace("\r", "")
|
16 |
return df
|
17 |
|
18 |
-
#
|
19 |
-
def
|
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 |
urls = [
|
48 |
"https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
|
49 |
"https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
|
50 |
"https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
|
51 |
]
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
# 設置 Streamlit 標題
|
57 |
-
st.title("台灣企業 ESG 數據分析與揭露")
|
58 |
|
59 |
-
#
|
60 |
-
|
61 |
-
st.dataframe(cleaned_df)
|
62 |
|
63 |
-
#
|
64 |
-
st.
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
#
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
73 |
|
74 |
-
#
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
import requests
|
|
|
4 |
import plotly.express as px
|
5 |
+
import plotly.graph_objects as go
|
6 |
+
import io
|
7 |
|
8 |
+
# 設置全局主題 - 使用更豐富的彩色主題
|
9 |
+
theme = px.colors.qualitative.Bold
|
10 |
+
|
11 |
+
# Function to download and load CSV data
|
12 |
def download_and_load_csv(url):
|
13 |
response = requests.get(url)
|
14 |
response.encoding = 'utf-8'
|
15 |
df = pd.read_csv(io.StringIO(response.text), encoding='utf-8')
|
16 |
+
df = df.fillna(1) # Replace all 0 with 1
|
|
|
|
|
17 |
return df
|
18 |
|
19 |
+
# 美化函數
|
20 |
+
def beautify_chart(fig):
|
21 |
+
fig.update_layout(
|
22 |
+
font_family="Arial",
|
23 |
+
font_color="#444",
|
24 |
+
title_font_family="Arial",
|
25 |
+
title_font_color="#000",
|
26 |
+
legend_title_font_color="#000",
|
27 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
28 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
29 |
+
)
|
30 |
+
fig.update_xaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
|
31 |
+
fig.update_yaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
|
32 |
+
return fig
|
33 |
+
|
34 |
+
# 通用函數來生成並顯示圖表
|
35 |
+
def generate_plots(df, df_name, selected_columns):
|
36 |
+
selected_columns = list(selected_columns) # 將 Index 轉換為列表
|
37 |
+
|
38 |
+
if not selected_columns:
|
39 |
+
st.write(f"{df_name} 中沒有找到可用的數值列來繪製圖表")
|
40 |
+
return
|
41 |
+
|
42 |
+
with st.expander(f"顯示/隱藏 {df_name} 圖表"):
|
43 |
+
st.subheader(f"{df_name} 線圖")
|
44 |
+
fig_line = px.line(df, x="公司名稱", y=selected_columns, title=f"{df_name} 線圖", color_discrete_sequence=theme)
|
45 |
+
fig_line = beautify_chart(fig_line)
|
46 |
+
st.plotly_chart(fig_line)
|
47 |
+
|
48 |
+
st.subheader(f"{df_name} 餅圖")
|
49 |
+
# 分組並計算總排放量
|
50 |
+
total_emissions = df.groupby("公司名稱")[selected_columns].sum().reset_index()
|
51 |
+
total_emissions = total_emissions.melt(id_vars=["公司名稱"], value_vars=selected_columns, var_name="Emission Type", value_name="Total Emissions")
|
52 |
+
# 創建餅圖
|
53 |
+
fig_pie = px.pie(total_emissions, values='Total Emissions', names='公司名稱',
|
54 |
+
title=f"{df_name} 各公司排放量的餅圖",
|
55 |
+
color_discrete_sequence=theme, hole=0.3)
|
56 |
+
fig_pie.update_traces(textposition='inside', textinfo='percent+label')
|
57 |
+
fig_pie = beautify_chart(fig_pie)
|
58 |
+
st.plotly_chart(fig_pie)
|
59 |
+
|
60 |
+
if len(selected_columns) >= 2:
|
61 |
+
st.subheader(f"{df_name} 散點圖")
|
62 |
+
fig_scatter = px.scatter(df, x="公司名稱", y=selected_columns,
|
63 |
+
title=f"{df_name} 散點圖", color_discrete_sequence=theme)
|
64 |
+
fig_scatter = beautify_chart(fig_scatter)
|
65 |
+
st.plotly_chart(fig_scatter)
|
66 |
+
|
67 |
+
# URLs for the CSV files
|
68 |
urls = [
|
69 |
"https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
|
70 |
"https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
|
71 |
"https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
|
72 |
]
|
73 |
|
74 |
+
# Download, load, and clean data from each CSV file
|
75 |
+
dfs = [download_and_load_csv(url) for url in urls]
|
|
|
|
|
|
|
76 |
|
77 |
+
# Combine all DataFrames into one
|
78 |
+
combined_df = pd.concat(dfs, ignore_index=True)
|
|
|
79 |
|
80 |
+
# Set up the Streamlit app
|
81 |
+
st.title("台灣企業ESG數據分析與揭露")
|
82 |
|
83 |
+
# Display the combined DataFrame
|
84 |
+
st.subheader("爬取的資料預覽")
|
85 |
+
st.dataframe(combined_df)
|
86 |
|
87 |
+
# Specify the columns related to emissions
|
88 |
+
emission_columns = [
|
89 |
+
"範疇一排放量(噸CO2e)",
|
90 |
+
"範疇二排放量(噸CO2e)",
|
91 |
+
"範疇三排放量(噸CO2e)"
|
92 |
+
]
|
93 |
|
94 |
+
# Create buttons for each emission category
|
95 |
+
selected_columns = []
|
96 |
+
for column in emission_columns:
|
97 |
+
if st.button(f"顯示 {column}"):
|
98 |
+
selected_columns.append(column)
|
99 |
+
|
100 |
+
# Generate plots for the selected emission categories
|
101 |
+
if selected_columns:
|
102 |
+
generate_plots(combined_df, "Combined Data", selected_columns)
|
103 |
+
else:
|
104 |
+
st.write("請選擇至少一個排放類別來顯示圖表。")
|