File size: 3,306 Bytes
3231b63
1a89c67
3231b63
7727a49
3231b63
 
112a098
 
3231b63
1ab47cd
74a75f8
 
1a89c67
 
3231b63
5dac924
1a89c67
 
 
 
3231b63
 
1a89c67
 
 
 
 
3231b63
1a89c67
3231b63
 
1a89c67
 
 
74a75f8
3231b63
1a89c67
 
 
 
 
 
c1442da
 
1a89c67
 
 
 
c1442da
1a89c67
3231b63
 
1a89c67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3231b63
1a89c67
3231b63
 
 
 
 
 
 
b2e7453
 
3231b63
 
b2e7453
1a89c67
 
61f43a3
 
3231b63
 
0d3f186
3231b63
 
 
 
1a89c67
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
from Source.Predict import predict
from flask import Flask, render_template, jsonify, request, session
import requests
import pickle as pkl
import pandas as pd
import numpy as np
pd.set_option('display.max_columns', None)
pd.set_option('display.expand_frame_repr', False)

import json
with open('Source/Data/record.json','r') as f:
    record = json.load(f)
with open('Source/Data/lines.json','r') as f:
    lines = json.load(f)

app = Flask(__name__, template_folder="Templates", static_folder="Static", static_url_path="/Static")
app.config.update(
    SESSION_COOKIE_SECURE=True,
    SESSION_COOKIE_SAMESITE='None',
)
app.secret_key = 'green-flounder'

# get week, season
current_week, season = predict.get_week()
current_games = predict.get_games(current_week)[['Date','Away Team','Home Team']]
available_weeks = list(range(current_week+1))[2:]
available_weeks.reverse()

# load current data by default
@app.route('/')
def index():
    print(current_week)
    session['selected_week'] = current_week
    session[f'games_week_{current_week}'] = current_games.to_json()
    return render_template('index.html', **record)

# send week list to front end
@app.route('/get_weeks')
def get_weeks():
    return jsonify(available_weeks)

# send lines to front end
@app.route('/get_lines')
def get_lines():
    try:
        return jsonify(lines[str(session.get('selected_week'))])
    except:
        return jsonify(lines[str(current_week)])

# send games of selected week to front end
@app.route('/get_games')
def get_games():
    requested_week = int(request.args.get('week'))
    session['selected_week'] = requested_week

    # If select a new week
    if requested_week and requested_week != current_week:
        
        # Check if that week's games are cached
        if session.get(f'games_week_{requested_week}'):
            print("Using cached games")
            games = session.get(f'games_week_{requested_week}')
            games = json.loads(games)
            return jsonify(games)
        else:
            games = predict.get_games(requested_week)[['Date','Away Team','Home Team']]
            session[f'games_week_{requested_week}'] = games.to_json(orient='records')
            return jsonify(games.to_dict(orient='records'))
    else:
        games = current_games
        return jsonify(games.to_dict(orient='records'))

# make predictions
@app.route('/submit_games', methods=['POST'])
def submit_games():
    data = request.json
    data = pd.DataFrame(data).replace('', np.nan).dropna()
    home_teams = data['HomeTeam'].values
    away_teams = data['AwayTeam'].values
    ou_lines = data['OverUnderLine'].values
    row_indices = data['rowIndex'].values
    
    moneylines = []
    over_unders = []
    for row_index,home,away,total in zip(row_indices,home_teams,away_teams,ou_lines):
        selected_week = session.get('selected_week')
        game_id, moneyline, over_under = predict.predict(home,away,season,selected_week,total)
        moneyline['rowIndex'] = int(row_index)
        over_under['rowIndex'] = int(row_index)
        moneylines.append(moneyline)
        over_unders.append(over_under)

    return jsonify({'moneylines': moneylines,
                    'over_unders': over_unders})
    
if __name__ == '__main__':
    app.run(host='0.0.0.0', port='7860', debug=True)