Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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 |
+
|
8 |
+
# Download and set custom font
|
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 |
+
# Register the font with matplotlib
|
15 |
+
fm.fontManager.addfont("TaipeiSansTCBeta-Regular.ttf")
|
16 |
+
mpl.rc('font', family='Taipei Sans TC Beta')
|
17 |
+
|
18 |
+
# Function to download and load CSV into a DataFrame
|
19 |
+
def download_csv(url, filename):
|
20 |
+
response = requests.get(url)
|
21 |
+
with open(filename, "wb") as file:
|
22 |
+
file.write(response.content)
|
23 |
+
df = pd.read_csv(filename)
|
24 |
+
df = df.fillna(0) # Fill missing values with 0
|
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)
|