This project is a sports analytics dashboard for NBA data. It provides detailed player and team statistics, along with interactive filtering and visualization capabilities.
- Ingest player and team data from the NBA API.
- Store data in a PostgreSQL database with schemas for development, staging, and production environments.
- Interactive web-based dashboard built with Flask and Tailwind CSS.
- Filterable and sortable statistics for players and teams.
- Python 3.9+
- PostgreSQL
- Node.js and npm (for Tailwind CSS)
- nba_api
git clone https://github.com/JoThurst/nba-sports-analytics.git
cd nba-sports-analytics
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
Ensure you have Node.js installed. Then install the necessary Tailwind CSS dependencies:
npm install
This installs tailwindcss
, postcss
, and autoprefixer
.
Tailwind CSS is used for styling the application. Here's how to set up and use it during development:
Tailwind is already set up in this project, but if you need to reinitialize it, run:
npx tailwindcss init
To generate the compiled output.css
file from the tailwind.css
source, run:
npm run build:css
The compiled CSS file will be saved in app/static/css/output.css
.
During development, you can watch for changes to your Tailwind CSS and Flask template files to rebuild the CSS automatically:
npm run watch:css
To customize the Tailwind configuration, edit the tailwind.config.js
file in the root of the project. For example, you can extend the theme or add custom plugins:
module.exports = {
content: [
'./app/templates/**/*.html',
'./app/static/**/*.js',
],
theme: {
extend: {
colors: {
customGray: '#2A2A2A',
},
},
},
plugins: [],
};
- Create a database named
nba_stats
. - Create schemas for
develop
,staging
, andproduction
. - Grant appropriate privileges to your users.
import psycopg2
from psycopg2 import sql
def get_connection(schema="develop"):
"""Establish and return a database connection."""
conn = psycopg2.connect(
database="postgres",
host="localhost",
user="postgres",
password="password",
port="5432"
)
cur = conn.cursor()
cur.execute(sql.SQL("SET search_path TO {};").format(sql.Identifier(schema)))
conn.commit()
return conn
def release_connection(conn):
"""Release the connection back to the pool."""
if connection_pool and conn:
connection_pool.putconn(conn)
def close_pool():
"""Close all connections when shutting down the app."""
if connection_pool:
connection_pool.closeall()
import psycopg2
from psycopg2 import sql, pool
DATABASE_URL = "<YOUR-CONNECTION-STRING>"
# Initialize the connection pool globally
connection_pool = pool.SimpleConnectionPool(
1, 10, DATABASE_URL
)
if connection_pool:
print("Connection pool created successfully")
def get_connection(schema="public"):
"""Get a database connection from the pool and set schema."""
if not connection_pool:
raise Exception("Connection pool is not initialized")
conn = connection_pool.getconn()
cur = conn.cursor()
cur.execute(sql.SQL("SET search_path TO {};").format(sql.Identifier(schema)))
conn.commit()
return conn
def release_connection(conn):
"""Release the connection back to the pool."""
if connection_pool and conn:
connection_pool.putconn(conn)
def close_pool():
"""Close all connections when shutting down the app."""
if connection_pool:
connection_pool.closeall()
Fetch and store players:
python ingest_data.py
python run.py
Create a new branch for your feature or bugfix:
git checkout -b feature-name
Commit your changes and push to your fork:
git add .
git commit -m "Description of changes"
git push origin feature-name
Submit a pull request describing your changes.
This project is licensed under the MIT License. See the LICENSE file for details.