-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
78 lines (59 loc) · 2.51 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
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
from flask import Flask, render_template, request
import json
from api_handler.dataclasses import ForecastData
from get_datapoint_handler import get_datapoint_handler
from postcode_resolver import PostcodeResolver
datapoint_handler = get_datapoint_handler()
postcode_resolver = PostcodeResolver()
datapoint_webapp = Flask(__name__)
@datapoint_webapp.route("/")
def index():
max_number_of_forecast_locations = len(datapoint_handler.get_forecast_locations())
return render_template(
"forecast_ui.html",
max_number_of_forecast_locations=max_number_of_forecast_locations,
)
@datapoint_webapp.route("/get_number_of_forecast_locations", methods=["GET"])
def get_number_of_forecast_locations():
return json.dumps(len(datapoint_handler.get_forecast_locations()))
@datapoint_webapp.route("/get_forecast_locations", methods=["POST"])
def get_forecast_locations():
return json.dumps(datapoint_handler.get_forecast_locations())
@datapoint_webapp.route("/get_limited_forecast_locations", methods=["POST"])
def get_limited_forecast_locations():
number_of_points_to_get = int(request.json["num_points"])
return_points = datapoint_handler.get_forecast_locations()
del return_points[number_of_points_to_get:]
return json.dumps(return_points)
@datapoint_webapp.route("/get_historic_locations", methods=["POST"])
def get_historic_locations():
return json.dumps(datapoint_handler.get_historic_locations())
@datapoint_webapp.route("/get_forecast_for_location_id", methods=["POST"])
def get_forecast_for_location_id():
location_data = ForecastData(
datapoint_handler.get_forecast_for_location_id(request.json["location_id"])
)
return render_template(
"elements/forecast/forecast_section_template.html", location_data=location_data
)
@datapoint_webapp.route("/get_forecast_location_for_postcode", methods=["POST"])
def get_forecast_location_for_postcode():
passed_postcode = request.json["postcode"]
try:
postcode_lat, postcode_long = postcode_resolver.get_lat_long_for_postcode(
passed_postcode
)
return json.dumps(
datapoint_handler.get_forecast_location_for_coordinates(
postcode_lat, postcode_long
)
)
except KeyError:
return json.dumps(
{
"state": "failed",
"data": render_template("elements/error/postcode_fail.html"),
}
)
if __name__ == "__main__":
datapoint_webapp.run(host="0.0.0.0", port=5000, debug=True)