bluenevus commited on
Commit
8c2a2be
·
verified ·
1 Parent(s): 1043c30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -35
app.py CHANGED
@@ -49,12 +49,13 @@ 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 = {
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,19 +150,13 @@ def get_indices_1day(location_key, index_id):
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,23 +243,27 @@ def create_forecast_5day_card(forecast):
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,12 +281,21 @@ app.layout = dbc.Container([
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
  )
@@ -343,8 +351,8 @@ def set_session_cookie(response):
343
 
344
  @app.callback(
345
  [
 
346
  Output("current-weather-output", "children"),
347
- Output("environmental-api-output", "children"),
348
  Output("hourly-forecast-output", "children"),
349
  Output("forecast-1day-output", "children"),
350
  Output("forecast-output", "children"),
@@ -362,7 +370,7 @@ def update_weather(location, session_data):
362
  return [dbc.Spinner(color="primary"), "", "", "", ""]
363
 
364
  lat, lon = location["latitude"], location["longitude"]
365
- results = {"current": "", "environmental": "", "hourly": "", "forecast_1day": "", "forecast": ""}
366
  def fetch_weather_data():
367
  try:
368
  location_key = get_data_from_session(session_id, "location_key")
@@ -376,21 +384,23 @@ def update_weather(location, session_data):
376
  forecast_5day = get_forecast_5day(location_key)
377
  forecast_1day = get_forecast_1day(location_key)
378
  hourly_1hour = get_hourly_forecast_1hour(location_key)
379
- environmental_data = get_indices_1day(location_key, INDEX_IDS["Environmental"])
 
 
380
 
381
  if current is None or forecast_5day is None or forecast_1day is None:
382
  raise ValueError("Failed to fetch weather data")
383
 
 
384
  results["current"] = create_current_weather_card(current)
385
- results["environmental"] = create_environmental_api_card(environmental_data)
386
  results["hourly"] = create_hourly_1hour_card(hourly_1hour)
387
  results["forecast_1day"] = create_forecast_1day_card(forecast_1day)
388
  results["forecast"] = create_forecast_5day_card(forecast_5day)
389
  save_session_data(session_id, "weather_results", results)
390
  except Exception as e:
391
  logger.error(f"Session {session_id} error: {str(e)}")
 
392
  results["current"] = ""
393
- results["environmental"] = ""
394
  results["hourly"] = ""
395
  results["forecast_1day"] = ""
396
  results["forecast"] = dbc.Card([
@@ -405,8 +415,8 @@ def update_weather(location, session_data):
405
  weather_results = get_data_from_session(session_id, "weather_results")
406
  if weather_results:
407
  return [
 
408
  weather_results.get("current", ""),
409
- weather_results.get("environmental", ""),
410
  weather_results.get("hourly", ""),
411
  weather_results.get("forecast_1day", ""),
412
  weather_results.get("forecast", ""),
 
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
  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
  ])
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
  ]),
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
  )
 
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"),
 
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")
 
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([
 
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", ""),