-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
118 lines (97 loc) · 3.15 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
from flask import Flask, render_template, redirect, jsonify
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, inspect
import pandas as pd
engine = create_engine("sqlite:///ETL_KD/Resources/mlsseasons.sqlite", echo=False)
app = Flask(__name__)
# Route to render index.html template using data from Mongo
@app.route("/")
def home():
# Return template and data
# return "Hello World!<br/><a href=zeus>Zeus</a>"
return render_template("index.html")
# Route that will trigger the scrape function
@app.route("/zeus")
def zeus():
data = []
sample = engine.execute("""
select --s."First Name" as firstName, s."Last Name" as lastName,
c.Club, c.pts, c.season, c.W, c.L, c.GF, c.GA,
avg(s."Total Compensation") as totalCompensation,
avg(s."Base Salary") as baseSalary
from seasons c
join club_map m on m.long_name = c.Club
join salaries s on s."Club (grouped)" = m.short_name
where c.season = s.Season
group by c.Club, c.pts, c.season, c.W, c.L, c.GF, c.GA
""")
for row in sample:
record = {}
for k, v in zip(sample.keys(), row):
record[k] = v
data.append(record)
return jsonify(data)
@app.route("/season/<year>")
def season(year):
data = []
sample = engine.execute(f"""
select c.pts, c.season, c.W, c.L, c.GF, c.GA,
c."#" as rank,
m.real_name as club,
s."Total Compensation" as totalComp,
s."Base Salary" as salary
from seasons c
join club_map m on m.long_name = c.Club
join salaries s on s."Club (grouped)" = m.short_name
where c.season = s.Season
and c.season = ?
""", year)
for row in sample:
record = {}
for k, v in zip(sample.keys(), row):
record[k] = v
data.append(record)
return jsonify(data)
# club = "2018; drop table blah;"
@app.route("/slice/<year>/<club>")
def season_club(year, club):
data = []
sample = engine.execute(f"""
select c.pts, c.season, c.W, c.L, c.GF, c.GA,
m.real_name as club,
s."Total Compensation" as totalComp,
s."Base Salary" as salary
from seasons c
join club_map m on m.long_name = c.Club
join salaries s on s."Club (grouped)" = m.short_name
where c.season = s.Season
and c.season = ?
and m.real_name = ?
""", year, club)
for row in sample:
record = {}
for k, v in zip(sample.keys(), row):
record[k] = v
data.append(record)
return jsonify(data)
@app.route("/clubs/<year>")
def clubs(year):
data = []
sample = engine.execute("""
select c.pts, c.W, c.L, c.GF, c.GA, c.GD,
c."#" as rank,
m.real_name as club
from seasons c
join club_map m on m.long_name = c.Club
and c.season = ?
""", year)
for row in sample:
record = {}
for k, v in zip(sample.keys(), row):
record[k] = v
data.append(record)
return jsonify(data)
if __name__ == "__main__":
app.run(debug=True)