Rozeeeee commited on
Commit
fe9642f
·
verified ·
1 Parent(s): 91b63f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -77
app.py CHANGED
@@ -2,21 +2,32 @@ 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(0) # 填充 NaN 為 0
 
 
 
 
 
 
 
 
 
 
 
17
  return df
18
 
19
- # 美化函數
20
  def beautify_chart(fig):
21
  fig.update_layout(
22
  font_family="Arial",
@@ -31,87 +42,29 @@ def beautify_chart(fig):
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 = [col for col in selected_columns if col in df.columns] # 確保欄位存在
37
-
38
- if not selected_columns:
39
- st.write(f"{df_name} 中沒有找到可用的數值列來繪製圖表")
40
- return
41
-
42
- # 移除所有選擇欄位值皆為 0 的行
43
- df = df[(df[selected_columns] != 0).any(axis=1)]
44
-
45
- if df.empty:
46
- st.write(f"{df_name} 中的選擇欄位數據全為 0 或缺失,無法繪製圖表。")
47
- return
48
-
49
- with st.expander(f"顯示/隱藏 {df_name} 圖表"):
50
- st.subheader(f"{df_name} 線圖")
51
- fig_line = px.line(df, x="公司名稱", y=selected_columns, title=f"{df_name} 線圖", color_discrete_sequence=theme)
52
- fig_line = beautify_chart(fig_line)
53
- st.plotly_chart(fig_line)
54
-
55
- st.subheader(f"{df_name} 餅圖")
56
- total_emissions = df.groupby("公司名稱")[selected_columns].sum().reset_index()
57
- total_emissions = total_emissions.melt(id_vars=["公司名稱"], value_vars=selected_columns, var_name="Emission Type", value_name="Total Emissions")
58
- fig_pie = px.pie(total_emissions, values='Total Emissions', names='公司名稱',
59
- title=f"{df_name} 各公司排放量的餅圖",
60
- color_discrete_sequence=theme, hole=0.3)
61
- fig_pie.update_traces(textposition='inside', textinfo='percent+label')
62
- fig_pie = beautify_chart(fig_pie)
63
- st.plotly_chart(fig_pie)
64
-
65
- if len(selected_columns) >= 2:
66
- st.subheader(f"{df_name} 散點圖")
67
- fig_scatter = px.scatter(df, x="公司名稱", y=selected_columns,
68
- title=f"{df_name} 散點圖", color_discrete_sequence=theme)
69
- fig_scatter = beautify_chart(fig_scatter)
70
- st.plotly_chart(fig_scatter)
71
-
72
- # URLs for the CSV files
73
  urls = [
74
  "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
75
  "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
76
  "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
77
  ]
78
-
79
- # Download, load, and clean data from each CSV file
80
- dfs = [download_and_load_csv(url) for url in urls]
81
-
82
- # Combine all DataFrames into one
83
  combined_df = pd.concat(dfs, ignore_index=True)
84
 
85
- # 確保所有 NaN 值填充為 0
86
- combined_df = combined_df.fillna(0)
87
-
88
- # 移除所有排放量皆為 0 的行
89
- combined_df = combined_df[(combined_df["範疇一排放量(噸CO2e)"] != 0) |
90
- (combined_df["範疇二排放量(噸CO2e)"] != 0) |
91
- (combined_df["範疇三排放量(噸CO2e)"] != 0)]
92
-
93
- # Set up the Streamlit app
94
  st.title("台灣企業ESG數據分析與揭露")
95
 
96
- # Display the combined DataFrame
97
- st.subheader("爬取的資料預覽")
98
  st.dataframe(combined_df)
99
 
100
- # Specify the columns related to emissions
101
- emission_columns = [
102
- "範疇一排放量(噸CO2e)",
103
- "範疇二排放量(噸CO2e)",
104
- "範疇三排放量(噸CO2e)"
105
- ]
106
-
107
- # Create buttons for each emission category
108
- selected_columns = []
109
- for column in emission_columns:
110
- if st.button(f"顯示 {column}"):
111
- selected_columns.append(column)
112
 
113
- # Generate plots for the selected emission categories
114
  if selected_columns:
115
- generate_plots(combined_df, "Combined Data", selected_columns)
 
 
116
  else:
117
- st.write("請選擇至少一個排放類別來顯示圖表。")
 
2
  import pandas as pd
3
  import requests
4
  import plotly.express as px
 
5
  import io
6
 
7
+ # 設置全局主題
8
+ THEME = px.colors.qualitative.Bold
9
 
10
+ # 下載並載入 CSV 數據
11
+ @st.cache_data
12
+ def download_and_clean_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
+
17
+ # 資料清理
18
+ df = df.dropna().drop_duplicates() # 移除缺失值與重複數據
19
+ df.columns = df.columns.str.strip() # 去除欄位名稱前後空格
20
+ df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x) # 去除字串內的空格
21
+
22
+ # 嘗試轉換數值欄位
23
+ num_cols = ["範疇一排放量(噸CO2e)", "範疇二排放量(噸CO2e)", "範疇三排放量(噸CO2e)"]
24
+ for col in num_cols:
25
+ if col in df.columns:
26
+ df[col] = pd.to_numeric(df[col], errors='coerce').fillna(0)
27
+
28
  return df
29
 
30
+ # 美化圖表
31
  def beautify_chart(fig):
32
  fig.update_layout(
33
  font_family="Arial",
 
42
  fig.update_yaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
43
  return fig
44
 
45
+ # 下載、清理並合併數據
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  urls = [
47
  "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
48
  "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
49
  "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
50
  ]
51
+ dfs = [download_and_clean_csv(url) for url in urls]
 
 
 
 
52
  combined_df = pd.concat(dfs, ignore_index=True)
53
 
54
+ # 設定 Streamlit 介面
 
 
 
 
 
 
 
 
55
  st.title("台灣企業ESG數據分析與揭露")
56
 
57
+ # 顯示清理後的數據
58
+ st.subheader("清理後的數據預覽")
59
  st.dataframe(combined_df)
60
 
61
+ # 可視化數據
62
+ emission_columns = ["範疇一排放量(噸CO2e)", "範疇二排放量(噸CO2e)", "範疇三排放量(噸CO2e)"]
63
+ selected_columns = [col for col in emission_columns if col in combined_df.columns]
 
 
 
 
 
 
 
 
 
64
 
 
65
  if selected_columns:
66
+ st.subheader("排放量折線圖")
67
+ fig_line = px.line(combined_df, x="公司名稱", y=selected_columns, title="企業排放量趨勢", color_discrete_sequence=THEME)
68
+ st.plotly_chart(beautify_chart(fig_line))
69
  else:
70
+ st.write("未找到有效的排放數據")