BraydenMoore commited on
Commit
5f81d51
1 Parent(s): 623a8f8

Improve speed and add week selector

Browse files
Files changed (3) hide show
  1. Source/Data/lines.json +56 -0
  2. Templates/index.html +80 -9
  3. main.py +42 -34
Source/Data/lines.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "2":[
3
+ 49,
4
+ 40.5,
5
+ 47,
6
+ 45.5,
7
+ 47,
8
+ 39.5,
9
+ 51.5,
10
+ 41,
11
+ 45,
12
+ 40,
13
+ 44.5,
14
+ 39,
15
+ 37.5,
16
+ 47,
17
+ 39.5,
18
+ 39
19
+ ],
20
+ "3":[
21
+ 44.5,
22
+ 43,
23
+ 38,
24
+ 47,
25
+ 41.5,
26
+ 43.5,
27
+ 47.5,
28
+ 54.5,
29
+ 35,
30
+ 43,
31
+ 42.5,
32
+ 44,
33
+ 48.8,
34
+ 43.5,
35
+ 45,
36
+ 43.5
37
+ ],
38
+ "4":[
39
+ 45.5,
40
+ 43,
41
+ 53.5,
42
+ 45.5,
43
+ 46,
44
+ 41,
45
+ 42.5,
46
+ 46.5,
47
+ 40.5,
48
+ 43.5,
49
+ 41,
50
+ 48,
51
+ 43,
52
+ 44.5,
53
+ 41.5,
54
+ 47
55
+ ]
56
+ }
Templates/index.html CHANGED
@@ -35,6 +35,7 @@
35
  }
36
 
37
  table {
 
38
  margin-top: 20px;
39
  width: 80%;
40
  border-collapse: collapse;
@@ -275,6 +276,15 @@
275
  display: inline-block;
276
  }
