-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFlask.py
72 lines (53 loc) · 1.78 KB
/
Flask.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
# coding=utf-8
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
import datetime
import os
import time
app = Flask(__name__)
app.config['DATABASE'] = 'pm.db'
def get_db():
if not hasattr(g, '_database'):
db = sqlite3.connect(app.config['DATABASE'])
def make_dicts(cursor, row):
return dict((cursor.description[idx][0], value)
for idx, value in enumerate(row))
db.row_factory = make_dicts
g._database = db
return g._database
@app.teardown_appcontext
def close_db(error):
if hasattr(g, '_database'):
g._database.close()
def query_db(query, args=(), one=False):
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
return rv
@app.route('/')
def index():
return redirect(url_for('index_hour', hour=8))
@app.route('/<int:hour>')
def index_int(hour):
return redirect(url_for('index_hour', hour=hour))
@app.route('/<float:hour>')
def index_float(hour):
return redirect(url_for('index_hour', hour=hour))
@app.route('/<float:hour>hour')
def index_hour(hour):
before = int(time.time()*1000) - hour*3600*1000
data = query_db('select * from pm where timestamp>%d' % before)
while len(data) > 10000:
data = data[::2]
times = [x['timestamp'] for x in data]
pm1_0 = [x['pm1_0'] for x in data]
pm2_5 = [x['pm2_5'] for x in data]
pm10 = [x['pm10'] for x in data]
co2 = [x['co2'] for x in data]
return render_template('chart.html', date=times, pm1_0=pm1_0, pm2_5=pm2_5, pm10=pm10, co2=co2)
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
if __name__ == '__main__':
app.run(debug=True)