Spaces:
Paused
Paused
Update app.py via AI Editor
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ from datetime import datetime
|
|
7 |
import os
|
8 |
from dotenv import load_dotenv
|
9 |
import threading
|
10 |
-
import json
|
11 |
import uuid
|
12 |
import flask
|
13 |
import logging
|
@@ -141,6 +140,22 @@ def get_hourly_forecast_1hour(location_key):
|
|
141 |
logger.error(f"Error in get_hourly_forecast_1hour: {e}")
|
142 |
return None
|
143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
def get_indices_1day(location_key, index_id):
|
145 |
if location_key is None:
|
146 |
return None
|
@@ -248,23 +263,30 @@ def create_forecast_5day_card(forecast):
|
|
248 |
])
|
249 |
], className="mb-4")
|
250 |
|
251 |
-
def
|
252 |
-
if not
|
253 |
return dbc.Card([
|
254 |
dbc.CardBody([
|
255 |
-
html.H4("
|
256 |
-
html.P("No
|
257 |
])
|
258 |
], className="mb-4")
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
return dbc.Card([
|
261 |
dbc.CardBody([
|
262 |
-
html.H4("
|
263 |
-
|
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 |
|
@@ -323,8 +345,7 @@ app.layout = dbc.Container([
|
|
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%"}
|
330 |
)
|
@@ -380,7 +401,6 @@ def set_session_cookie(response):
|
|
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 |
],
|
386 |
[Input("location-store", "data")],
|
@@ -393,10 +413,10 @@ def update_weather(location, session_data):
|
|
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": "", "
|
400 |
def fetch_weather_data():
|
401 |
try:
|
402 |
location_key = get_data_from_session(session_id, "location_key")
|
@@ -408,9 +428,8 @@ def update_weather(location, session_data):
|
|
408 |
|
409 |
current = get_current_conditions(location_key)
|
410 |
forecast_5day = get_forecast_5day(location_key)
|
411 |
-
|
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)
|
@@ -420,14 +439,14 @@ def update_weather(location, session_data):
|
|
420 |
|
421 |
results["current"] = create_current_weather_card(current)
|
422 |
results["indices"] = create_environmental_indices_card(indices_dict)
|
423 |
-
results["
|
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["
|
431 |
results["forecast"] = dbc.Card([
|
432 |
dbc.CardBody([
|
433 |
html.P(f"Error fetching weather data: {str(e)}", className="text-danger")
|
@@ -442,11 +461,10 @@ def update_weather(location, session_data):
|
|
442 |
return [
|
443 |
weather_results.get("current", ""),
|
444 |
weather_results.get("indices", ""),
|
445 |
-
weather_results.get("
|
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...")
|
|
|
7 |
import os
|
8 |
from dotenv import load_dotenv
|
9 |
import threading
|
|
|
10 |
import uuid
|
11 |
import flask
|
12 |
import logging
|
|
|
140 |
logger.error(f"Error in get_hourly_forecast_1hour: {e}")
|
141 |
return None
|
142 |
|
143 |
+
def get_hourly_forecast_12hour(location_key):
|
144 |
+
if location_key is None:
|
145 |
+
return None
|
146 |
+
url = f"{BASE_URL}/forecasts/v1/hourly/12hour/{location_key}"
|
147 |
+
params = {
|
148 |
+
"apikey": API_KEY,
|
149 |
+
"metric": "false",
|
150 |
+
}
|
151 |
+
try:
|
152 |
+
response = requests.get(url, params=params)
|
153 |
+
response.raise_for_status()
|
154 |
+
return response.json()
|
155 |
+
except requests.RequestException as e:
|
156 |
+
logger.error(f"Error in get_hourly_forecast_12hour: {e}")
|
157 |
+
return None
|
158 |
+
|
159 |
def get_indices_1day(location_key, index_id):
|
160 |
if location_key is None:
|
161 |
return None
|
|
|
263 |
])
|
264 |
], className="mb-4")
|
265 |
|
266 |
+
def create_hourly_12hour_card(hourly_data):
|
267 |
+
if not hourly_data or not isinstance(hourly_data, list) or len(hourly_data) == 0:
|
268 |
return dbc.Card([
|
269 |
dbc.CardBody([
|
270 |
+
html.H4("12-Hour Hourly Forecast", className="card-title"),
|
271 |
+
html.P("No 12-hour forecast available.")
|
272 |
])
|
273 |
], className="mb-4")
|
274 |
+
times = [datetime.strptime(hr['DateTime'], "%Y-%m-%dT%H:%M:%S%z").strftime("%I%p") for hr in hourly_data]
|
275 |
+
temps = [hr['Temperature']['Value'] for hr in hourly_data]
|
276 |
+
phrases = [hr['IconPhrase'] for hr in hourly_data]
|
277 |
+
fig = go.Figure()
|
278 |
+
fig.add_trace(go.Scatter(x=times, y=temps, mode="lines+markers", name="Temperature", line=dict(color="orange")))
|
279 |
+
fig.update_layout(
|
280 |
+
title="12-Hour Temperature Forecast",
|
281 |
+
xaxis_title="Time",
|
282 |
+
yaxis_title="Temperature (°F)",
|
283 |
+
legend_title="Temperature",
|
284 |
+
height=400
|
285 |
+
)
|
286 |
return dbc.Card([
|
287 |
dbc.CardBody([
|
288 |
+
html.H4("12-Hour Hourly Forecast", className="card-title"),
|
289 |
+
dcc.Graph(figure=fig)
|
|
|
|
|
|
|
|
|
290 |
])
|
291 |
], className="mb-4")
|
292 |
|
|
|
345 |
type="default",
|
346 |
children=[
|
347 |
html.Div(id="current-weather-output"),
|
348 |
+
html.Div(id="environmental-indices-output")
|
|
|
349 |
],
|
350 |
style={"width": "100%"}
|
351 |
)
|
|
|
401 |
[
|
402 |
Output("current-weather-output", "children"),
|
403 |
Output("environmental-indices-output", "children"),
|
|
|
404 |
Output("forecast-output", "children"),
|
405 |
],
|
406 |
[Input("location-store", "data")],
|
|
|
413 |
if not location or 'error' in location:
|
414 |
error_message = location.get('error', 'Waiting for location data...') if location else 'Waiting for location data...'
|
415 |
logger.warning(f"Session {session_id} waiting for location: {error_message}")
|
416 |
+
return [dbc.Spinner(color="primary"), "", ""]
|
417 |
|
418 |
lat, lon = location["latitude"], location["longitude"]
|
419 |
+
results = {"current": "", "indices": "", "hourly12": "", "forecast": ""}
|
420 |
def fetch_weather_data():
|
421 |
try:
|
422 |
location_key = get_data_from_session(session_id, "location_key")
|
|
|
428 |
|
429 |
current = get_current_conditions(location_key)
|
430 |
forecast_5day = get_forecast_5day(location_key)
|
431 |
+
hourly_12 = get_hourly_forecast_12hour(location_key)
|
432 |
|
|
|
433 |
indices_dict = {}
|
434 |
for name, idx in INDEX_IDS.items():
|
435 |
indices_dict[name] = get_indices_1day(location_key, idx)
|
|
|
439 |
|
440 |
results["current"] = create_current_weather_card(current)
|
441 |
results["indices"] = create_environmental_indices_card(indices_dict)
|
442 |
+
results["hourly12"] = create_hourly_12hour_card(hourly_12)
|
443 |
results["forecast"] = create_forecast_5day_card(forecast_5day)
|
444 |
save_session_data(session_id, "weather_results", results)
|
445 |
except Exception as e:
|
446 |
logger.error(f"Session {session_id} error: {str(e)}")
|
447 |
results["current"] = ""
|
448 |
results["indices"] = ""
|
449 |
+
results["hourly12"] = ""
|
450 |
results["forecast"] = dbc.Card([
|
451 |
dbc.CardBody([
|
452 |
html.P(f"Error fetching weather data: {str(e)}", className="text-danger")
|
|
|
461 |
return [
|
462 |
weather_results.get("current", ""),
|
463 |
weather_results.get("indices", ""),
|
464 |
+
html.Div([weather_results.get("hourly12", ""), weather_results.get("forecast", "")])
|
|
|
465 |
]
|
466 |
else:
|
467 |
+
return [dbc.Spinner(color="primary"), "", ""]
|
468 |
|
469 |
if __name__ == '__main__':
|
470 |
print("Starting the Dash application...")
|