Roberta2024 commited on
Commit
9016aeb
·
verified ·
1 Parent(s): df41ada

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -23
app.py CHANGED
@@ -6,52 +6,74 @@ import plotly.express as px
6
  import matplotlib.font_manager as fm
7
  import matplotlib as mpl
8
  import io
 
9
 
10
- # Ensure proper encoding for Chinese characters
11
  st.set_page_config(page_title="ESG 專題數據分析", page_icon=":chart_with_upwards_trend:", layout="wide")
12
 
13
- # Define URLs
14
  urls = {
15
  "溫室氣體": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
16
  "能源": "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
17
  "董事會揭露": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
18
  }
19
 
20
- # Function to download and load CSV files into DataFrame
 
21
  def load_data():
22
  dataframes = {}
23
  for name, url in urls.items():
24
  response = requests.get(url)
25
- response.encoding = 'utf-8' # Set encoding to UTF-8
26
  df = pd.read_csv(io.StringIO(response.text), encoding='utf-8')
27
- df = df.fillna(0) # Replace missing values with 0
28
  dataframes[name] = df
29
  return dataframes
30
 
31
- # Load all datasets
32
- dataframes = load_data()
33
-
34
- # Streamlit App
35
  st.title("ESG 專題數據分析")
36
 
37
- # Allow user to select dataset
38
- dataset_choice = st.selectbox("選擇要顯示的數據集", list(dataframes.keys()))
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # Get the selected dataframe
41
- selected_df = dataframes[dataset_choice]
42
 
43
- # Allow user to select a column for pie chart
44
- column_choice = st.selectbox("選擇欄位來繪製圓餅圖", selected_df.columns)
45
 
46
- # Check if the column is numerical
47
- if pd.api.types.is_numeric_dtype(selected_df[column_choice]):
48
- # Create a pie chart using plotly
49
- fig = px.pie(selected_df, names=selected_df.index, values=column_choice, title=f"{dataset_choice} - {column_choice} 圓餅圖")
50
- st.plotly_chart(fig)
51
- else:
52
- st.write("選定的欄位不是數值類型,無法繪製圓餅圖。")
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- # Download and set custom font for displaying Chinese characters
55
  font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
56
  font_response = requests.get(font_url)
57
  with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file:
 
6
  import matplotlib.font_manager as fm
7
  import matplotlib as mpl
8
  import io
9
+ import time
10
 
11
+ # 確保正確的中文字符編碼
12
  st.set_page_config(page_title="ESG 專題數據分析", page_icon=":chart_with_upwards_trend:", layout="wide")
13
 
14
+ # 定義 URL
15
  urls = {
16
  "溫室氣體": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
17
  "能源": "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
18
  "董事會揭露": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
19
  }
20
 
21
+ # 下載並加載 CSV 文件到 DataFrame 的函數
22
+ @st.cache_data
23
  def load_data():
24
  dataframes = {}
25
  for name, url in urls.items():
26
  response = requests.get(url)
27
+ response.encoding = 'utf-8'
28
  df = pd.read_csv(io.StringIO(response.text), encoding='utf-8')
29
+ df = df.fillna(0)
30
  dataframes[name] = df
31
  return dataframes
32
 
33
+ # Streamlit 應用
 
 
 
34
  st.title("ESG 專題數據分析")
35
 
36
+ # 添加一個加載數據的按鈕
37
+ if st.button("加載數據"):
38
+ # 顯示進度條
39
+ progress_bar = st.progress(0)
40
+ for i in range(100):
41
+ time.sleep(0.01)
42
+ progress_bar.progress(i + 1)
43
+
44
+ # 加載所有數據集
45
+ dataframes = load_data()
46
+ st.success("數據加載完成!")
47
+
48
+ # 允許用戶選擇數據集
49
+ dataset_choice = st.selectbox("選擇要顯示的數據集", list(dataframes.keys()))
50
 
51
+ # 獲取選定的 DataFrame
52
+ selected_df = dataframes[dataset_choice]
53
 
54
+ # 允許用戶選擇用於繪製圓餅圖的列
55
+ column_choice = st.selectbox("選擇欄位來繪製圖表", selected_df.columns)
56
 
57
+ # 檢查該列是否為數值類型
58
+ if pd.api.types.is_numeric_dtype(selected_df[column_choice]):
59
+ # 創建一個標籤頁布局
60
+ tab1, tab2 = st.tabs(["圓餅圖", "長條圖"])
61
+
62
+ with tab1:
63
+ # 使用 plotly 創建圓餅圖
64
+ fig_pie = px.pie(selected_df, names=selected_df.index, values=column_choice,
65
+ title=f"{dataset_choice} - {column_choice} 圓餅圖")
66
+ st.plotly_chart(fig_pie)
67
+
68
+ with tab2:
69
+ # 使用 plotly 創建長條圖
70
+ fig_bar = px.bar(selected_df, x=selected_df.index, y=column_choice,
71
+ title=f"{dataset_choice} - {column_choice} 長條圖")
72
+ st.plotly_chart(fig_bar)
73
+ else:
74
+ st.write("選定的欄位不是數值類型,無法繪製圖表。")
75
 
76
+ # 下載並設置自定義字體以顯示中文字符
77
  font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
78
  font_response = requests.get(font_url)
79
  with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file: