-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
52 lines (46 loc) · 1.83 KB
/
app.py
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
from flask import Flask, render_template
from src.data_processing.data_processor import DataProcessor
from src.database.db_handler import DBHandler
from src.utils.config_loader import load_config
from src.ml.weather_predictor import WeatherPredictor
from datetime import datetime, timedelta
import os
app = Flask(__name__)
config = load_config()
db_handler = DBHandler(config['database'])
data_processor = DataProcessor()
@app.route('/')
def dashboard():
cities = db_handler.get_cities()
latest_data = {}
predictions = {}
for city in cities:
city_data = db_handler.get_recent_weather_data(city, limit=1)
if city_data:
latest_data[city] = city_data[0]
predictor = WeatherPredictor(city)
try:
predictor.load_model()
except FileNotFoundError:
historical_data = db_handler.get_historical_weather_data(city)
if historical_data:
predictor.train_model(historical_data)
else:
continue # Skip prediction if no historical data
next_day = datetime.now() + timedelta(days=1)
try:
prediction = predictor.predict(
next_day.hour,
next_day.weekday(),
next_day.month,
latest_data[city]['humidity'],
latest_data[city]['wind_speed'],
latest_data[city]['weather_condition']
)
predictions[city] = round(prediction, 1)
except ValueError as e:
print(f"Prediction error for {city}: {str(e)}")
predictions[city] = "N/A"
return render_template('dashboard.html', latest_data=latest_data, predictions=predictions)
if __name__ == '__main__':
app.run(debug=True)