Spaces:
Paused
Paused
Update app.py via AI Editor
Browse files
app.py
CHANGED
@@ -50,11 +50,11 @@ API_KEY = os.getenv('ACCUWEATHER_API_KEY')
|
|
50 |
BASE_URL = "http://dataservice.accuweather.com"
|
51 |
|
52 |
INDEX_IDS = {
|
53 |
-
"Health":
|
54 |
-
"Environmental":
|
55 |
-
"Pollen":
|
56 |
-
"Mosquito":
|
57 |
-
"
|
58 |
}
|
59 |
|
60 |
def get_location_key(lat, lon):
|
@@ -268,6 +268,42 @@ def create_environmental_api_card(environmental_data):
|
|
268 |
])
|
269 |
], className="mb-4")
|
270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
272 |
server = app.server
|
273 |
|
@@ -287,6 +323,7 @@ app.layout = dbc.Container([
|
|
287 |
type="default",
|
288 |
children=[
|
289 |
html.Div(id="current-weather-output"),
|
|
|
290 |
html.Div(id="environmental-api-output")
|
291 |
],
|
292 |
style={"width": "100%"}
|
@@ -342,6 +379,7 @@ def set_session_cookie(response):
|
|
342 |
@app.callback(
|
343 |
[
|
344 |
Output("current-weather-output", "children"),
|
|
|
345 |
Output("environmental-api-output", "children"),
|
346 |
Output("forecast-output", "children"),
|
347 |
],
|
@@ -355,10 +393,10 @@ def update_weather(location, session_data):
|
|
355 |
if not location or 'error' in location:
|
356 |
error_message = location.get('error', 'Waiting for location data...') if location else 'Waiting for location data...'
|
357 |
logger.warning(f"Session {session_id} waiting for location: {error_message}")
|
358 |
-
return [dbc.Spinner(color="primary"), "", ""]
|
359 |
|
360 |
lat, lon = location["latitude"], location["longitude"]
|
361 |
-
results = {"current": "", "environmental": "", "forecast": ""}
|
362 |
def fetch_weather_data():
|
363 |
try:
|
364 |
location_key = get_data_from_session(session_id, "location_key")
|
@@ -372,16 +410,23 @@ def update_weather(location, session_data):
|
|
372 |
forecast_5day = get_forecast_5day(location_key)
|
373 |
environmental_data = get_indices_1day(location_key, INDEX_IDS["Environmental"])
|
374 |
|
|
|
|
|
|
|
|
|
|
|
375 |
if current is None or forecast_5day is None:
|
376 |
raise ValueError("Failed to fetch weather data")
|
377 |
|
378 |
results["current"] = create_current_weather_card(current)
|
|
|
379 |
results["environmental"] = create_environmental_api_card(environmental_data)
|
380 |
results["forecast"] = create_forecast_5day_card(forecast_5day)
|
381 |
save_session_data(session_id, "weather_results", results)
|
382 |
except Exception as e:
|
383 |
logger.error(f"Session {session_id} error: {str(e)}")
|
384 |
results["current"] = ""
|
|
|
385 |
results["environmental"] = ""
|
386 |
results["forecast"] = dbc.Card([
|
387 |
dbc.CardBody([
|
@@ -396,11 +441,12 @@ def update_weather(location, session_data):
|
|
396 |
if weather_results:
|
397 |
return [
|
398 |
weather_results.get("current", ""),
|
|
|
399 |
weather_results.get("environmental", ""),
|
400 |
weather_results.get("forecast", ""),
|
401 |
]
|
402 |
else:
|
403 |
-
return [dbc.Spinner(color="primary"), "", ""]
|
404 |
|
405 |
if __name__ == '__main__':
|
406 |
print("Starting the Dash application...")
|
|
|
50 |
BASE_URL = "http://dataservice.accuweather.com"
|
51 |
|
52 |
INDEX_IDS = {
|
53 |
+
"Health": 10,
|
54 |
+
"Environmental": 5,
|
55 |
+
"Pollen": 30,
|
56 |
+
"Mosquito": 59,
|
57 |
+
"Pest": 61
|
58 |
}
|
59 |
|
60 |
def get_location_key(lat, lon):
|
|
|
268 |
])
|
269 |
], className="mb-4")
|
270 |
|
271 |
+
def create_environmental_indices_card(indices_dict):
|
272 |
+
items = []
|
273 |
+
index_display_names = {
|
274 |
+
"Health": "Health API",
|
275 |
+
"Environmental": "Environmental API",
|
276 |
+
"Pollen": "Pollen",
|
277 |
+
"Pest": "Pest",
|
278 |
+
"Mosquito": "Mosquito"
|
279 |
+
}
|
280 |
+
for key in ["Health", "Environmental", "Pollen", "Pest", "Mosquito"]:
|
281 |
+
data = indices_dict.get(key)
|
282 |
+
display_name = index_display_names[key]
|
283 |
+
if data and isinstance(data, list) and len(data) > 0:
|
284 |
+
d = data[0]
|
285 |
+
items.append(
|
286 |
+
html.Div([
|
287 |
+
html.H6(display_name, style={"marginBottom": "0.25rem", "fontWeight": "bold"}),
|
288 |
+
html.P(f"Category: {d.get('Category', 'N/A')}", style={"marginBottom": "0.25rem"}),
|
289 |
+
html.P(f"Value: {d.get('Value', 'N/A')}", style={"marginBottom": "0.25rem"}),
|
290 |
+
html.P(f"Text: {d.get('Text', 'N/A')}", style={"marginBottom": "0.75rem"})
|
291 |
+
])
|
292 |
+
)
|
293 |
+
else:
|
294 |
+
items.append(
|
295 |
+
html.Div([
|
296 |
+
html.H6(display_name, style={"marginBottom": "0.25rem", "fontWeight": "bold"}),
|
297 |
+
html.P("No data available.", style={"marginBottom": "0.75rem"})
|
298 |
+
])
|
299 |
+
)
|
300 |
+
return dbc.Card([
|
301 |
+
dbc.CardBody([
|
302 |
+
html.H4("Environmental Indices", className="card-title"),
|
303 |
+
*items
|
304 |
+
])
|
305 |
+
], className="mb-4")
|
306 |
+
|
307 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
308 |
server = app.server
|
309 |
|
|
|
323 |
type="default",
|
324 |
children=[
|
325 |
html.Div(id="current-weather-output"),
|
326 |
+
html.Div(id="environmental-indices-output"),
|
327 |
html.Div(id="environmental-api-output")
|
328 |
],
|
329 |
style={"width": "100%"}
|
|
|
379 |
@app.callback(
|
380 |
[
|
381 |
Output("current-weather-output", "children"),
|
382 |
+
Output("environmental-indices-output", "children"),
|
383 |
Output("environmental-api-output", "children"),
|
384 |
Output("forecast-output", "children"),
|
385 |
],
|
|
|
393 |
if not location or 'error' in location:
|
394 |
error_message = location.get('error', 'Waiting for location data...') if location else 'Waiting for location data...'
|
395 |
logger.warning(f"Session {session_id} waiting for location: {error_message}")
|
396 |
+
return [dbc.Spinner(color="primary"), "", "", ""]
|
397 |
|
398 |
lat, lon = location["latitude"], location["longitude"]
|
399 |
+
results = {"current": "", "indices": "", "environmental": "", "forecast": ""}
|
400 |
def fetch_weather_data():
|
401 |
try:
|
402 |
location_key = get_data_from_session(session_id, "location_key")
|
|
|
410 |
forecast_5day = get_forecast_5day(location_key)
|
411 |
environmental_data = get_indices_1day(location_key, INDEX_IDS["Environmental"])
|
412 |
|
413 |
+
# Environmental Indices
|
414 |
+
indices_dict = {}
|
415 |
+
for name, idx in INDEX_IDS.items():
|
416 |
+
indices_dict[name] = get_indices_1day(location_key, idx)
|
417 |
+
|
418 |
if current is None or forecast_5day is None:
|
419 |
raise ValueError("Failed to fetch weather data")
|
420 |
|
421 |
results["current"] = create_current_weather_card(current)
|
422 |
+
results["indices"] = create_environmental_indices_card(indices_dict)
|
423 |
results["environmental"] = create_environmental_api_card(environmental_data)
|
424 |
results["forecast"] = create_forecast_5day_card(forecast_5day)
|
425 |
save_session_data(session_id, "weather_results", results)
|
426 |
except Exception as e:
|
427 |
logger.error(f"Session {session_id} error: {str(e)}")
|
428 |
results["current"] = ""
|
429 |
+
results["indices"] = ""
|
430 |
results["environmental"] = ""
|
431 |
results["forecast"] = dbc.Card([
|
432 |
dbc.CardBody([
|
|
|
441 |
if weather_results:
|
442 |
return [
|
443 |
weather_results.get("current", ""),
|
444 |
+
weather_results.get("indices", ""),
|
445 |
weather_results.get("environmental", ""),
|
446 |
weather_results.get("forecast", ""),
|
447 |
]
|
448 |
else:
|
449 |
+
return [dbc.Spinner(color="primary"), "", "", ""]
|
450 |
|
451 |
if __name__ == '__main__':
|
452 |
print("Starting the Dash application...")
|