-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
98 lines (89 loc) · 3.28 KB
/
Copy pathmain.py
File metadata and controls
98 lines (89 loc) · 3.28 KB
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
import os, sys, json
from models import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy import *
from heatmap import Heatmap
from flask import Flask, request, render_template
app = Flask(__name__)
Session = sessionmaker()
db = create_engine("sqlite:///shots.db",
connect_args={'check_same_thread':False})
Session.configure(bind=db)
session = Session()
@app.route("/")
def index():
teams = session.query(Team).all()
teams = [[team.id, team] for team in teams]
return render_template("heatmap.html", teams=teams)
@app.route("/canvas")
def canvas():
teams = session.query(Team).all()
teams = [[team.id, team] for team in teams]
return render_template("heatmap_canvas.html", teams=teams)
def free_throw_filter(shot_rows):
"""
Returns only rows of Shots table that are NOT free throws or
uncontested layups.
"""
filtered_shot_rows = []
for shot_row in shot_rows:
if (not (shot_row[0] == 0 and abs(shot_row[1]) in [28,42])
and ("Free" not in shot_row[3] and "Lay" not in shot_row[3])):
filtered_shot_rows.append(shot_row)
return filtered_shot_rows
@app.route('/get_shot_data', methods=['GET'])
def get_shot_data():
"""
Returns shot totals as json list.
"""
testing = True
player_id = request.args.get('player_id')
q = "select xcoord, ycoord, shotresult, " + \
" shot_type from shots where player_id=" + player_id
sqlresponse = session.execute(q)
shot_rows = [list(row) for row in sqlresponse.fetchall()]
filtered_shot_rows = free_throw_filter(shot_rows)
hm = Heatmap(filtered_shot_rows)
print hm.get_json_data()
return json.dumps(hm.get_json_data())
@app.route("/gen_heatmap_img", methods=['GET'])
def gen_heatmap_img():
"""
Generates heatmap image then returns img tag for it. Heatmap image is saved
to 'static/<player_id>.png'.
"""
testing = True
player_id = request.args.get('player_id')
path = player_id + ".png"
imtag = "<img src=\"static/" + path + "\">"
if path not in os.listdir("static/") or testing:
q = "select xcoord, ycoord, shotresult, " + \
" shot_type from shots where player_id=" + player_id
sqlresponse = session.execute(q)
shot_rows = [list(row) for row in sqlresponse.fetchall()]
i = len(shot_rows)
filtered_shot_rows = free_throw_filter(shot_rows)
hm = Heatmap(filtered_shot_rows)
hm.k_nearest_gen(rdist=4, sd=3.2)
path = "static/" + path
hm.im.save(path)
print path
return imtag
@app.route("/get_players", methods=['GET'])
def get_players():
"""
Returns all players with the given team id as json.
"""
tid = int(request.args.get('team_id')[4:])
json_players = []
t = session.query(Team).filter_by(id = tid).first()
t.players
for player in t.players:
if player.n_shots > 400:
name = player.full_name
name = name.replace("'", "")
json_players.append({'id': player.id, 'name': name})
return json.dumps({'teamname': t.name, 'players': json_players})
if __name__ == "__main__":
app.run(debug=True)