Rooobert commited on
Commit
973cdca
·
verified ·
1 Parent(s): e6d3b9b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import requests
4
+ import io
5
+
6
+ def fetch_data(url):
7
+ """從 URL 獲取資料並返回 DataFrame"""
8
+ try:
9
+ response = requests.get(url)
10
+ response.raise_for_status()
11
+ return pd.read_csv(io.StringIO(response.content.decode('utf-8-sig')))
12
+ except Exception as e:
13
+ return f"錯誤:{str(e)}"
14
+
15
+ def load_board_data():
16
+ """載入董事會資料"""
17
+ url = "https://mopsfin.twse.com.tw/opendata/t187ap46_O_16.csv"
18
+ df = fetch_data(url)
19
+ if isinstance(df, pd.DataFrame):
20
+ return df, "董事會資料載入成功!"
21
+ return None, df
22
+
23
+ def load_ghg_data():
24
+ """載入溫室氣體排放資料"""
25
+ url = "https://mopsfin.twse.com.tw/opendata/t187ap46_O_6.csv"
26
+ df = fetch_data(url)
27
+ if isinstance(df, pd.DataFrame):
28
+ return df, "溫室氣體排放資料載入成功!"
29
+ return None, df
30
+
31
+ def load_esg_data():
32
+ """載入 ESG 綜合資料"""
33
+ url = "https://mopsfin.twse.com.tw/opendata/t187ap46_O_1.csv"
34
+ df = fetch_data(url)
35
+ if isinstance(df, pd.DataFrame):
36
+ df = df.dropna()
37
+ return df, "ESG 綜合資料載入成功!"
38
+ return None, df
39
+
40
+ def process_and_display(data_choice):
41
+ """處理並顯示所選資料"""
42
+ if data_choice == "董事會":
43
+ df, message = load_board_data()
44
+ elif data_choice == "溫室氣體":
45
+ df, message = load_ghg_data()
46
+ else:
47
+ df, message = load_esg_data()
48
+
49
+ if df is not None:
50
+ # 計算基本統計資訊
51
+ stats = f"""
52
+ 資料列數:{len(df)}
53
+ 資料欄位:{', '.join(df.columns)}
54
+ """
55
+ return df.head().to_html(), stats, message
56
+ return "", "", message
57
+
58
+ # 創建 Gradio 介面
59
+ with gr.Blocks() as app:
60
+ gr.Markdown("# 上櫃公司 ESG 資訊查詢系統")
61
+
62
+ with gr.Row():
63
+ data_choice = gr.Radio(
64
+ choices=["董事會", "溫室氣體", "ESG綜合"],
65
+ label="選擇資料類型",
66
+ value="董事會"
67
+ )
68
+
69
+ with gr.Row():
70
+ load_btn = gr.Button("載入資料")
71
+
72
+ with gr.Row():
73
+ output_message = gr.Textbox(label="狀態訊息")
74
+
75
+ with gr.Row():
76
+ stats_display = gr.Textbox(label="資料統計", lines=5)
77
+
78
+ with gr.Row():
79
+ data_display = gr.HTML(label="資料預覽")
80
+
81
+ load_btn.click(
82
+ process_and_display,
83
+ inputs=[data_choice],
84
+ outputs=[data_display, stats_display, output_message]
85
+ )
86
+
87
+ if __name__ == "__main__":
88
+ app.launch()