-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
81 lines (69 loc) · 2.36 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
80
81
#setting up dependencies
import datetime as dt
import numpy as np
import pandas as pd
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
from flask import Flask, jsonify
#setting up create_engine and automap_base and prepare
engine = create_engine("sqlite:///hawaii.sqlite",\
connect_args={'check_same_thread':False})
Base = automap_base()
Base.prepare(engine, reflect=True)
#setting up table references
Measurement = Base.classes.measurement
Station = Base.classes.station
#setting up session link
session = Session(engine)
#Creating a new flask instance called app
app = Flask(__name__)
#create flask route
@app.route('/')
def welcome():
return(
'''
Welcome to the Climate Analysis API!<br/>
Available Routes:<br/>
/api/v1.0/precipitation<br/>
/api/v1.0/stations<br/>
/api/v1.0/tobs<br/>
/api/v1.0/temp/start/end <br/>
''')
@app.route("/api/v1.0/precipitation")
def precipitation():
prev_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
precipitation = session.query(\
Measurement.date, Measurement.prcp).filter(\
Measurement.date >= prev_year).all()
precip = {date: prcp for date, prcp in precipitation}
return jsonify(precip)
@app.route("/api/v1.0/stations")
def stations():
results = session.query(Station.station).all()
stations = list(np.ravel(results))
return jsonify(stations=stations)
@app.route("/api/v1.0/tobs")
def temp_monthly():
prev_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
results = session.query(Measurement.tobs).filter(\
Measurement.station == 'USC00519281').filter(\
Measurement.date >= prev_year).all()
temps = list(np.ravel(results))
return jsonify(temps=temps)
@app.route("/api/v1.0/temp/<start>")
@app.route("/api/v1.0/temp/<start>/<end>")
def stats(start=None, end=None):
sel = [func.min(Measurement.tobs), func.avg(\
Measurement.tobs),func.max(Measurement.tobs)]
if not end:
results = session.query(*sel).filter(\
Measurement.date <= start).all()
temps = list(np.ravel(results))
return jsonify(temps)
results = session.query(*sel).filter(\
Measurement.date >= start).filter(\
Measurement.date <= end).all()
temps = list(np.ravel(results))
return jsonify(temps=temps)