Roberta2024 commited on
Commit
89bee7d
·
verified ·
1 Parent(s): 7028711

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -34
app.py CHANGED
@@ -2,6 +2,7 @@ import pandas as pd
2
  import requests
3
  import plotly.express as px
4
  import streamlit as st
 
5
  import matplotlib.font_manager as fm
6
  import matplotlib as mpl
7
 
@@ -25,45 +26,45 @@ def download_csv(url, filename):
25
  return df
26
 
27
  # URLs to download data from
28
- urls = [
29
- "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv", # Original file - ESG
30
- "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv", # Renewable Energy
31
- "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv" # Board of Directors
32
- ]
33
 
34
  # Filenames to save CSV data
35
- filenames = ["data01.csv", "data02.csv", "data03.csv"]
36
-
37
- # Download and process each CSV
38
- dfs = [download_csv(url, filename) for url, filename in zip(urls, filenames)]
 
39
 
40
  # Streamlit app title
41
  st.title("ESG Data Visualization with Streamlit")
42
 
43
- # Pie Chart for the first DataFrame
44
- st.header("Pie Chart for ESG Data")
45
- fig_pie = px.pie(
46
- dfs[0],
47
- names=dfs[0].columns[0],
48
- values=dfs[0].columns[1],
49
- title="Pie Chart for ESG Data"
50
- )
51
- fig_pie.update_layout(font_family="Taipei Sans TC Beta")
52
- st.plotly_chart(fig_pie)
53
-
54
- # Bar Chart for the second DataFrame
55
- st.header("Bar Chart for Renewable Energy Data")
56
- fig_bar = px.bar(
57
- dfs[1],
58
- x=dfs[1].columns[0],
59
- y=dfs[1].columns[1],
60
- title="Bar Chart for Renewable Energy Data"
61
- )
62
- fig_bar.update_layout(font_family="Taipei Sans TC Beta")
63
- st.plotly_chart(fig_bar)
64
 
65
- # Optionally, show the raw DataFrames
66
- st.header("Raw DataFrames")
67
- for i, df in enumerate(dfs):
68
- st.subheader(f"DataFrame {i + 1}")
 
 
 
 
 
 
69
  st.dataframe(df)
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  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
 
 
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
  # 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
+ # Plotting the data
61
+ if selected_option == "溫室氣體":
62
+ fig = px.pie(df, names=df.columns[0], values=df.columns[1], title=f"Pie Chart for {selected_option}")
63
+ elif selected_option == "再生能源":
64
+ fig = px.bar(df, x=df.columns[0], y=df.columns[1], title=f"Bar Chart for {selected_option}")
65
+ elif selected_option == "董事會":
66
+ fig = px.bar(df, x=df.columns[0], y=df.columns[1], title=f"Bar Chart for {selected_option}")
67
+
68
+ # Apply custom font to the plot
69
+ fig.update_layout(font_family="Taipei Sans TC Beta")
70
+ st.plotly_chart(fig)