Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,43 +5,55 @@ import requests
|
|
| 5 |
import os
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={api_key}"
|
| 15 |
geo_response = requests.get(geo_url).json()
|
| 16 |
if not geo_response:
|
| 17 |
return None
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Initialize Dash app
|
| 47 |
app = dash.Dash(__name__)
|
|
@@ -51,14 +63,14 @@ app.title = "Weather Forecast Dashboard"
|
|
| 51 |
API_KEY = os.getenv("OPENWEATHERMAP_API_KEY", "53d50455f91b6bc3c920959e2954576d")
|
| 52 |
|
| 53 |
# Fixed locations
|
| 54 |
-
LOCATIONS = ["Hyderabad", "
|
| 55 |
DAYS = 5 # Fixed 5-day forecast
|
| 56 |
|
| 57 |
# Layout
|
| 58 |
app.layout = html.Div([
|
| 59 |
html.Div([
|
| 60 |
html.H1("Weather Forecast Dashboard", style={'textAlign': 'center', 'color': '#FFFFFF'}),
|
| 61 |
-
html.P("Current weather for Hyderabad, Gadwal, and
|
| 62 |
style={'textAlign': 'center', 'color': '#D3D9D4'})
|
| 63 |
], style={'backgroundColor': '#212A31', 'padding': '20px'}),
|
| 64 |
|
|
|
|
| 5 |
import os
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
+
# Inline weather_model.py with fixes
|
| 9 |
+
def get_weather_data(city, days, api_key):
|
| 10 |
+
try:
|
| 11 |
+
# Fallback to coordinates if city name fails
|
| 12 |
+
city_coords = {
|
| 13 |
+
"Hyderabad": {"lat": 17.3850, "lon": 78.4867},
|
| 14 |
+
"Gadwal": {"lat": 16.2350, "lon": 77.8050},
|
| 15 |
+
"Bengaluru": {"lat": 12.9716, "lon": 77.5946}
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
coords = city_coords.get(city)
|
| 19 |
+
if not coords:
|
| 20 |
geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={api_key}"
|
| 21 |
geo_response = requests.get(geo_url).json()
|
| 22 |
if not geo_response:
|
| 23 |
return None
|
| 24 |
+
coords = {"lat": geo_response[0]['lat'], "lon": geo_response[0]['lon']}
|
| 25 |
+
|
| 26 |
+
url = f"http://api.openweathermap.org/data/2.5/onecall?lat={coords['lat']}&lon={coords['lon']}&exclude=minutely,hourly&units=metric&appid={api_key}"
|
| 27 |
+
response = requests.get(url, timeout=10).json()
|
| 28 |
+
|
| 29 |
+
current = {
|
| 30 |
+
'temp': response['current']['temp'],
|
| 31 |
+
'weather': response['current']['weather'][0]['description'],
|
| 32 |
+
'humidity': response['current']['humidity'],
|
| 33 |
+
'wind_speed': response['current']['wind_speed']
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
forecast = []
|
| 37 |
+
for i in range(min(days, len(response['daily']))):
|
| 38 |
+
day = response['daily'][i]
|
| 39 |
+
forecast.append({
|
| 40 |
+
'date': datetime.fromtimestamp(day['dt']).strftime('%Y-%m-%d'),
|
| 41 |
+
'temp': day['temp']['day'],
|
| 42 |
+
'precipitation': day.get('rain', 0) + day.get('snow', 0),
|
| 43 |
+
'wind_speed': day['wind_speed'],
|
| 44 |
+
'weather': day['weather'][0]['main']
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
return {'current': current, 'forecast': forecast}
|
| 48 |
+
except requests.exceptions.RequestException as e:
|
| 49 |
+
print(f"Network error for {city}: {e}")
|
| 50 |
+
return None
|
| 51 |
+
except KeyError as e:
|
| 52 |
+
print(f"API response error for {city}: {e}")
|
| 53 |
+
return None
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Unexpected error for {city}: {e}")
|
| 56 |
+
return None
|
| 57 |
|
| 58 |
# Initialize Dash app
|
| 59 |
app = dash.Dash(__name__)
|
|
|
|
| 63 |
API_KEY = os.getenv("OPENWEATHERMAP_API_KEY", "53d50455f91b6bc3c920959e2954576d")
|
| 64 |
|
| 65 |
# Fixed locations
|
| 66 |
+
LOCATIONS = ["Hyderabad", "Gadwal", "Bengaluru"]
|
| 67 |
DAYS = 5 # Fixed 5-day forecast
|
| 68 |
|
| 69 |
# Layout
|
| 70 |
app.layout = html.Div([
|
| 71 |
html.Div([
|
| 72 |
html.H1("Weather Forecast Dashboard", style={'textAlign': 'center', 'color': '#FFFFFF'}),
|
| 73 |
+
html.P("Current weather for Hyderabad, Gadwal, and Bengaluru (Karnataka).",
|
| 74 |
style={'textAlign': 'center', 'color': '#D3D9D4'})
|
| 75 |
], style={'backgroundColor': '#212A31', 'padding': '20px'}),
|
| 76 |
|