Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import requests | |
| import urllib3 | |
| import gradio as gr | |
| # 關閉 SSL 警告 | |
| urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
| # 下載並讀取資料 | |
| def load_data(): | |
| url = "https://data.tainan.gov.tw/File/ResourceCsvDownload/d7349de7-2260-436f-afcb-6699373f0460" | |
| response = requests.get(url, verify=False) | |
| with open("data01.csv", "wb") as file: | |
| file.write(response.content) | |
| df = pd.read_csv("data01.csv", encoding="utf-8-sig") | |
| return df | |
| # 篩選邏輯 | |
| def filter_parking(district, min_spaces, top_n): | |
| try: | |
| df = load_data() | |
| # 動態篩選區域與車位數 | |
| df_filtered = df[ | |
| (df["停車場地址"].str.contains(district)) & | |
| (df["一般小型車數量"] > min_spaces) | |
| ] | |
| # 取前 N 筆 | |
| df_top = df_filtered.head(top_n) | |
| # 只顯示地址與車位數 | |
| df_result = df_top[["停車場地址", "一般小型車數量"]] | |
| return df_result | |
| except Exception as e: | |
| return pd.DataFrame({"錯誤": [str(e)]}) | |
| # Gradio 介面 | |
| with gr.Blocks(title="台南市停車場查詢系統") as demo: | |
| gr.Markdown("# 🅿️ 台南市停車場查詢系統") | |
| gr.Markdown("資料來源:台南市政府開放資料平台") | |
| with gr.Row(): | |
| district_input = gr.Textbox( | |
| label="行政區關鍵字", | |
| value="安南區", | |
| placeholder="例如:安南區、東區、北區" | |
| ) | |
| min_spaces_input = gr.Slider( | |
| label="一般小型車數量下限", | |
| minimum=0, | |
| maximum=500, | |
| value=30, | |
| step=5 | |
| ) | |
| top_n_input = gr.Slider( | |
| label="顯示筆數", | |
| minimum=1, | |
| maximum=20, | |
| value=3, | |
| step=1 | |
| ) | |
| search_btn = gr.Button("🔍 查詢", variant="primary") | |
| output_table = gr.Dataframe( | |
| label="查詢結果", | |
| headers=["停車場地址", "一般小型車數量"], | |
| interactive=False | |
| ) | |
| search_btn.click( | |
| fn=filter_parking, | |
| inputs=[district_input, min_spaces_input, top_n_input], | |
| outputs=output_table | |
| ) | |
| demo.launch() |