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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -36
app.py CHANGED
@@ -5,6 +5,7 @@ import streamlit as st
5
  import time
6
  import matplotlib.font_manager as fm
7
  import matplotlib as mpl
 
8
 
9
  # Download and set custom font
10
  font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
@@ -17,11 +18,10 @@ fm.fontManager.addfont("TaipeiSansTCBeta-Regular.ttf")
17
  mpl.rc('font', family='Taipei Sans TC Beta')
18
 
19
  # Function to download and load CSV into a DataFrame
20
- def download_csv(url, filename):
 
21
  response = requests.get(url)
22
- with open(filename, "wb") as file:
23
- file.write(response.content)
24
- df = pd.read_csv(filename)
25
  df = df.fillna(0) # Fill missing values with 0
26
  return df
27
 
@@ -32,44 +32,78 @@ urls = {
32
  "董事會": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv" # Board of Directors
33
  }
34
 
35
- # Filenames to save CSV data
36
- filenames = {
37
- "溫室氣體": "data01.csv",
38
- "再生能源": "data02.csv",
39
- "董事會": "data03.csv"
40
- }
41
 
42
- # Streamlit app title
43
- st.title("ESG Data Visualization with Streamlit")
 
 
 
 
 
 
 
 
 
44
 
45
- # Dropdown menu for data selection
46
- selected_option = st.selectbox("選擇資料類型", options=list(urls.keys()))
 
 
47
 
48
- # Button to load data
49
- if st.button("載入資料"):
50
  with st.spinner('資料載入中...'):
51
- # Simulate a delay for the process bar
52
- for _ in range(5):
53
- time.sleep(0.2)
54
- df = download_csv(urls[selected_option], filenames[selected_option])
 
 
 
55
 
56
- # Displaying the DataFrame
57
  st.header(f"{selected_option} 資料")
58
- st.dataframe(df)
59
-
60
- # Column selection for visualization
61
- x_col = st.selectbox("選擇類別欄位 (X 軸)", options=df.columns)
62
- y_col = st.selectbox("選擇數值欄位 (Y 軸)", options=df.columns)
63
-
64
- # Chart type selection
65
- chart_type = st.radio("選擇圖表類型", ("圓餅圖", "柱狀圖"))
66
-
67
- # Plotting the data based on user selection
 
 
68
  if chart_type == "圓餅圖":
69
- fig = px.pie(df, names=x_col, values=y_col, title=f"Pie Chart for {selected_option}")
70
  elif chart_type == "柱狀圖":
71
- fig = px.bar(df, x=x_col, y=y_col, title=f"Bar Chart for {selected_option}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- # Apply custom font to the plot
74
- fig.update_layout(font_family="Taipei Sans TC Beta")
75
- st.plotly_chart(fig)
 
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"
 
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
 
 
32
  "董事會": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv" # Board of Directors
33
  }
34
 
35
+ # Set page config for a wider layout
36
+ st.set_page_config(layout="wide", page_title="ESG Data Visualization")
 
 
 
 
37
 
38
+ # Streamlit app title with custom styling
39
+ st.markdown("""
40
+ <style>
41
+ .big-font {
42
+ font-size:50px !important;
43
+ color: #1E88E5;
44
+ font-family: 'Taipei Sans TC Beta';
45
+ }
46
+ </style>
47
+ """, unsafe_allow_html=True)
48
+ st.markdown('<p class="big-font">ESG Data Visualization</p>', unsafe_allow_html=True)
49
 
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("載入資料"):
57
  with st.spinner('資料載入中...'):
58
+ # Add a progress bar
59
+ progress_bar = st.progress(0)
60
+ for i in range(100):
61
+ time.sleep(0.01)
62
+ progress_bar.progress(i + 1)
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)
100
+ st.download_button(
101
+ label="下載資料為 CSV",
102
+ data=csv,
103
+ file_name=f"{selected_option}_data.csv",
104
+ mime="text/csv",
105
+ )
106
 
107
+ # Footer
108
+ st.markdown("---")
109
+ st.markdown("Created with ❤️ using Streamlit")