Spaces:
Paused
Paused
Update app.py via AI Editor
Browse files
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,
|
55 |
-
"Environmental": 34,
|
56 |
-
"Pollen": 11,
|
57 |
-
"Mosquito": 50,
|
58 |
-
"Pests": 53
|
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
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
html.
|
252 |
-
|
253 |
-
|
254 |
-
|
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("
|
266 |
-
|
|
|
|
|
|
|
|
|
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="
|
298 |
-
html.Div(id="
|
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("
|
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 = {"
|
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 |
-
|
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
|
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["
|
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["
|
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("
|
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...")
|