bluenevus commited on
Commit
1edd3b2
·
1 Parent(s): 8c2a2be

Update app.py via AI Editor

Browse files
Files changed (1) hide show
  1. app.py +38 -60
app.py CHANGED
@@ -49,13 +49,12 @@ def get_data_from_session(session_id, key):
49
  API_KEY = os.getenv('ACCUWEATHER_API_KEY')
50
  BASE_URL = "http://dataservice.accuweather.com"
51
 
52
- # Index IDs from AccuWeather's documentation (example IDs, replace with actual as needed)
53
  INDEX_IDS = {
54
- "Health": 31, # e.g., Flu Index
55
- "Environmental": 34, # e.g., Air Quality Index
56
- "Pollen": 11, # e.g., Tree Pollen
57
- "Mosquito": 50, # e.g., Mosquito Index
58
- "Pests": 53 # e.g., Pest Activity
59
  }
60
 
61
  def get_location_key(lat, lon):
@@ -150,13 +149,19 @@ def get_indices_1day(location_key, index_id):
150
  try:
151
  response = requests.get(url, params=params)
152
  response.raise_for_status()
153
- # returns a list with one dict per day (only 1 for 1day endpoint)
154
  return response.json()
155
  except requests.RequestException as e:
156
  logger.error(f"Error in get_indices_1day {index_id}: {e}")
157
  return None
158
 
159
  def create_current_weather_card(current):
 
 
 
 
 
 
 
160
  return dbc.Card([
161
  dbc.CardBody([
162
  html.H4("Current Weather", className="card-title"),
@@ -243,27 +248,23 @@ def create_forecast_5day_card(forecast):
243
  ])
244
  ], className="mb-4")
245
 
246
- def create_indices_card(indices_dict):
247
- cards = []
248
- for idx_name, idx_data in indices_dict.items():
249
- if not idx_data or not isinstance(idx_data, list) or 'Category' not in idx_data[0]:
250
- cards.append(html.Div([
251
- html.H5(f"{idx_name} Index"),
252
- html.P("No data available.")
253
- ], style={"marginBottom": "10px"}))
254
- else:
255
- info = idx_data[0]
256
- cards.append(html.Div([
257
- html.H5(f"{idx_name} Index"),
258
- html.P(f"{info.get('Name', '')}: {info.get('Category', '')}"),
259
- html.P(f"Value: {info.get('Value', 'N/A')}"),
260
- html.P(f"Category Value: {info.get('CategoryValue', 'N/A')}"),
261
- html.P(f"Text: {info.get('Text', '')}")
262
- ], style={"marginBottom": "10px"}))
263
  return dbc.Card([
264
  dbc.CardBody([
265
- html.H4("Today's Health & Environmental Indices", className="card-title"),
266
- *cards
 
 
 
 
267
  ])
268
  ], className="mb-4")
269
 
@@ -281,21 +282,12 @@ app.layout = dbc.Container([
281
  ]),
