Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import dash
|
2 |
+
from dash import dcc, html, Input, Output
|
3 |
+
import plotly.graph_objs as go
|
4 |
+
import requests
|
5 |
+
from datetime import datetime, timedelta
|
6 |
+
from weather_model import get_weather_data
|
7 |
+
|
8 |
+
# Initialize Dash app
|
9 |
+
app = dash.Dash(__name__)
|
10 |
+
app.title = "Weather Forecast Dashboard"
|
11 |
+
|
12 |
+
# API Key for OpenWeatherMap (replace with your own)
|
13 |
+
API_KEY = "your_openweathermap_api_key"
|
14 |
+
|
15 |
+
# Layout with advanced UI
|
16 |
+
app.layout = html.Div([
|
17 |
+
html.Div([
|
18 |
+
html.H1("Weather Forecast Dashboard", style={'textAlign': 'center', 'color': '#FFFFFF'}),
|
19 |
+
html.P("Get real-time updates and predictions for your location.",
|
20 |
+
style={'textAlign': 'center', 'color': '#D3D9D4'})
|
21 |
+
], style={'backgroundColor': '#212A31', 'padding': '20px'}),
|
22 |
+
|
23 |
+
html.Div([
|
24 |
+
dcc.Input(id='city-input', type='text', value='New York',
|
25 |
+
style={'width': '50%', 'margin': '10px', 'padding': '5px'}),
|
26 |
+
html.Button('Get Weather', id='submit-btn', n_clicks=0,
|
27 |
+
style={'backgroundColor': '#748D92', 'color': '#212A31', 'padding': '5px 15px'}),
|
28 |
+
dcc.Dropdown(id='forecast-days', options=[
|
29 |
+
{'label': '5 Days', 'value': 5},
|
30 |
+
{'label': '10 Days', 'value': 10}
|
31 |
+
], value=5, style={'width': '50%', 'margin': '10px'})
|
32 |
+
], style={'backgroundColor': '#2E3944', 'padding': '20px', 'borderRadius': '10px'}),
|
33 |
+
|
34 |
+
html.Div(id='weather-summary', style={'color': '#FFFFFF', 'padding': '20px'}),
|
35 |
+
dcc.Graph(id='temperature-graph'),
|
36 |
+
dcc.Graph(id='precipitation-graph'),
|
37 |
+
dcc.Graph(id='wind-graph'),
|
38 |
+
], style={'backgroundColor': '#1A2329', 'fontFamily': 'Arial', 'minHeight': '100vh'})
|
39 |
+
|
40 |
+
# Callback to update dashboard
|
41 |
+
@app.callback(
|
42 |
+
[Output('weather-summary', 'children'),
|
43 |
+
Output('temperature-graph', 'figure'),
|
44 |
+
Output('precipitation-graph', 'figure'),
|
45 |
+
Output('wind-graph', 'figure')],
|
46 |
+
[Input('submit-btn', 'n_clicks')],
|
47 |
+
[dash.dependencies.State('city-input', 'value'),
|
48 |
+
dash.dependencies.State('forecast-days', 'value')]
|
49 |
+
)
|
50 |
+
def update_dashboard(n_clicks, city, days):
|
51 |
+
if n_clicks == 0:
|
52 |
+
return "Enter a city and click 'Get Weather'.", {}, {}, {}
|
53 |
+
|
54 |
+
weather_data = get_weather_data(city, days, API_KEY)
|
55 |
+
if not weather_data:
|
56 |
+
return "City not found!", {}, {}, {}
|
57 |
+
|
58 |
+
# Summary
|
59 |
+
current = weather_data['current']
|
60 |
+
summary = html.Div([
|
61 |
+
html.H3(f"Current Weather in {city}"),
|
62 |
+
html.P(f"Temperature: {current['temp']}°C"),
|
63 |
+
html.P(f"Condition: {current['weather']}"),
|
64 |
+
html.P(f"Humidity: {current['humidity']}%"),
|
65 |
+
html.P(f"Wind Speed: {current['wind_speed']} km/h"),
|
66 |
+
])
|
67 |
+
|
68 |
+
# Temperature Graph
|
69 |
+
dates = [d['date'] for d in weather_data['forecast']]
|
70 |
+
temps = [d['temp'] for d in weather_data['forecast']]
|
71 |
+
temp_fig = {
|
72 |
+
'data': [go.Scatter(x=dates, y=temps, mode='lines+markers', name='Temperature')],
|
73 |
+
'layout': go.Layout(title='Temperature Forecast', xaxis={'title': 'Date'},
|
74 |
+
yaxis={'title': 'Temperature (°C)'}, template='plotly_dark')
|
75 |
+
}
|
76 |
+
|
77 |
+
# Precipitation Graph (Rain/Thunderstorm)
|
78 |
+
precip = [d['precipitation'] for d in weather_data['forecast']]
|
79 |
+
precip_fig = {
|
80 |
+
'data': [go.Bar(x=dates, y=precip, name='Precipitation')],
|
81 |
+
'layout': go.Layout(title='Precipitation Forecast', xaxis={'title': 'Date'},
|
82 |
+
yaxis={'title': 'Precipitation (mm)'}, template='plotly_dark')
|
83 |
+
}
|
84 |
+
|
85 |
+
# Wind Speed Graph
|
86 |
+
wind = [d['wind_speed'] for d in weather_data['forecast']]
|
87 |
+
wind_fig = {
|
88 |
+
'data': [go.Scatter(x=dates, y=wind, mode='lines+markers', name='Wind Speed')],
|
89 |
+
'layout': go.Layout(title='Wind Speed Forecast', xaxis={'title': 'Date'},
|
90 |
+
yaxis={'title': 'Wind Speed (km/h)'}, template='plotly_dark')
|
91 |
+
}
|
92 |
+
|
93 |
+
return summary, temp_fig, precip_fig, wind_fig
|
94 |
+
|
95 |
+
if __name__ == '__main__':
|
96 |
+
app.run_server(debug=True)
|