Roberta2024 commited on
Commit
3a5a6b0
·
verified ·
1 Parent(s): e85f2f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -39
app.py CHANGED
@@ -3,33 +3,27 @@ import requests
3
  import plotly.express as px
4
  import streamlit as st
5
  import time
6
- import matplotlib.font_manager as fm
7
- import matplotlib as mpl
8
  from io import StringIO
9
 
10
- # Download and set custom font
11
  font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
12
  font_response = requests.get(font_url)
13
  with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file:
14
  font_file.write(font_response.content)
15
 
16
- # Register the font with matplotlib
17
- fm.fontManager.addfont("TaipeiSansTCBeta-Regular.ttf")
18
- mpl.rc('font', family='Taipei Sans TC Beta')
19
-
20
  # Function to download and load CSV into a DataFrame
21
- @st.cache_data # Add caching to improve performance
22
  def download_csv(url):
23
  response = requests.get(url)
24
  df = pd.read_csv(StringIO(response.text))
25
  df = df.fillna(0) # Fill missing values with 0
26
  return df
27
 
28
- # URLs to download data from
29
  urls = {
30
- "溫室氣體": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv", # ESG
31
- "再生能源": "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv", # Renewable Energy
32
- "董事會": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv" # Board of Directors
33
  }
34
 
35
  # Set page config for a wider layout
@@ -50,7 +44,6 @@ st.markdown('<p class="big-font">ESG Data Visualization</p>', unsafe_allow_html=
50
  # Sidebar for data selection and chart options
51
  st.sidebar.header("設置")
52
  selected_option = st.sidebar.selectbox("選擇資料類型", options=list(urls.keys()))
53
- chart_type = st.sidebar.radio("選擇圖表類型", ("圓餅圖", "柱狀圖", "折線圖"))
54
 
55
  # Main content area
56
  if st.sidebar.button("載入資料"):
@@ -63,37 +56,41 @@ if st.sidebar.button("載入資料"):
63
 
64
  df = download_csv(urls[selected_option])
65
 
66
- # Displaying the DataFrame with styling
67
  st.header(f"{selected_option} 資料")
68
- st.dataframe(df.style.highlight_max(axis=0))
69
 
70
  # Data summary
71
  st.subheader("資料摘要")
72
- col1, col2 = st.columns(2)
73
- with col1:
74
- st.write(df.describe())
75
- with col2:
76
- st.write(df.info())
77
-
78
- # Visualization
79
- st.subheader("資料視覺化")
80
- if chart_type == "圓餅圖":
81
- fig = px.pie(df, names=df.columns[0], values=df.columns[1], title=f"{selected_option} 分佈")
82
- elif chart_type == "柱狀圖":
83
- fig = px.bar(df, x=df.columns[0], y=df.columns[1], title=f"{selected_option} 比較")
84
- else: # 折線圖
85
- fig = px.line(df, x=df.columns[0], y=df.columns[1], title=f"{selected_option} 趨勢")
86
-
87
- # Apply custom font and styling to the plot
88
- fig.update_layout(
89
- font_family="Taipei Sans TC Beta",
90
- title_font_size=24,
91
- legend_title_font_size=14,
92
- legend_font_size=12
93
- )
94
 
95
- # Display the chart
96
- st.plotly_chart(fig, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  # Add download button for the data
99
  csv = df.to_csv(index=False)
 
3
  import plotly.express as px
4
  import streamlit as st
5
  import time
 
 
6
  from io import StringIO
7
 
8
+ # Download and set custom font (unchanged)
9
  font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
10
  font_response = requests.get(font_url)
11
  with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file:
12
  font_file.write(font_response.content)
13
 
 
 
 
 
14
  # Function to download and load CSV into a DataFrame
15
+ @st.cache_data
16
  def download_csv(url):
17
  response = requests.get(url)
18
  df = pd.read_csv(StringIO(response.text))
19
  df = df.fillna(0) # Fill missing values with 0
20
  return df
21
 
22
+ # URLs to download data from (unchanged)
23
  urls = {
24
+ "溫室氣體": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
25
+ "再生能源": "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
26
+ "董事會": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
27
  }
28
 
29
  # Set page config for a wider layout
 
44
  # Sidebar for data selection and chart options
45
  st.sidebar.header("設置")
46
  selected_option = st.sidebar.selectbox("選擇資料類型", options=list(urls.keys()))
 
47
 
48
  # Main content area
49
  if st.sidebar.button("載入資料"):
 
56
 
57
  df = download_csv(urls[selected_option])
58
 
59
+ # Displaying the DataFrame without styling
60
  st.header(f"{selected_option} 資料")
61
+ st.dataframe(df)
62
 
63
  # Data summary
64
  st.subheader("資料摘要")
65
+ st.write(df.describe())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # Column selection for visualization
68
+ numeric_columns = df.select_dtypes(include=['int64', 'float64']).columns
69
+ if len(numeric_columns) > 1:
70
+ x_column = st.selectbox("選擇 X 軸欄位", options=df.columns)
71
+ y_column = st.selectbox("選擇 Y 軸欄位", options=numeric_columns)
72
+
73
+ # Visualization
74
+ st.subheader("資料視覺化")
75
+ chart_type = st.radio("選擇圖表類型", ("圓餅圖", "柱狀圖"))
76
+
77
+ if chart_type == "圓餅圖":
78
+ fig = px.pie(df, names=x_column, values=y_column, title=f"{selected_option} 分佈")
79
+ else: # 柱狀圖
80
+ fig = px.bar(df, x=x_column, y=y_column, title=f"{selected_option} 比較")
81
+
82
+ # Apply custom font and styling to the plot
83
+ fig.update_layout(
84
+ font_family="Taipei Sans TC Beta",
85
+ title_font_size=24,
86
+ legend_title_font_size=14,
87
+ legend_font_size=12
88
+ )
89
+
90
+ # Display the chart
91
+ st.plotly_chart(fig, use_container_width=True)
92
+ else:
93
+ st.warning("沒有足夠的數值型欄位來創建圖表。請選擇另一個數據集。")
94
 
95
  # Add download button for the data
96
  csv = df.to_csv(index=False)