forked from basse058/Project-3-Overdose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
82 lines (58 loc) · 2.06 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
82
import numpy as np
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, render_template
import flask_cors
from flask_cors import CORS, cross_origin
#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///CDC.sqlite")
# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(autoload_with=engine)
# Save reference to the table
overdose = Base.classes.od_data_df_ordered
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
#################################################
# Flask Routes
#################################################
@app.route("/")
@cross_origin()
def index():
return render_template('index.html')
def welcome():
"""It worked! List all available api routes."""
return (
f"Available Routes:<br/>"
f"/api/v1.0/od_data"
)
@app.route("/api/v1.0/od_data")
def data():
# Create our session (link) from Python to the DB
session = Session(engine)
"""Return a list of all passenger names"""
# Query all passengers
results = session.query(overdose.year, overdose.month, overdose.overdose_deaths, overdose.state, overdose.state_name).all()
session.close()
# Create a dictionary from the row data and append to a list of all_passengers
all_overdoses = []
for year, month, overdose_deaths, state, state_name in results:
overdose_dict = {}
overdose_dict["year"] = year
overdose_dict["month"] = month
overdose_dict["overdose_deaths"] = overdose_deaths
overdose_dict["state"] = state
overdose_dict["state_name"] = state_name
all_overdoses.append(overdose_dict)
return jsonify(all_overdoses)
if __name__ == '__main__':
app.run(debug=True)