Added boxscores API to Flask server

This commit is contained in:
Lucas
2024-02-27 22:01:58 -08:00
parent 44d86a8752
commit d735010ca4
5 changed files with 93 additions and 0 deletions

Binary file not shown.

22
src/boxscores.py Normal file
View File

@@ -0,0 +1,22 @@
from nba_api.stats.endpoints import boxscoretraditionalv2
from nba_api.stats.static import teams
from nba_api.stats.endpoints import leaguegamefinder
def boxscores_For_Latest_Game():
nba_teams = teams.get_teams()
# Select the dictionary for the Celtics, which contains their team ID
kings = [team for team in nba_teams if team['abbreviation'] == 'SAC'][0]
kings_id = kings['id']
# Query for games where the Celtics were playing
gamefinder = leaguegamefinder.LeagueGameFinder(team_id_nullable=kings_id)
# The first DataFrame of those returned is what we want.
games = gamefinder.get_data_frames()[0]
games.head()
boxScore = boxscoretraditionalv2.BoxScoreTraditionalV2(games.head()["GAME_ID"][0])
return(boxScore.get_dict())

55
src/index.html Normal file
View File

@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BANG</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>
</head>
<body>
<h2>TABLE</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Player</th>
<th>FGM</th>
<th>FGA</th>
</tr>
</thead>
<tbody>
<td id="player"></td>
<td id="fgm"></td>
</tbody>
</table>
<script>
// Extracting PLAYER_NAME from the JSON
$.getJSON('http://127.0.0.1:5000/boxscore', function(jsonData) {
// Extracting PLAYER_NAME from the JSON
const playerStats = jsonData.resultSets[0].rowSet.map(row => ({
playerName: row[5],
fgm: row[10],
fga: row[11]
}));
// Selecting the table body
const tbody = $('tbody');
// Populating the table with player names
playerStats.forEach(stats => {
if (stats.fga == null) {
stats.fga = 0;
stats.fgm = 0;
}
const row = $('<tr>');
const playerNameCell = $('<td>').text(stats.playerName);
const fgaCell = $('<td>').text(stats.fgm);
const fgmCell = $('<td>').text(stats.fga);
row.append(playerNameCell, fgaCell, fgmCell); // Appending both cells to the same row
tbody.append(row);
});
});
</script>
</body>
</html>

14
src/main.py Normal file
View File

@@ -0,0 +1,14 @@
import boxscores
from flask import Flask, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/boxscore")
def names():
return jsonify(boxscores.boxscores_For_Latest_Game())
if __name__ == "__main__":
app.run(debug=True)

2
src/requirements.txt Normal file
View File

@@ -0,0 +1,2 @@
nba_api
flask