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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -85
app.py CHANGED
@@ -22,92 +22,30 @@ API_KEY = os.getenv('ACCUWEATHER_API_KEY')
22
  # Base URL for AccuWeather API
23
  BASE_URL = "http://dataservice.accuweather.com"
24
 
25
- def get_location_key(lat, lon):
26
- url = f"{BASE_URL}/locations/v1/cities/geoposition/search"
27
- params = {
28
- "apikey": API_KEY,
29
- "q": f"{lat},{lon}",
30
- }
31
- try:
32
- response = requests.get(url, params=params)
33
- response.raise_for_status() # Raise an exception for bad status codes
34
- data = response.json()
35
- if "Key" not in data:
36
- raise ValueError("Location key not found in API response")
37
- return data["Key"]
38
- except requests.RequestException as e:
39
- print(f"Error in get_location_key: {e}")
40
- return None
41
-
42
- def get_current_conditions(location_key):
43
- if location_key is None:
44
- return None
45
- url = f"{BASE_URL}/currentconditions/v1/{location_key}"
46
- params = {
47
- "apikey": API_KEY,
48
- "details": "true",
49
- }
50
- try:
51
- response = requests.get(url, params=params)
52
- response.raise_for_status()
53
- data = response.json()
54
- if not data:
55
- raise ValueError("No current conditions data in API response")
56
- return data[0]
57
- except requests.RequestException as e:
58
- print(f"Error in get_current_conditions: {e}")
59
- return None
60
-
61
- def get_forecast(location_key):
62
- if location_key is None:
63
- return None
64
- url = f"{BASE_URL}/forecasts/v1/daily/5day/{location_key}"
65
- params = {
66
- "apikey": API_KEY,
67
- "metric": "true",
68
- }
69
- try:
70
- response = requests.get(url, params=params)
71
- response.raise_for_status()
72
- return response.json()
73
- except requests.RequestException as e:
74
- print(f"Error in get_forecast: {e}")
75
- return None
76
-
77
- def get_indices(location_key):
78
- if location_key is None:
79
- return None
80
- url = f"{BASE_URL}/indices/v1/daily/5day/{location_key}"
81
- params = {
82
- "apikey": API_KEY,
83
- }
84
- try:
85
- response = requests.get(url, params=params)
86
- response.raise_for_status()
87
- return response.json()
88
- except requests.RequestException as e:
89
- print(f"Error in get_indices: {e}")
90
- return None
91
-
92
- def get_alerts(location_key):
93
- if location_key is None:
94
- return None
95
- url = f"{BASE_URL}/alerts/v1/{location_key}"
96
- params = {
97
- "apikey": API_KEY,
98
- }
99
- try:
100
- response = requests.get(url, params=params)
101
- response.raise_for_status()
102
- return response.json()
103
- except requests.RequestException as e:
104
- print(f"Error in get_alerts: {e}")
105
- return None
106
-
107
- # App layout remains the same
108
  app.layout = dbc.Container([
109
- # ... (previous layout code)
110
- ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  @app.callback(
113
  Output("location-store", "data"),
 
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([
32
+ html.H1("Weather Dashboard", className="text-center mb-4"),
33
+ html.Div(id="loading-output"),
34
+ html.Div(id="alert-output"),
35
+ dbc.Row([
36
+ dbc.Col([
37
+ html.Div(id="current-weather-output"),
38
+ ], width=6),
39
+ dbc.Col([
40
+ html.Div(id="forecast-output"),
41
+ ], width=6),
42
+ ]),
43
+ html.Div(id="indices-output"),
44
+ ], width=12)
45
+ ]),
46
+ dcc.Store(id="location-store"),
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"),