Spaces:
Paused
Paused
Update app.py via AI Editor
Browse files
app.py
CHANGED
|
@@ -167,45 +167,66 @@ def get_indices_1day(location_key, index_id):
|
|
| 167 |
logger.error(f"Error in get_indices_1day {index_id}: {e}")
|
| 168 |
return None
|
| 169 |
|
| 170 |
-
def
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
"details": "true"
|
| 176 |
-
}
|
| 177 |
try:
|
| 178 |
response = requests.get(url, params=params)
|
| 179 |
response.raise_for_status()
|
| 180 |
-
|
| 181 |
-
return data
|
| 182 |
except requests.RequestException as e:
|
| 183 |
-
logger.error(f"Error in
|
| 184 |
return None
|
| 185 |
|
| 186 |
-
def
|
| 187 |
-
if not
|
| 188 |
return dbc.Card([
|
| 189 |
dbc.CardBody([
|
| 190 |
-
html.H4("
|
| 191 |
-
html.P("No
|
| 192 |
])
|
| 193 |
], className="mb-4")
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
return dbc.Card([
|
| 203 |
dbc.CardBody([
|
| 204 |
-
html.H4("
|
| 205 |
-
|
| 206 |
-
html.P(f"Precipitation Type: {precip_type}"),
|
| 207 |
-
html.P(f"Start: {start_str}"),
|
| 208 |
-
html.P(f"End: {end_str}")
|
| 209 |
])
|
| 210 |
], className="mb-4")
|
| 211 |
|
|
@@ -382,7 +403,7 @@ app.layout = dbc.Container([
|
|
| 382 |
id="loading",
|
| 383 |
type="default",
|
| 384 |
children=[
|
| 385 |
-
html.Div(id="
|
| 386 |
html.Div(id="current-weather-output"),
|
| 387 |
html.Div(id="environmental-indices-output")
|
| 388 |
],
|
|
@@ -438,7 +459,7 @@ def set_session_cookie(response):
|
|
| 438 |
|
| 439 |
@app.callback(
|
| 440 |
[
|
| 441 |
-
Output("
|
| 442 |
Output("current-weather-output", "children"),
|
| 443 |
Output("environmental-indices-output", "children"),
|
| 444 |
Output("forecast-output", "children"),
|
|
@@ -456,7 +477,7 @@ def update_weather(location, session_data):
|
|
| 456 |
return [dbc.Spinner(color="primary"), "", "", ""]
|
| 457 |
|
| 458 |
lat, lon = location["latitude"], location["longitude"]
|
| 459 |
-
results = {"
|
| 460 |
def fetch_weather_data():
|
| 461 |
try:
|
| 462 |
location_key = get_data_from_session(session_id, "location_key")
|
|
@@ -469,7 +490,7 @@ def update_weather(location, session_data):
|
|
| 469 |
current = get_current_conditions(location_key)
|
| 470 |
forecast_5day = get_forecast_5day(location_key)
|
| 471 |
hourly_12 = get_hourly_forecast_12hour(location_key)
|
| 472 |
-
|
| 473 |
|
| 474 |
indices_dict = {}
|
| 475 |
for name, idx in INDEX_IDS.items():
|
|
@@ -478,7 +499,7 @@ def update_weather(location, session_data):
|
|
| 478 |
if current is None or forecast_5day is None:
|
| 479 |
raise ValueError("Failed to fetch weather data")
|
| 480 |
|
| 481 |
-
results["
|
| 482 |
results["current"] = create_current_weather_card(current)
|
| 483 |
results["indices"] = create_environmental_indices_card(indices_dict)
|
| 484 |
results["hourly12"] = create_hourly_12hour_card(hourly_12)
|
|
@@ -486,7 +507,7 @@ def update_weather(location, session_data):
|
|
| 486 |
save_session_data(session_id, "weather_results", results)
|
| 487 |
except Exception as e:
|
| 488 |
logger.error(f"Session {session_id} error: {str(e)}")
|
| 489 |
-
results["
|
| 490 |
results["current"] = ""
|
| 491 |
results["indices"] = ""
|
| 492 |
results["hourly12"] = ""
|
|
@@ -495,7 +516,6 @@ def update_weather(location, session_data):
|
|
| 495 |
html.P(f"Error fetching weather data: {str(e)}", className="text-danger")
|
| 496 |
])
|
| 497 |
], className="mb-4")
|
| 498 |
-
results["minutecast"] = ""
|
| 499 |
|
| 500 |
with lock:
|
| 501 |
fetch_weather_data()
|
|
@@ -503,7 +523,7 @@ def update_weather(location, session_data):
|
|
| 503 |
weather_results = get_data_from_session(session_id, "weather_results")
|
| 504 |
if weather_results:
|
| 505 |
return [
|
| 506 |
-
weather_results.get("
|
| 507 |
weather_results.get("current", ""),
|
| 508 |
weather_results.get("indices", ""),
|
| 509 |
html.Div([
|
|
|
|
| 167 |
logger.error(f"Error in get_indices_1day {index_id}: {e}")
|
| 168 |
return None
|
| 169 |
|
| 170 |
+
def get_weather_alarms_1day(location_key):
|
| 171 |
+
if location_key is None:
|
| 172 |
+
return None
|
| 173 |
+
url = f"{BASE_URL}/alarms/v1/1day/{location_key}"
|
| 174 |
+
params = {"apikey": API_KEY}
|
|
|
|
|
|
|
| 175 |
try:
|
| 176 |
response = requests.get(url, params=params)
|
| 177 |
response.raise_for_status()
|
| 178 |
+
return response.json()
|
|
|
|
| 179 |
except requests.RequestException as e:
|
| 180 |
+
logger.error(f"Error in get_weather_alarms_1day: {e}")
|
| 181 |
return None
|
| 182 |
|
| 183 |
+
def create_weather_alerts_card(alarms_data):
|
| 184 |
+
if not alarms_data or not isinstance(alarms_data, list) or len(alarms_data) == 0:
|
| 185 |
return dbc.Card([
|
| 186 |
dbc.CardBody([
|
| 187 |
+
html.H4("Weather Alerts", className="card-title"),
|
| 188 |
+
html.P("No weather alerts or alarms for today.")
|
| 189 |
])
|
| 190 |
], className="mb-4")
|
| 191 |
+
items = []
|
| 192 |
+
for entry in alarms_data:
|
| 193 |
+
date = entry.get("Date", "N/A")
|
| 194 |
+
try:
|
| 195 |
+
date_str = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S%z").strftime("%A, %B %d %Y")
|
| 196 |
+
except Exception:
|
| 197 |
+
date_str = date
|
| 198 |
+
alarms = entry.get("Alarms", [])
|
| 199 |
+
if not alarms or not isinstance(alarms, list):
|
| 200 |
+
continue
|
| 201 |
+
for alarm in alarms:
|
| 202 |
+
alarm_type = alarm.get("AlarmType", "N/A")
|
| 203 |
+
value = alarm.get("Value", {})
|
| 204 |
+
metric = value.get("Metric", {})
|
| 205 |
+
imperial = value.get("Imperial", {})
|
| 206 |
+
metric_val = metric.get("Value")
|
| 207 |
+
metric_unit = metric.get("Unit", "")
|
| 208 |
+
imperial_val = imperial.get("Value")
|
| 209 |
+
imperial_unit = imperial.get("Unit", "")
|
| 210 |
+
day = alarm.get("Day", {})
|
| 211 |
+
night = alarm.get("Night", {})
|
| 212 |
+
day_val = day.get("Imperial", {}).get("Value")
|
| 213 |
+
day_unit = day.get("Imperial", {}).get("Unit", "")
|
| 214 |
+
night_val = night.get("Imperial", {}).get("Value")
|
| 215 |
+
night_unit = night.get("Imperial", {}).get("Unit", "")
|
| 216 |
+
items.append(html.Div([
|
| 217 |
+
html.H6(f"{alarm_type} Alert", style={"fontWeight": "bold"}),
|
| 218 |
+
html.P(f"Date: {date_str}"),
|
| 219 |
+
html.P(f"Total Day: {day_val} {day_unit}" if day_val is not None else "Day: N/A"),
|
| 220 |
+
html.P(f"Total Night: {night_val} {night_unit}" if night_val is not None else "Night: N/A"),
|
| 221 |
+
html.P(f"Total Value: {imperial_val} {imperial_unit}" if imperial_val is not None else "Value: N/A"),
|
| 222 |
+
html.Hr(style={"marginTop": "0.75rem", "marginBottom": "0.75rem"})
|
| 223 |
+
]))
|
| 224 |
+
if not items:
|
| 225 |
+
items = [html.P("No weather alerts or alarms for today.")]
|
| 226 |
return dbc.Card([
|
| 227 |
dbc.CardBody([
|
| 228 |
+
html.H4("Weather Alerts", className="card-title"),
|
| 229 |
+
*items
|
|
|
|
|
|
|
|
|
|
| 230 |
])
|
| 231 |
], className="mb-4")
|
| 232 |
|
|
|
|
| 403 |
id="loading",
|
| 404 |
type="default",
|
| 405 |
children=[
|
| 406 |
+
html.Div(id="weather-alerts-output"),
|
| 407 |
html.Div(id="current-weather-output"),
|
| 408 |
html.Div(id="environmental-indices-output")
|
| 409 |
],
|
|
|
|
| 459 |
|
| 460 |
@app.callback(
|
| 461 |
[
|
| 462 |
+
Output("weather-alerts-output", "children"),
|
| 463 |
Output("current-weather-output", "children"),
|
| 464 |
Output("environmental-indices-output", "children"),
|
| 465 |
Output("forecast-output", "children"),
|
|
|
|
| 477 |
return [dbc.Spinner(color="primary"), "", "", ""]
|
| 478 |
|
| 479 |
lat, lon = location["latitude"], location["longitude"]
|
| 480 |
+
results = {"weather_alerts": "", "current": "", "indices": "", "hourly12": "", "forecast": ""}
|
| 481 |
def fetch_weather_data():
|
| 482 |
try:
|
| 483 |
location_key = get_data_from_session(session_id, "location_key")
|
|
|
|
| 490 |
current = get_current_conditions(location_key)
|
| 491 |
forecast_5day = get_forecast_5day(location_key)
|
| 492 |
hourly_12 = get_hourly_forecast_12hour(location_key)
|
| 493 |
+
alarms_1day = get_weather_alarms_1day(location_key)
|
| 494 |
|
| 495 |
indices_dict = {}
|
| 496 |
for name, idx in INDEX_IDS.items():
|
|
|
|
| 499 |
if current is None or forecast_5day is None:
|
| 500 |
raise ValueError("Failed to fetch weather data")
|
| 501 |
|
| 502 |
+
results["weather_alerts"] = create_weather_alerts_card(alarms_1day)
|
| 503 |
results["current"] = create_current_weather_card(current)
|
| 504 |
results["indices"] = create_environmental_indices_card(indices_dict)
|
| 505 |
results["hourly12"] = create_hourly_12hour_card(hourly_12)
|
|
|
|
| 507 |
save_session_data(session_id, "weather_results", results)
|
| 508 |
except Exception as e:
|
| 509 |
logger.error(f"Session {session_id} error: {str(e)}")
|
| 510 |
+
results["weather_alerts"] = ""
|
| 511 |
results["current"] = ""
|
| 512 |
results["indices"] = ""
|
| 513 |
results["hourly12"] = ""
|
|
|
|
| 516 |
html.P(f"Error fetching weather data: {str(e)}", className="text-danger")
|
| 517 |
])
|
| 518 |
], className="mb-4")
|
|
|
|
| 519 |
|
| 520 |
with lock:
|
| 521 |
fetch_weather_data()
|
|
|
|
| 523 |
weather_results = get_data_from_session(session_id, "weather_results")
|
| 524 |
if weather_results:
|
| 525 |
return [
|
| 526 |
+
weather_results.get("weather_alerts", ""),
|
| 527 |
weather_results.get("current", ""),
|
| 528 |
weather_results.get("indices", ""),
|
| 529 |
html.Div([
|