bluenevus commited on
Commit
a1eee9d
·
verified ·
1 Parent(s): bd37c51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -57
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import dash
2
- from dash import dcc, html, Input, Output, State
3
  import dash_bootstrap_components as dbc
4
  import plotly.graph_objs as go
5
  import requests
@@ -8,6 +8,7 @@ from datetime import datetime
8
  import os
9
  from dotenv import load_dotenv
10
  import threading
 
11
 
12
  # Load environment variables
13
  load_dotenv()
@@ -22,10 +23,41 @@ API_KEY = os.getenv('ACCUWEATHER_API_KEY')
22
  # Base URL for AccuWeather API
23
  BASE_URL = "http://dataservice.accuweather.com"
24
 
25
- # API functions remain the same
26
- # ...
27
-
28
- # Updated App layout
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  app.layout = dbc.Container([
30
  dbc.Row([
31
  dbc.Col([
@@ -47,12 +79,32 @@ app.layout = dbc.Container([
47
  dcc.Interval(id="location-interval", interval=1000, max_intervals=1),
48
  ], fluid=True, className="mt-4")
49
 
50
- @app.callback(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  Output("location-store", "data"),
52
- Input("location-interval", "n_intervals")
53
  )
54
- def update_location(n):
55
- return {"latitude": 0, "longitude": 0} # Replace with actual browser location data
56
 
57
  @app.callback(
58
  [Output("loading-output", "children"),
@@ -63,57 +115,14 @@ def update_location(n):
63
  Input("location-store", "data")
64
  )
65
  def update_weather(location):
66
- if not location:
67
- return "Waiting for location data...", "", "", "", ""
 
68
 
69
  lat, lon = location["latitude"], location["longitude"]
70
 
71
- loading_output = html.Div([
72
- html.P("Loading weather data...", className="text-center"),
73
- dbc.Spinner(color="primary", type="grow"),
74
- ])
75
-
76
- def fetch_weather_data():
77
- nonlocal loading_output
78
- try:
79
- location_key = get_location_key(lat, lon)
80
- if location_key is None:
81
- raise ValueError("Failed to get location key")
82
-
83
- current = get_current_conditions(location_key)
84
- forecast = get_forecast(location_key)
85
- indices = get_indices(location_key)
86
- alerts = get_alerts(location_key)
87
-
88
- if current is None or forecast is None or indices is None:
89
- raise ValueError("Failed to fetch weather data")
90
-
91
- current_weather = create_current_weather_card(current)
92
- forecast_output = create_forecast_card(forecast)
93
- indices_output = create_indices_card(indices)
94
- alert_output = create_alert_card(alerts)
95
-
96
- loading_output = ""
97
-
98
- return current_weather, forecast_output, indices_output, alert_output
99
-
100
- except Exception as e:
101
- error_message = f"Error fetching weather data: {str(e)}"
102
- print(error_message) # Log the error
103
- loading_output = html.Div(error_message, className="text-danger")
104
- return "", "", "", ""
105
-
106
- # Use threading to fetch data asynchronously
107
- thread = threading.Thread(target=fetch_weather_data)
108
- thread.start()
109
- thread.join(timeout=10) # Wait for up to 10 seconds
110
-
111
- if thread.is_alive():
112
- loading_output = html.Div("Weather data fetch timed out. Please try again.", className="text-warning")
113
- return loading_output, "", "", "", ""
114
-
115
- current_weather, forecast_output, indices_output, alert_output = fetch_weather_data()
116
- return loading_output, current_weather, forecast_output, indices_output, alert_output
117
 
118
  # The rest of the code (create_current_weather_card, create_forecast_card, etc.) remains the same
119
 
 
1
  import dash
2
+ from dash import dcc, html, Input, Output, State, clientside_callback
3
  import dash_bootstrap_components as dbc
4
  import plotly.graph_objs as go
5
  import requests
 
8
  import os
9
  from dotenv import load_dotenv
10
  import threading
11
+ import json
12
 
13
  # Load environment variables
14
  load_dotenv()
 
23
  # Base URL for AccuWeather API
24
  BASE_URL = "http://dataservice.accuweather.com"
25
 
26
+ # Define all the necessary functions
27
+ def get_location_key(lat, lon):
28
+ url = f"{BASE_URL}/locations/v1/cities/geoposition/search"
29
+ params = {
30
+ "apikey": API_KEY,
31
+ "q": f"{lat},{lon}",
32
+ }
33
+ try:
34
+ response = requests.get(url, params=params)
35
+ response.raise_for_status()
36
+ data = response.json()
37
+ if "Key" not in data:
38
+ raise ValueError("Location key not found in API response")
39
+ return data["Key"]
40
+ except requests.RequestException as e:
41
+ print(f"Error in get_location_key: {e}")
42
+ return None
43
+
44
+ def get_current_conditions(location_key):
45
+ # Implementation as before
46
+ ...
47
+
48
+ def get_forecast(location_key):
49
+ # Implementation as before
50
+ ...
51
+
52
+ def get_indices(location_key):
53
+ # Implementation as before
54
+ ...
55
+
56
+ def get_alerts(location_key):
57
+ # Implementation as before
58
+ ...
59
+
60
+ # App layout
61
  app.layout = dbc.Container([
62
  dbc.Row([
63
  dbc.Col([
 
79
  dcc.Interval(id="location-interval", interval=1000, max_intervals=1),
80
  ], fluid=True, className="mt-4")
81
 
82
+ # Clientside callback to get user's location
83
+ clientside_callback(
84
+ """
85
+ function(n_intervals) {
86
+ return new Promise((resolve, reject) => {
87
+ if (navigator.geolocation) {
88
+ navigator.geolocation.getCurrentPosition(
89
+ (position) => {
90
+ resolve({
91
+ latitude: position.coords.latitude,
92
+ longitude: position.coords.longitude
93
+ });
94
+ },
95
+ (error) => {
96
+ resolve({error: error.message});
97
+ }
98
+ );
99
+ } else {
100
+ resolve({error: "Geolocation is not supported by this browser."});
101
+ }
102
+ });
103
+ }
104
+ """,
105
  Output("location-store", "data"),
106
+ Input("location-interval", "n_intervals"),
107
  )
 
 
108
 
109
  @app.callback(
110
  [Output("loading-output", "children"),
 
115
  Input("location-store", "data")
116
  )
117
  def update_weather(location):
118
+ if not location or 'error' in location:
119
+ error_message = location.get('error', 'Waiting for location data...') if location else 'Waiting for location data...'
120
+ return error_message, "", "", "", ""
121
 
122
  lat, lon = location["latitude"], location["longitude"]
123
 
124
+ # Rest of the function remains the same
125
+ ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  # The rest of the code (create_current_weather_card, create_forecast_card, etc.) remains the same
128