-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
145 lines (114 loc) · 4.03 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# import dependancies
import os
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.ext.sqlalchemy import SQLAlchemy
import urllib
import psycopg2
from flask_heroku import Heroku
from flask import Flask, jsonify, render_template
app = Flask(__name__)
#################################################
# Database Setup
#################################################
@app.route("/")
def index():
return render_template('index.html')
@app.route("/regions")
def names():
# query the SQLite table and store in a df
stmt = session.query(wine_reviews).statement
df = pd.read_sql_query(stmt, session.bind)
# list of regions
regions = list(df['region_1'].unique())
return jsonify(regions)
@app.route("/varieties")
def types():
# query the SQLite table and store in a df
stmt = session.query(wine_reviews).statement
df = pd.read_sql_query(stmt, session.bind)
# list of grape varieties
variety = list(cluster_df['variety'].unique())
return jsonify(variety)
# data for marker clusters map
@app.route("/cluster_data")
def cluster():
# query the SQLite table and store in a df
stmt = session.query(wine_reviews).statement
df = pd.read_sql_query(stmt, session.bind)
# group the df by region_1,latitude and logitude and aggregate the grape variety
cluster_df = df.groupby(["region_1","latitude","longitude"]).agg({"variety":pd.Series.nunique}).reset_index()
# create json dictionary from the cluster_df dataframe to render the clusters map
json_data =[]
for index,row in cluster_df.iterrows():
location = {
"type": "Point",
"coordinates": [row['latitude'],row['longitude']] ,
"variety_count":row['variety'],
"region":row['region_1']
}
json_data.append(location)
return jsonify(json_data)
# Navigation between pages
@app.route("/bar.html")
def bar():
"""Return the homepage."""
return render_template('bar.html')
@app.route("/index.html")
def home():
return render_template('index.html')
# Routes for charts
@app.route('/states')
def states():
"""Return a list of sample names."""
# Use Pandas to perform the sql query
state_avg=pd.read_sql('prpt',engine)
states=list(state_avg['State'].unique())
# Return a list of the column names (sample names)
return jsonify(list(states))
@app.route("/stateData/<state>")
def stateData(state):
state_avg=pd.read_sql('prpt',engine)
stateData=state_avg[(state_avg['State']==state)]
stateData=stateData.sort_values(by='Avg_Points', ascending=1)
data= [{
"Avg_Points": stateData['Avg_Points'].values.tolist(),
"Avg_Price": stateData['Avg_Price'].values.tolist(),
"State": stateData['State'].values.tolist(),
"Variety":stateData['Variety'].values.tolist(),
"Title_Count":stateData['Title_Count'].values.tolist()
}]
return jsonify(data)
@app.route("/data.html")
def data():
"""Return the data for the table"""
return render_template('data.html')
@app.route("/tabledata")
def tabledata():
# Query for the number of wine reviews by state
stmt = session.query(wine_reviews).statement
df = pd.read_sql_query(stmt, session.bind)
del df['id']
del df['latitude']
del df['longitude']
df.dropna(how="any", inplace=True)
data = df.to_dict(orient='records')
# Returns json list of all reviews
return jsonify(data)
if __name__ == "__main__":
#dbfile = os.path.join('raw_data/wine_reviews.sqlite')
# dbfile = os.path.join('postgresql-shallow-66978')
engine = create_engine()
# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)
# Save references to each table
wine_reviews = Base.classes.reviews
# Create our session (link) from Python to the Database
session = Session(engine)
app.run(port=os.environ["PORT"], host="0.0.0.0")