-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
72 lines (46 loc) · 1.31 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
import json
import requests
from flask import Flask
from flask import request, render_template, jsonify, make_response
app = Flask(__name__)
'''
Load Data
'''
data_url = 'https://raw.githubusercontent.com/hvo/datasets/master/nyc_restaurants_by_cuisine.json'
def load_data():
'''load data from url'''
return json.loads(requests.get(data_url).content)
def load_zipcode(zipcode):
'''get the cuisine counts for the requested zipcode'''
return sorted([
dict(
cuisine=d['cuisine'],
total=d['perZip'].get(zipcode, 0)
)
for d in data
], key=lambda d: d['total'], reverse=True)
data = load_data()
all_zipcodes = list(set([
zcode for d in data
for zcode in list(d['perZip'].keys())
]))
'''
App Routes
'''
@app.route('/')
def index():
'''Renders the page'''
return render_template('index.html', all_zipcodes=all_zipcodes)
@app.route('/restaurants')
@app.route('/restaurants/<zipcode>')
def get_zipcode_data(zipcode=None):
'''Returns a json array of cuisine and counts for the specified zipcode'''
zip_data = load_zipcode(zipcode)
return jsonify(zip_data)
@app.route('/chart')
@app.route('/chart/<zipcode>')
def get_zipcode_chart(zipcode=None):
'''Render the altair chart'''
return render_template('altair/bar_chart.json', zipcode=zipcode)
if __name__ == '__main__':
app.run(debug=True, port=5001)