Update app.py
Browse files
app.py
CHANGED
|
@@ -2,60 +2,77 @@ import sqlite3
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
async def fetch_earthquake_data(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
conn = sqlite3.connect('earthquake_data.db')
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
filters = {
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"ML >= ?": ML_min, "ML <= ?": ML_max,
|
| 20 |
}
|
| 21 |
|
| 22 |
-
for condition,
|
| 23 |
-
|
|
|
|
| 24 |
query += f" AND {condition}"
|
| 25 |
-
params.
|
| 26 |
|
| 27 |
df = pd.read_sql_query(query, conn, params=tuple(params))
|
| 28 |
conn.close()
|
| 29 |
|
| 30 |
-
# 如果沒有結果,顯示一個空的 DataFrame 而不是錯誤
|
| 31 |
if df.empty:
|
| 32 |
return pd.DataFrame()
|
| 33 |
|
| 34 |
return df
|
| 35 |
except Exception as e:
|
| 36 |
-
# 在介面上顯示錯誤訊息
|
| 37 |
return pd.DataFrame({"Error": [str(e)]})
|
| 38 |
|
| 39 |
|
| 40 |
-
# 建立 Gradio
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("# Earthquake Data Explorer")
|
| 43 |
gr.Markdown("Use the filters below to search the earthquake catalog.")
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Row():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
with gr.Row():
|
| 51 |
-
|
| 52 |
-
|
| 53 |
with gr.Row():
|
| 54 |
-
depth_min_input = gr.Number(label="
|
| 55 |
-
depth_max_input = gr.Number(label="
|
| 56 |
with gr.Row():
|
| 57 |
-
ML_min_input = gr.Number(label="
|
| 58 |
-
ML_max_input = gr.Number(label="
|
| 59 |
|
| 60 |
filter_button = gr.Button("Filter Data")
|
| 61 |
|
|
@@ -64,7 +81,8 @@ with gr.Blocks() as demo:
|
|
| 64 |
filter_button.click(
|
| 65 |
fn=fetch_earthquake_data,
|
| 66 |
inputs=[
|
| 67 |
-
|
|
|
|
| 68 |
depth_min_input, depth_max_input, ML_min_input, ML_max_input
|
| 69 |
],
|
| 70 |
outputs=output_df
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# 後端資料查詢函式,已更新以支援日期時間區間
|
| 6 |
+
async def fetch_earthquake_data(
|
| 7 |
+
start_date, start_time, end_date, end_time,
|
| 8 |
+
lat_min, lat_max, lon_min, lon_max,
|
| 9 |
+
depth_min, depth_max, ML_min, ML_max
|
| 10 |
+
):
|
| 11 |
try:
|
| 12 |
+
# 組合日期和時間,如果時間為空,則使用預設值
|
| 13 |
+
start_datetime_str = f"{start_date.strip()} {start_time.strip() if start_time and start_time.strip() else '00:00:00'}"
|
| 14 |
+
end_datetime_str = f"{end_date.strip()} {end_time.strip() if end_time and end_time.strip() else '23:59:59'}"
|
| 15 |
+
|
| 16 |
conn = sqlite3.connect('earthquake_data.db')
|
| 17 |
|
| 18 |
+
# 使用 SQLite 的 `date || ' ' || time` 來組合欄位進行比較
|
| 19 |
+
query = "SELECT * FROM earthquakes WHERE (date || ' ' || time) BETWEEN ? AND ?"
|
| 20 |
+
params = [start_datetime_str, end_datetime_str]
|
| 21 |
|
| 22 |
+
# 處理其他數值範圍篩選條件
|
| 23 |
filters = {
|
| 24 |
+
"lat BETWEEN ? AND ?": (lat_min, lat_max),
|
| 25 |
+
"lon BETWEEN ? AND ?": (lon_min, lon_max),
|
| 26 |
+
"depth BETWEEN ? AND ?": (depth_min, depth_max),
|
| 27 |
+
"ML BETWEEN ? AND ?": (ML_min, ML_max),
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
+
for condition, values in filters.items():
|
| 31 |
+
# 確保兩個值都存在才加入篩選
|
| 32 |
+
if values[0] is not None and values[1] is not None:
|
| 33 |
query += f" AND {condition}"
|
| 34 |
+
params.extend(values)
|
| 35 |
|
| 36 |
df = pd.read_sql_query(query, conn, params=tuple(params))
|
| 37 |
conn.close()
|
| 38 |
|
|
|
|
| 39 |
if df.empty:
|
| 40 |
return pd.DataFrame()
|
| 41 |
|
| 42 |
return df
|
| 43 |
except Exception as e:
|
|
|
|
| 44 |
return pd.DataFrame({"Error": [str(e)]})
|
| 45 |
|
| 46 |
|
| 47 |
+
# 建立 Gradio 使用者介面,已更新欄位和預設值
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
gr.Markdown("# Earthquake Data Explorer")
|
| 50 |
gr.Markdown("Use the filters below to search the earthquake catalog.")
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
gr.Markdown("### 起訖日期與時間")
|
| 55 |
+
start_date_input = gr.Textbox(label="開始日期 (Start Date)", value="2024-01-01")
|
| 56 |
+
start_time_input = gr.Textbox(label="開始時間 (Start Time, HH:MM:SS)", placeholder="00:00:00")
|
| 57 |
+
with gr.Column():
|
| 58 |
+
gr.Markdown("### (空白)") # 佔位,讓排版好看
|
| 59 |
+
end_date_input = gr.Textbox(label="結束日期 (End Date)", value="2024-12-31")
|
| 60 |
+
end_time_input = gr.Textbox(label="結束時間 (End Time, HH:MM:SS)", placeholder="23:59:59")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
gr.Markdown("### 經緯度、深度、規模")
|
| 64 |
with gr.Row():
|
| 65 |
+
lon_min_input = gr.Number(label="經度 (Longitude) From", value=119)
|
| 66 |
+
lon_max_input = gr.Number(label="To", value=123)
|
| 67 |
with gr.Row():
|
| 68 |
+
lat_min_input = gr.Number(label="緯度 (Latitude) From", value=21)
|
| 69 |
+
lat_max_input = gr.Number(label="To", value=26)
|
| 70 |
with gr.Row():
|
| 71 |
+
depth_min_input = gr.Number(label="深度 (Depth) From", value=0)
|
| 72 |
+
depth_max_input = gr.Number(label="To", value=100)
|
| 73 |
with gr.Row():
|
| 74 |
+
ML_min_input = gr.Number(label="規模 (Magnitude) From", value=4.5)
|
| 75 |
+
ML_max_input = gr.Number(label="To", value=8)
|
| 76 |
|
| 77 |
filter_button = gr.Button("Filter Data")
|
| 78 |
|
|
|
|
| 81 |
filter_button.click(
|
| 82 |
fn=fetch_earthquake_data,
|
| 83 |
inputs=[
|
| 84 |
+
start_date_input, start_time_input, end_date_input, end_time_input,
|
| 85 |
+
lat_min_input, lat_max_input, lon_min_input, lon_max_input,
|
| 86 |
depth_min_input, depth_max_input, ML_min_input, ML_max_input
|
| 87 |
],
|
| 88 |
outputs=output_df
|