-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
65 lines (52 loc) · 1.94 KB
/
application.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
"""
This flask application reads data of Canada cities from
a database on the disk, in the same directory where application
is located, and provides this data through endpoints to the user
through a html page with two select elements.
"""
from flask import Flask, render_template, jsonify, request
from models import * # import Province and City classes
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///ca.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
@app.route("/")
def index():
"""
When the home page is loaded, the list of provinces
are also loaded to the first select element of the
page.
"""
provinces = Province.query.all()
return render_template("index.html", provinces=provinces)
@app.route("/province", methods=['POST'])
def cities():
"""
This function return the list of cities of the given
province specified by corresponding province_id.
"""
try:
province_id = int(request.form.get("province_id"))
except:
return jsonify({'Error': 'id should be of type Integer'})
cities = Province.query.get(province_id).cities
if cities is None:
return render_template("error.html", message="No such province.")
# create a list capable to be converted to a json object using jsonify
list_cities = []
for city in cities:
list_cities.append({'id': city.id, 'name': city.name})
# This one also works: return jsonify({'cities': list_cities})
return jsonify(cities=list_cities)
@app.route("/city/<int:city_id>", methods=['POST'])
def single_city(city_id):
"""
receiving the id of a city and returning the population
of that city.
"""
city = City.query.get(city_id)
if city is None:
return jsonify({'Error': 'No such city!'})
return jsonify(population=city.population, extra=2)
if __name__ == "__main__":
app.run(debug=True)