Spaces:
Running
Running
File size: 2,524 Bytes
973cdca df537f8 973cdca |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import gradio as gr
import pandas as pd
import requests
import io
def fetch_data(url):
"""從 URL 獲取資料並返回 DataFrame"""
try:
response = requests.get(url)
response.raise_for_status()
return pd.read_csv(io.StringIO(response.content.decode('utf-8-sig')))
except Exception as e:
return f"錯誤:{str(e)}"
def load_board_data():
"""載入董事會資料"""
url = "https://mopsfin.twse.com.tw/opendata/t187ap46_O_16.csv"
df = fetch_data(url)
if isinstance(df, pd.DataFrame):
return df, "董事會資料載入成功!"
return None, df
def load_ghg_data():
"""載入溫室氣體排放資料"""
url = "https://mopsfin.twse.com.tw/opendata/t187ap46_O_6.csv"
df = fetch_data(url)
if isinstance(df, pd.DataFrame):
return df, "溫室氣體排放資料載入成功!"
return None, df
def load_esg_data():
"""載入 ESG 綜合資料"""
url = "https://mopsfin.twse.com.tw/opendata/t187ap46_O_1.csv"
df = fetch_data(url)
if isinstance(df, pd.DataFrame):
df = df.dropna()
return df, "ESG 綜合資料載入成功!"
return None, df
def process_and_display(data_choice):
"""處理並顯示所選資料"""
if data_choice == "董事會":
df, message = load_board_data()
elif data_choice == "溫室氣體":
df, message = load_ghg_data()
else:
df, message = load_esg_data()
if df is not None:
# 計算基本統計資訊
stats = f"""
資料列數:{len(df)}
資料欄位:{', '.join(df.columns)}
"""
return df.head().to_html(), stats, message
return "", "", message
# 創建 Gradio 介面
with gr.Blocks() as app:
gr.Markdown("# 上櫃公司 ESG 資訊查詢系統")
with gr.Row():
data_choice = gr.Radio(
choices=["董事會", "溫室氣體", "ESG綜合"],
label="選擇資料類型",
value="董事會"
)
with gr.Row():
load_btn = gr.Button("載入資料")
with gr.Row():
output_message = gr.Textbox(label="狀態訊息")
with gr.Row():
stats_display = gr.Textbox(label="資料統計", lines=5)
with gr.Row():
data_display = gr.HTML(label="資料預覽")
load_btn.click(
process_and_display,
inputs=[data_choice],
outputs=[data_display, stats_display, output_message]
)
if __name__ == "__main__":
app.launch() |