File size: 1,963 Bytes
579e6f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
import streamlit as st
import pandas as pd
import requests
import plotly.express as px
import matplotlib.font_manager as fm
import matplotlib as mpl

# Define URLs
urls = {
    "溫室氣體": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
    "能源": "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
    "董事會揭露": "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
}

# Function to download and load CSV files into DataFrame
def load_data():
    dataframes = {}
    for name, url in urls.items():
        response = requests.get(url)
        df = pd.read_csv(pd.compat.StringIO(response.text))
        df = df.fillna(0)  # Replace missing values with 0
        dataframes[name] = df
    return dataframes

# Load all datasets
dataframes = load_data()

# Streamlit App
st.title("ESG 專題數據分析")

# Allow user to select dataset
dataset_choice = st.selectbox("選擇要顯示的數據集", list(dataframes.keys()))

# Get the selected dataframe
selected_df = dataframes[dataset_choice]

# Allow user to select a column for pie chart
column_choice = st.selectbox("選擇欄位來繪製圓餅圖", selected_df.columns)

# Check if the column is numerical
if pd.api.types.is_numeric_dtype(selected_df[column_choice]):
    # Create a pie chart using plotly
    fig = px.pie(selected_df, names=selected_df.index, values=column_choice, title=f"{dataset_choice} - {column_choice} 圓餅圖")
    st.plotly_chart(fig)
else:
    st.write("選定的欄位不是數值類型,無法繪製圓餅圖。")

# Download and set custom font for displaying Chinese characters
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
font_response = requests.get(font_url)
with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file:
    font_file.write(font_response.content)

fm.fontManager.addfont("TaipeiSansTCBeta-Regular.ttf")
mpl.rc('font', family='Taipei Sans TC Beta')