277
 
 
 
 
 
 
 
 
 
 
278
  @keyframes spin {
279
  0% {
280
  transform: rotate(0deg);
@@ -328,6 +338,10 @@
328
  <span class="label">Winners:</span> {{ winners_correct }}-{{winners_incorrect}}{{winners_tie}}<span class="label"> ({{ winners_return }})</span><br>
329
  <span class="label">Over/Unders:</span> {{over_unders_correct}}-{{over_unders_incorrect}}{{over_unders_push}}<span class="label"> ({{over_unders_return}})</span><br><br>
330
  </div>
 
 
 
 
331
  <div class="table-div">
332
  <table id="gameTable">
333
  <tr>
@@ -386,10 +400,15 @@
386
 
387
 
388
  <script>
389
- async function fetchGames() {
390
- const response = await fetch('/get_games');
391
  const pulled_games = await response.json();
392
  const table = document.getElementById('gameTable');
 
 
 
 
 
393
  const columns = ['Date','Away Team', 'Home Team'];
394
  const lines_response = await fetch('/get_lines');
395
  const lines = await lines_response.json()
@@ -561,17 +580,69 @@
561
 
562
  }
563
 
564
- document.getElementById('submitButton').addEventListener('click', submitData);
565
 
566
- fetchGames().then(() => {
567
- submitData();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
  });
569
 
570
- document.addEventListener('keydown', function(event) {
571
- if (event.keyCode === 13) {
572
- submitData();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573
  }
574
- });
 
 
 
 
 
 
 
 
 
 
 
575
 
576
 
577
  </script>
 
35
  }
36
 
37
  table {
38
+ transition: 0.3s ease;
39
  margin-top: 20px;
40
  width: 80%;
41
  border-collapse: collapse;
 
276
  display: inline-block;
277
  }
278
 
279
+ #weekSelector {
280
+ transition: 0.3s ease;
281
+ border-radius: 10px;
282
+ padding: 5px;
283
+ color: white;
284
+ background: rgb(30, 30, 30) !important;
285
+ font-family: Arial, Helvetica, sans-serif;
286
+ }
287
+
288
  @keyframes spin {
289
  0% {
290
  transform: rotate(0deg);
 
338
  <span class="label">Winners:</span> {{ winners_correct }}-{{winners_incorrect}}{{winners_tie}}<span class="label"> ({{ winners_return }})</span><br>
339
  <span class="label">Over/Unders:</span> {{over_unders_correct}}-{{over_unders_incorrect}}{{over_unders_push}}<span class="label"> ({{over_unders_return}})</span><br><br>
340
  </div>
341
+
342
+ <select id="weekSelector">
343
+ </select>
344
+
345
  <div class="table-div">
346
  <table id="gameTable">
347
  <tr>
 
400
 
401
 
402
  <script>
403
+ async function fetchGames(selectedWeek) {
404
+ const response = await fetch(`/get_games?week=${selectedWeek}`);
405
  const pulled_games = await response.json();
406
  const table = document.getElementById('gameTable');
407
+
408
+ for(let i = table.rows.length - 1; i > 0; i--) {
409
+ table.deleteRow(i);
410
+ }
411
+
412
  const columns = ['Date','Away Team', 'Home Team'];
413
  const lines_response = await fetch('/get_lines');
414
  const lines = await lines_response.json()
 
580
 
581
  }
582
 
 
583
 
584
+ // Populate dropdown
585
+ let selectedWeek;
586
+ async function populateDropdown() {
587
+ const weekSelector = document.getElementById('weekSelector');
588
+ weekSelector.innerHTML = "";
589
+
590
+ const response = await fetch('/get_weeks');
591
+ const data = await response.json();
592
+
593
+ data.forEach((week, index) => {
594
+ const option = document.createElement('option');
595
+ option.value = week;
596
+ option.text = `Week ${week}`;
597
+ weekSelector.appendChild(option);
598
+
599
+ if (index === 0) {
600
+ selectedWeek = week;
601
+ }
602
+ });
603
+ }
604
+
605
+
606
+ // Get new games when new week selected
607
+ document.getElementById('weekSelector').addEventListener('change', function(event) {
608
+ selectedWeek = event.target.value;
609
+ getNew();
610
  });
611
 
612
+
613
+ // Initial load
614
+ function loadThings() {
615
+ populateDropdown()
616
+ .then(() => fetchGames(selectedWeek))
617
+ .then(() => submitData())
618
+ .catch(error => console.error(error));
619
+ }
620
+
621
+ // Get new
622
+ async function getNew() {
623
+ const table = document.getElementById('gameTable');
624
+ table.style.opacity = "0.5";
625
+
626
+ try {
627
+ await fetchGames(selectedWeek);
628
+ await submitData();
629
+ table.style.opacity = "1";
630
+ } catch (error) {
631
+ console.error(error);
632
+ }
633
  }
634
+
635
+
636
+ // Submit on click, enter, and pageload
637
+ loadThings();
638
+
639
+ document.getElementById('submitButton').addEventListener('click', submitData);
640
+
641
+ document.addEventListener('keydown', function(event) {
642
+ if (event.keyCode === 13) {
643
+ submitData();
644
+ }
645
+ });
646
 
647
 
648
  </script>
main.py CHANGED
@@ -1,5 +1,5 @@
1
  from Source.Predict import predict
2
- from flask import Flask, render_template, jsonify, request
3
  import requests
4
  import pickle as pkl
5
  import pandas as pd
@@ -7,54 +7,66 @@ import numpy as np
7
  pd.set_option('display.max_columns', None)
8
  pd.set_option('display.expand_frame_repr', False)
9
 
10
- import os
11
  import json
12
  with open('Source/Data/record.json','r') as f:
13
  record = json.load(f)
14
-
15
- # input the over/under lines here for quicker predictions!
16
- lines = [45.5,
17
- 43,
18
- 53.5,
19
- 45.5,
20
- 46,
21
- 41,
22
- 42.5,
23
- 46.5,
24
- 40.5,
25
- 43.5,
26
- 41,
27
- 48,
28
- 43,
29
- 44.5,
30
- 41.5,
31
- 47]
32
-
33
- # get week, season
34
- week, season = predict.get_week()
35
 
36
  app = Flask(__name__, template_folder="Templates", static_folder="Static", static_url_path="/Static")
37
  app.secret_key = 'green-flounder'
38
 
39
- games = predict.get_games(week)[['Date','Away Team','Home Team']]
 
 
 
 
40
 
 
41
  @app.route('/')
42
  def index():
 
 
43
  return render_template('index.html', **record)
44
 
 
 
 
 
 
 
45
  @app.route('/get_lines')
46
  def get_lines():
47
- return jsonify(lines)
48
 
 
49
  @app.route('/get_games')
50
  def get_games():
51
- return jsonify(games.to_dict(orient='records'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
 
53
  @app.route('/submit_games', methods=['POST'])
54
  def submit_games():
55
  data = request.json
56
  data = pd.DataFrame(data).replace('', np.nan).dropna()
57
- print(data)
58
  home_teams = data['HomeTeam'].values
59
  away_teams = data['AwayTeam'].values
60
  ou_lines = data['OverUnderLine'].values
@@ -63,19 +75,15 @@ def submit_games():
63
  moneylines = []
64
  over_unders = []
65
  for row_index,home,away,total in zip(row_indices,home_teams,away_teams,ou_lines):
66
- game_id, moneyline, over_under = predict.predict(home,away,season,week,total)
 
67
  moneyline['rowIndex'] = int(row_index)
68
  over_under['rowIndex'] = int(row_index)
69
  moneylines.append(moneyline)
70
  over_unders.append(over_under)
71
 
72
- print('MoneyLines')
73
- print(moneylines)
74
- print('OverUnders')
75
- print(over_unders)
76
-
77
  return jsonify({'moneylines': moneylines,
78
  'over_unders': over_unders})
79
 
80
  if __name__ == '__main__':
81
- app.run(host='0.0.0.0', port='7860')
 
1
  from Source.Predict import predict
2
+ from flask import Flask, render_template, jsonify, request, session
3
  import requests
4
  import pickle as pkl
5
  import pandas as pd
 
7
  pd.set_option('display.max_columns', None)
8
  pd.set_option('display.expand_frame_repr', False)
9
 
 
10
  import json
11
  with open('Source/Data/record.json','r') as f:
12
  record = json.load(f)
13
+ with open('Source/Data/lines.json','r') as f:
14
+ lines = json.load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  app = Flask(__name__, template_folder="Templates", static_folder="Static", static_url_path="/Static")
17
  app.secret_key = 'green-flounder'
18
 
19
+ # get week, season
20
+ current_week, season = predict.get_week()
21
+ current_games = predict.get_games(current_week)[['Date','Away Team','Home Team']]
22
+ available_weeks = list(range(current_week+1))[2:]
23
+ available_weeks.reverse()
24
 
25
+ # load current data by default
26
  @app.route('/')
27
  def index():
28
+ session['selected_week'] = current_week
29
+ session[f'games_week_{current_week}'] = current_games.to_json()
30
  return render_template('index.html', **record)
31
 
32
+ # send week list to front end
33
+ @app.route('/get_weeks')
34
+ def get_weeks():
35
+ return jsonify(available_weeks)
36
+
37
+ # send lines to front end
38
  @app.route('/get_lines')
39
  def get_lines():
40
+ return jsonify(lines[str(session.get('selected_week'))])
41
 
42
+ # send games of selected week to front end
43
  @app.route('/get_games')
44
  def get_games():
45
+ requested_week = int(request.args.get('week'))
46
+ session['selected_week'] = requested_week
47
+
48
+ # If select a new week
49
+ if requested_week and requested_week != current_week:
50
+
51
+ # Check if that week's games are cached
52
+ if session.get(f'games_week_{requested_week}'):
53
+ print("Using cached games")
54
+ games = session.get(f'games_week_{requested_week}')
55
+ games = json.loads(games)
56
+ return jsonify(games)
57
+ else:
58
+ games = predict.get_games(requested_week)[['Date','Away Team','Home Team']]
59
+ session[f'games_week_{requested_week}'] = games.to_json(orient='records')
60
+ return jsonify(games.to_dict(orient='records'))
61
+ else:
62
+ games = current_games
63
+ return jsonify(games.to_dict(orient='records'))
64
 
65
+ # make predictions
66
  @app.route('/submit_games', methods=['POST'])
67
  def submit_games():
68
  data = request.json
69
  data = pd.DataFrame(data).replace('', np.nan).dropna()
 
70
  home_teams = data['HomeTeam'].values
71
  away_teams = data['AwayTeam'].values
72
  ou_lines = data['OverUnderLine'].values
 
75
  moneylines = []
76
  over_unders = []
77
  for row_index,home,away,total in zip(row_indices,home_teams,away_teams,ou_lines):
78
+ selected_week = session.get('selected_week')
79
+ game_id, moneyline, over_under = predict.predict(home,away,season,selected_week,total)
80
  moneyline['rowIndex'] = int(row_index)
81
  over_under['rowIndex'] = int(row_index)
82
  moneylines.append(moneyline)
83
  over_unders.append(over_under)
84
 
 
 
 
 
 
85
  return jsonify({'moneylines': moneylines,
86
  'over_unders': over_unders})
87
 
88
  if __name__ == '__main__':
89
+ app.run(host='0.0.0.0', port='7860', debug=True)