-
Notifications
You must be signed in to change notification settings - Fork 0
/
aq_dashboard.py
107 lines (85 loc) · 2.29 KB
/
aq_dashboard.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'''
OpenAQ Air Quality Dashboard with Flask.
'''
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import openaq
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
DB = SQLAlchemy(app)
# API obj
api = openaq.OpenAQ()
def get_data(city, parameter):
'''
retrieve data by city and param
'''
api = openaq.OpenAQ()
status, body = api.measurements(city=city, parameter=parameter)
results = [
(result['date']['utc'], result['value'])
for result in body['results']
]
return results
def query_DB():
'''
query DB to check results
'''
condition = Record.query.filter(Record.value >= 18).all()
return str(condition)
def get_results():
'''
get results with certain params
'''
api = openaq.OpenAQ()
status, body = api.measurements(
city='Los Angeles', parameter='pm25'
)
results = body['results']
values = []
for result in results:
values.append((result['date']['utc'], result['value']))
return values
@app.route('/')
def root():
'''
Main route
'''
roots = query_DB()
return roots
@app.route('/refresh')
def refresh():
'''
Pull fresh data from Open AQ and replace existing data.
'''
DB.drop_all()
DB.create_all()
# TODO Get data from OpenAQ, make Record objects with it, and add to db
for result in get_data('Los Angeles', 'pm25'):
record = Record(datetime=str(result[0]), value=result[1])
DB.session.add(record)
DB.session.commit()
return'Data Refreshed'
@app.route('/reset')
def reset():
'''
reset DB to ensure nothing is in DB before refreshing
'''
DB.drop_all()
DB.create_all()
return'DB reset!!!'
class Record(DB.Model):
'''
Create class with 3 attributes
, id
, datetime(string since sql doesnt recognize datetime objects)
, and value(float)
'''
# id (integer, primary key)
id = DB.Column(DB.Integer, primary_key=True)
# datetime (string)
datetime = DB.Column(DB.String(25))
# value (float, cannot be null)
value = DB.Column(DB.Float, nullable=False)
def __repr__(self):
return f'< -- Date & Time {self.datetime} --- Value {self.value} -- >'