File size: 3,916 Bytes
77b0eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import dash
from dash import dcc, html, Input, Output
import plotly.graph_objs as go
import requests
from datetime import datetime, timedelta
from weather_model import get_weather_data

# Initialize Dash app
app = dash.Dash(__name__)
app.title = "Weather Forecast Dashboard"

# API Key for OpenWeatherMap (replace with your own)
API_KEY = "your_openweathermap_api_key"

# Layout with advanced UI
app.layout = html.Div([
    html.Div([
        html.H1("Weather Forecast Dashboard", style={'textAlign': 'center', 'color': '#FFFFFF'}),
        html.P("Get real-time updates and predictions for your location.", 
               style={'textAlign': 'center', 'color': '#D3D9D4'})
    ], style={'backgroundColor': '#212A31', 'padding': '20px'}),

    html.Div([
        dcc.Input(id='city-input', type='text', value='New York', 
                  style={'width': '50%', 'margin': '10px', 'padding': '5px'}),
        html.Button('Get Weather', id='submit-btn', n_clicks=0, 
                    style={'backgroundColor': '#748D92', 'color': '#212A31', 'padding': '5px 15px'}),
        dcc.Dropdown(id='forecast-days', options=[
            {'label': '5 Days', 'value': 5},
            {'label': '10 Days', 'value': 10}
        ], value=5, style={'width': '50%', 'margin': '10px'})
    ], style={'backgroundColor': '#2E3944', 'padding': '20px', 'borderRadius': '10px'}),

    html.Div(id='weather-summary', style={'color': '#FFFFFF', 'padding': '20px'}),
    dcc.Graph(id='temperature-graph'),
    dcc.Graph(id='precipitation-graph'),
    dcc.Graph(id='wind-graph'),
], style={'backgroundColor': '#1A2329', 'fontFamily': 'Arial', 'minHeight': '100vh'})

# Callback to update dashboard
@app.callback(
    [Output('weather-summary', 'children'),
     Output('temperature-graph', 'figure'),
     Output('precipitation-graph', 'figure'),
     Output('wind-graph', 'figure')],
    [Input('submit-btn', 'n_clicks')],
    [dash.dependencies.State('city-input', 'value'),
     dash.dependencies.State('forecast-days', 'value')]
)
def update_dashboard(n_clicks, city, days):
    if n_clicks == 0:
        return "Enter a city and click 'Get Weather'.", {}, {}, {}

    weather_data = get_weather_data(city, days, API_KEY)
    if not weather_data:
        return "City not found!", {}, {}, {}

    # Summary
    current = weather_data['current']
    summary = html.Div([
        html.H3(f"Current Weather in {city}"),
        html.P(f"Temperature: {current['temp']}°C"),
        html.P(f"Condition: {current['weather']}"),
        html.P(f"Humidity: {current['humidity']}%"),
        html.P(f"Wind Speed: {current['wind_speed']} km/h"),
    ])

    # Temperature Graph
    dates = [d['date'] for d in weather_data['forecast']]
    temps = [d['temp'] for d in weather_data['forecast']]
    temp_fig = {
        'data': [go.Scatter(x=dates, y=temps, mode='lines+markers', name='Temperature')],
        'layout': go.Layout(title='Temperature Forecast', xaxis={'title': 'Date'}, 
                           yaxis={'title': 'Temperature (°C)'}, template='plotly_dark')
    }

    # Precipitation Graph (Rain/Thunderstorm)
    precip = [d['precipitation'] for d in weather_data['forecast']]
    precip_fig = {
        'data': [go.Bar(x=dates, y=precip, name='Precipitation')],
        'layout': go.Layout(title='Precipitation Forecast', xaxis={'title': 'Date'}, 
                           yaxis={'title': 'Precipitation (mm)'}, template='plotly_dark')
    }

    # Wind Speed Graph
    wind = [d['wind_speed'] for d in weather_data['forecast']]
    wind_fig = {
        'data': [go.Scatter(x=dates, y=wind, mode='lines+markers', name='Wind Speed')],
        'layout': go.Layout(title='Wind Speed Forecast', xaxis={'title': 'Date'}, 
                           yaxis={'title': 'Wind Speed (km/h)'}, template='plotly_dark')
    }

    return summary, temp_fig, precip_fig, wind_fig

if __name__ == '__main__':
    app.run_server(debug=True)