-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
79 lines (57 loc) · 1.92 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
79
import os
import sys
import logging
from flask import Flask
from flask import render_template
from flask import url_for
from flask.ext.sqlalchemy import SQLAlchemy
# Configuration
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
#app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///forecastaer'
db = SQLAlchemy(app)
# Models
class CurrentGridData(db.Model):
id = db.Column(db.Integer, primary_key=True)
gr_id = db.Column(db.Integer)
PM25 = db.Column(db.Float)
O3 = db.Column(db.Float)
AQI = db.Column(db.Float)
time = db.Column(db.DateTime)
def __init__(self, gr_id, PM25, O3, AQI, time):
self.gr_id = gr_id
self.PM25 = PM25
self.O3 = O3
self.AQI = AQI
self.time = time
def to_csv(self):
return ','.join([str(self.gr_id),
str(round(self.PM25, 2)),
str(round(self.O3, 2)),
str(round(self.AQI, 2)),
str(self.time)]) + '\n'
# Handlers
@app.route('/')
def FrontPage():
return render_template('map_nyc.html')
@app.route('/about')
def AboutPage():
return render_template('about.html')
@app.route('/grid_data.csv')
def CSVPage_grid_data():
"""
Query the most recent grid data from database, output as a .csv
"""
all_data = CurrentGridData.query.all()
out = 'gr_id,PM25,O3,AQI,time\n'
for record in all_data:
out += record.to_csv()
return out
if __name__ == '__main__':
app.run()
# Generate static urls for the data files that won't change
url_for('static', filename='breakpoints.csv')
url_for('static', filename='grid.geojson')
url_for('static', filename='grid_locs.csv')
url_for('static', filename='nyc_border_smaller.geojson')
url_for('static', filename='images/cur_loc.png')