Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import requests
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
import matplotlib.font_manager as fm
|
| 7 |
+
import matplotlib as mpl
|
| 8 |
+
|
| 9 |
+
# Define URLs
|
| 10 |
+
urls = {
|
| 11 |
+
"溫室氣體": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
|
| 12 |
+
"能源": "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
|
| 13 |
+
"董事會揭露": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Function to download and load CSV files into DataFrame
|
| 17 |
+
def load_data():
|
| 18 |
+
dataframes = {}
|
| 19 |
+
for name, url in urls.items():
|
| 20 |
+
response = requests.get(url)
|
| 21 |
+
df = pd.read_csv(pd.compat.StringIO(response.text))
|
| 22 |
+
df = df.fillna(0) # Replace missing values with 0
|
| 23 |
+
dataframes[name] = df
|
| 24 |
+
return dataframes
|
| 25 |
+
|
| 26 |
+
# Load all datasets
|
| 27 |
+
dataframes = load_data()
|
| 28 |
+
|
| 29 |
+
# Streamlit App
|
| 30 |
+
st.title("ESG 專題數據分析")
|
| 31 |
+
|
| 32 |
+
# Allow user to select dataset
|
| 33 |
+
dataset_choice = st.selectbox("選擇要顯示的數據集", list(dataframes.keys()))
|
| 34 |
+
|
| 35 |
+
# Get the selected dataframe
|
| 36 |
+
selected_df = dataframes[dataset_choice]
|
| 37 |
+
|
| 38 |
+
# Allow user to select a column for pie chart
|
| 39 |
+
column_choice = st.selectbox("選擇欄位來繪製圓餅圖", selected_df.columns)
|
| 40 |
+
|
| 41 |
+
# Check if the column is numerical
|
| 42 |
+
if pd.api.types.is_numeric_dtype(selected_df[column_choice]):
|
| 43 |
+
# Create a pie chart using plotly
|
| 44 |
+
fig = px.pie(selected_df, names=selected_df.index, values=column_choice, title=f"{dataset_choice} - {column_choice} 圓餅圖")
|
| 45 |
+
st.plotly_chart(fig)
|
| 46 |
+
else:
|
| 47 |
+
st.write("選定的欄位不是數值類型,無法繪製圓餅圖。")
|
| 48 |
+
|
| 49 |
+
# Download and set custom font for displaying Chinese characters
|
| 50 |
+
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|
| 51 |
+
font_response = requests.get(font_url)
|
| 52 |
+
with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file:
|
| 53 |
+
font_file.write(font_response.content)
|
| 54 |
+
|
| 55 |
+
fm.fontManager.addfont("TaipeiSansTCBeta-Regular.ttf")
|
| 56 |
+
mpl.rc('font', family='Taipei Sans TC Beta')
|