282
  dbc.Row([
283
  dbc.Col([
284
- dbc.Card([
285
- dbc.CardBody([
286
- html.H5("Navigation", className="card-title"),
287
- dbc.Nav([
288
- dbc.NavLink("Health & Env. Indices", href="#", id="nav-health"),
289
- dbc.NavLink("Current Weather", href="#", id="nav-current"),
290
- ], vertical=True, pills=True)
291
- ])
292
- ], className="mb-4"),
293
  dcc.Loading(
294
  id="loading",
295
  type="default",
296
  children=[
297
- html.Div(id="indices-output"),
298
- html.Div(id="current-weather-output")
299
  ],
300
  style={"width": "100%"}
301
  )
@@ -305,8 +297,6 @@ app.layout = dbc.Container([
305
  id="loading-forecast",
306
  type="default",
307
  children=[
308
- html.Div(id="hourly-forecast-output"),
309
- html.Div(id="forecast-1day-output"),
310
  html.Div(id="forecast-output")
311
  ],
312
  style={"width": "100%"}
@@ -351,10 +341,8 @@ def set_session_cookie(response):
351
 
352
  @app.callback(
353
  [
354
- Output("indices-output", "children"),
355
  Output("current-weather-output", "children"),
356
- Output("hourly-forecast-output", "children"),
357
- Output("forecast-1day-output", "children"),
358
  Output("forecast-output", "children"),
359
  ],
360
  [Input("location-store", "data")],
@@ -367,10 +355,10 @@ def update_weather(location, session_data):
367
  if not location or 'error' in location:
368
  error_message = location.get('error', 'Waiting for location data...') if location else 'Waiting for location data...'
369
  logger.warning(f"Session {session_id} waiting for location: {error_message}")
370
- return [dbc.Spinner(color="primary"), "", "", "", ""]
371
 
372
  lat, lon = location["latitude"], location["longitude"]
373
- results = {"indices": "", "current": "", "hourly": "", "forecast_1day": "", "forecast": ""}
374
  def fetch_weather_data():
375
  try:
376
  location_key = get_data_from_session(session_id, "location_key")
@@ -382,27 +370,19 @@ def update_weather(location, session_data):
382
 
383
  current = get_current_conditions(location_key)
384
  forecast_5day = get_forecast_5day(location_key)
385
- forecast_1day = get_forecast_1day(location_key)
386
- hourly_1hour = get_hourly_forecast_1hour(location_key)
387
- indices_dict = {}
388
- for idx_name, idx_id in INDEX_IDS.items():
389
- indices_dict[idx_name] = get_indices_1day(location_key, idx_id)
390
 
391
- if current is None or forecast_5day is None or forecast_1day is None:
392
  raise ValueError("Failed to fetch weather data")
393
 
394
- results["indices"] = create_indices_card(indices_dict)
395
  results["current"] = create_current_weather_card(current)
396
- results["hourly"] = create_hourly_1hour_card(hourly_1hour)
397
- results["forecast_1day"] = create_forecast_1day_card(forecast_1day)
398
  results["forecast"] = create_forecast_5day_card(forecast_5day)
399
  save_session_data(session_id, "weather_results", results)
400
  except Exception as e:
401
  logger.error(f"Session {session_id} error: {str(e)}")
402
- results["indices"] = ""
403
  results["current"] = ""
404
- results["hourly"] = ""
405
- results["forecast_1day"] = ""
406
  results["forecast"] = dbc.Card([
407
  dbc.CardBody([
408
  html.P(f"Error fetching weather data: {str(e)}", className="text-danger")
@@ -415,14 +395,12 @@ def update_weather(location, session_data):
415
  weather_results = get_data_from_session(session_id, "weather_results")
416
  if weather_results:
417
  return [
418
- weather_results.get("indices", ""),
419
  weather_results.get("current", ""),
420
- weather_results.get("hourly", ""),
421
- weather_results.get("forecast_1day", ""),
422
  weather_results.get("forecast", ""),
423
  ]
424
  else:
425
- return [dbc.Spinner(color="primary"), "", "", "", ""]
426
 
427
  if __name__ == '__main__':
428
  print("Starting the Dash application...")
 
49
  API_KEY = os.getenv('ACCUWEATHER_API_KEY')
50
  BASE_URL = "http://dataservice.accuweather.com"
51
 
 
52
  INDEX_IDS = {
53
+ "Health": 31,
54
+ "Environmental": 34,
55
+ "Pollen": 11,
56
+ "Mosquito": 50,
57
+ "Pests": 53
58
  }
59
 
60
  def get_location_key(lat, lon):
 
149
  try:
150
  response = requests.get(url, params=params)
151
  response.raise_for_status()
 
152
  return response.json()
153
  except requests.RequestException as e:
154
  logger.error(f"Error in get_indices_1day {index_id}: {e}")
155
  return None
156
 
157
  def create_current_weather_card(current):
158
+ if not current:
159
+ return dbc.Card([
160
+ dbc.CardBody([
161
+ html.H4("Current Weather", className="card-title"),
162
+ html.P("No weather data available.")
163
+ ])
164
+ ], className="mb-4")
165
  return dbc.Card([
166
  dbc.CardBody([
167
  html.H4("Current Weather", className="card-title"),
 
248
  ])
249
  ], className="mb-4")
250
 
251
+ def create_environmental_api_card(environmental_data):
252
+ if not environmental_data or not isinstance(environmental_data, list) or not environmental_data[0]:
253
+ return dbc.Card([
254
+ dbc.CardBody([
255
+ html.H4("Environmental API", className="card-title"),
256
+ html.P("No environmental data available.")
257
+ ])
258
+ ], className="mb-4")
259
+ info = environmental_data[0]
 
 
 
 
 
 
 
 
260
  return dbc.Card([
261
  dbc.CardBody([
262
+ html.H4("Environmental API", className="card-title"),
263
+ html.P(f"Name: {info.get('Name', 'N/A')}"),
264
+ html.P(f"Category: {info.get('Category', 'N/A')}"),
265
+ html.P(f"Value: {info.get('Value', 'N/A')}"),
266
+ html.P(f"Category Value: {info.get('CategoryValue', 'N/A')}"),
267
+ html.P(f"Text: {info.get('Text', 'N/A')}")
268
  ])
269
  ], className="mb-4")
270
 
 
282
  ]),
283
  dbc.Row([
284
  dbc.Col([
 
 
 
 
 
 
 
 
 
285
  dcc.Loading(
286
  id="loading",
287
  type="default",
288
  children=[
289
+ html.Div(id="current-weather-output"),
290
+ html.Div(id="environmental-api-output")
291
  ],
292
  style={"width": "100%"}
293
  )
 
297
  id="loading-forecast",
298
  type="default",
299
  children=[
 
 
300
  html.Div(id="forecast-output")
301
  ],
302
  style={"width": "100%"}
 
341
 
342
  @app.callback(
343
  [
 
344
  Output("current-weather-output", "children"),
345
+ Output("environmental-api-output", "children"),
 
346
  Output("forecast-output", "children"),
347
  ],
348
  [Input("location-store", "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")
 
370
 
371
  current = get_current_conditions(location_key)
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([
388
  html.P(f"Error fetching weather data: {str(e)}", className="text-danger")
 
395
  weather_results = get_data_from_session(session_id, "weather_results")
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...")