Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ import matplotlib.pyplot as plt
|
|
| 10 |
# === grafanalib: 定義 Dashboard 結構(不負責畫圖) ===
|
| 11 |
# 注意:grafanalib 只產生 Grafana JSON;我們會把 JSON 顯示出來,並用 Matplotlib 實際畫圖
|
| 12 |
from grafanalib.core import (
|
| 13 |
-
Dashboard, Graph, Row, Target, YAxis, YAxes, #
|
| 14 |
)
|
| 15 |
|
| 16 |
TAIPEI = tz.gettz("Asia/Taipei")
|
|
@@ -40,7 +40,7 @@ def load_csv(file: gr.File | None) -> pd.DataFrame:
|
|
| 40 |
df[time_col] = pd.to_datetime(df[time_col])
|
| 41 |
df.rename(columns={time_col: "time"}, inplace=True)
|
| 42 |
# 保證有時區(預設轉為台北)
|
| 43 |
-
if df["time"].dt
|
| 44 |
df["time"] = df["time"].dt.tz_localize(TAIPEI)
|
| 45 |
return df
|
| 46 |
|
|
@@ -59,8 +59,8 @@ def build_grafanalib_dashboard(series_columns: list[str]) -> dict:
|
|
| 59 |
dataSource="(example)",
|
| 60 |
targets=targets,
|
| 61 |
yAxes=YAxes(
|
| 62 |
-
left=YAxis(format="
|
| 63 |
-
right=YAxis(format="
|
| 64 |
),
|
| 65 |
)
|
| 66 |
]
|
|
@@ -72,8 +72,8 @@ def build_grafanalib_dashboard(series_columns: list[str]) -> dict:
|
|
| 72 |
dataSource="(example)",
|
| 73 |
targets=[Target(expr=f"{series_columns[1]}", legendFormat=series_columns[1])],
|
| 74 |
yAxes=YAxes(
|
| 75 |
-
left=YAxis(format="
|
| 76 |
-
right=YAxis(format="
|
| 77 |
),
|
| 78 |
)
|
| 79 |
)
|
|
@@ -82,8 +82,7 @@ def build_grafanalib_dashboard(series_columns: list[str]) -> dict:
|
|
| 82 |
title="Seismology Demo Dashboard (grafanalib + Gradio)",
|
| 83 |
rows=[Row(panels=panels)],
|
| 84 |
timezone="browser",
|
| 85 |
-
|
| 86 |
-
time_to="now",
|
| 87 |
)
|
| 88 |
return dash.to_json_data()
|
| 89 |
|
|
|
|
| 10 |
# === grafanalib: 定義 Dashboard 結構(不負責畫圖) ===
|
| 11 |
# 注意:grafanalib 只產生 Grafana JSON;我們會把 JSON 顯示出來,並用 Matplotlib 實際畫圖
|
| 12 |
from grafanalib.core import (
|
| 13 |
+
Dashboard, Graph, Row, Target, YAxis, YAxes, TimeRange # ⬅️ 加入 TimeRange
|
| 14 |
)
|
| 15 |
|
| 16 |
TAIPEI = tz.gettz("Asia/Taipei")
|
|
|
|
| 40 |
df[time_col] = pd.to_datetime(df[time_col])
|
| 41 |
df.rename(columns={time_col: "time"}, inplace=True)
|
| 42 |
# 保證有時區(預設轉為台北)
|
| 43 |
+
if getattr(df["time"].dt, "tz", None) is None:
|
| 44 |
df["time"] = df["time"].dt.tz_localize(TAIPEI)
|
| 45 |
return df
|
| 46 |
|
|
|
|
| 59 |
dataSource="(example)",
|
| 60 |
targets=targets,
|
| 61 |
yAxes=YAxes(
|
| 62 |
+
left=YAxis(format="short"), # 用 Grafana 內建單位 key;避免不支援的 "none"
|
| 63 |
+
right=YAxis(format="short"),
|
| 64 |
),
|
| 65 |
)
|
| 66 |
]
|
|
|
|
| 72 |
dataSource="(example)",
|
| 73 |
targets=[Target(expr=f"{series_columns[1]}", legendFormat=series_columns[1])],
|
| 74 |
yAxes=YAxes(
|
| 75 |
+
left=YAxis(format="short"),
|
| 76 |
+
right=YAxis(format="short"),
|
| 77 |
),
|
| 78 |
)
|
| 79 |
)
|
|
|
|
| 82 |
title="Seismology Demo Dashboard (grafanalib + Gradio)",
|
| 83 |
rows=[Row(panels=panels)],
|
| 84 |
timezone="browser",
|
| 85 |
+
time=TimeRange("now-1h", "now"), # ⬅️ 正確做法:用 TimeRange 取代 time_from/time_to
|
|
|
|
| 86 |
)
|
| 87 |
return dash.to_json_data()
|
| 88 |
|