Skip to content

Commit

Permalink
basic working tables
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanEmb committed Dec 1, 2024
1 parent 8c08f0e commit dd9c8f0
Show file tree
Hide file tree
Showing 7 changed files with 748 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#project
main.ipynb
.vscode/*

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.13
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "space-jam-dashboard"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"espn-api>=0.43.0",
"pandas>=2.2.3",
"panel>=1.5.4",
]
84 changes: 84 additions & 0 deletions src/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import pandas as pd

from espn_api.basketball import League

import constants as const


def get_league(league_id: int = const.SPACEJAM_LEAGUE_ID, year: int = const.YEAR):
return League(league_id, year)


def get_league_all_raw_stats_df(league: League):
league_stats = []
for team in league.teams:
temp_dict = team.stats.copy()
temp_dict["Team"] = team.team_name
temp_dict["Standing"] = team.standing
league_stats.append(temp_dict)

df = pd.DataFrame(league_stats)

df = df.astype(const.ALL_RAW_DATA_TABLE_DEF)
return df[list(const.ALL_RAW_DATA_TABLE_DEF.keys())]


def get_league_all_raw_data_rankings(league: League):
raw_stats_df = get_league_all_raw_stats_df(league)
# Rank only numeric columns
want_big_num_df = raw_stats_df[const.WANT_BIG_NUM]
want_small_num_df = raw_stats_df[const.WANT_SMALL_NUM]
want_big_ranked_df = want_big_num_df.rank(ascending=False).astype(int)
want_small_ranked_df = want_small_num_df.rank(ascending=True).astype(int)
ranked_df = pd.concat([want_big_ranked_df, want_small_ranked_df], axis=1)
ranked_df["Avg. Cat. Rank"] = ranked_df.mean(axis=1)

# Concatenate with non-numeric columns
ranked_df = pd.concat(
[
ranked_df,
raw_stats_df.select_dtypes(exclude="number"),
],
axis=1,
)

return ranked_df[list(const.ALL_DATA_RANKED_TABLE_DEF.keys())]


def get_league_cat_raw_stats_df(league: League):
league_stats = []
for team in league.teams:
temp_dict = team.stats.copy()
temp_dict["Team"] = team.team_name
temp_dict["Standing"] = team.standing
league_stats.append(temp_dict)

df = pd.DataFrame(league_stats)

df = df.astype(const.CAT_ONLY_RAW_DATA_TABLE_DEF)
return df[list(const.CAT_ONLY_RAW_DATA_TABLE_DEF.keys())]


def get_league_cat_data_rankings(league: League):
raw_stats_df = get_league_all_raw_stats_df(league)
# Rank only numeric columns
want_big_num_df = raw_stats_df[const.WANT_BIG_NUM]
want_small_num_df = raw_stats_df[const.WANT_SMALL_NUM]
want_big_ranked_df = want_big_num_df.rank(ascending=False).astype(int)
want_small_ranked_df = want_small_num_df.rank(ascending=True).astype(int)
ranked_df = pd.concat([want_big_ranked_df, want_small_ranked_df], axis=1)
ranked_df["Avg. Cat. Rank"] = ranked_df.mean(axis=1)

# Concatenate with non-numeric columns
ranked_df = pd.concat(
[
ranked_df,
raw_stats_df.select_dtypes(exclude="number"),
],
axis=1,
)
return ranked_df[list(const.CAT_ONLY_DATA_RANKED_TABLE_DEF.keys())]


if __name__ == "__main__":
pass
85 changes: 85 additions & 0 deletions src/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
SPACEJAM_LEAGUE_ID = 233677
YEAR = 2025

ALL_RAW_DATA_TABLE_DEF = {
"Team": str,
"Standing": int,
"PTS": int,
"BLK": int,
"STL": int,
"AST": int,
"REB": int,
"TO": int,
"FGM": int,
"FGA": int,
"FTM": int,
"FTA": int,
"3PM": int,
"FG%": float,
"FT%": float,
}

ALL_DATA_RANKED_TABLE_DEF = {
"Team": str,
"Standing": int,
"Avg. Cat. Rank": int,
"PTS": int,
"BLK": int,
"STL": int,
"AST": int,
"REB": int,
"TO": int,
"FGM": int,
"FGA": int,
"FTM": int,
"FTA": int,
"3PM": int,
"FG%": int,
"FT%": int,
}

CAT_ONLY_RAW_DATA_TABLE_DEF = {
"Team": str,
"Standing": int,
"PTS": int,
"BLK": int,
"STL": int,
"AST": int,
"REB": int,
"TO": int,
"3PM": int,
"FG%": float,
"FT%": float,
}

CAT_ONLY_DATA_RANKED_TABLE_DEF = {
"Team": str,
"Standing": int,
"Avg. Cat. Rank": int,
"PTS": int,
"BLK": int,
"STL": int,
"AST": int,
"REB": int,
"TO": int,
"3PM": int,
"FG%": int,
"FT%": int,
}

WANT_BIG_NUM = [
"PTS",
"BLK",
"STL",
"AST",
"REB",
"FGM",
"FGA",
"FTM",
"FTA",
"3PM",
"FG%",
"FT%",
]

WANT_SMALL_NUM = ["Standing", "TO"]
43 changes: 43 additions & 0 deletions src/frontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import panel as pn
import backend as be

pn.extension("tabulator")

all_stats = be.get_league_all_raw_stats_df(be.get_league())
all_rankings = be.get_league_all_raw_data_rankings(be.get_league())
cat_stats = be.get_league_cat_raw_stats_df(be.get_league())
cat_rankings = be.get_league_cat_data_rankings(be.get_league())


def get_dataframe(mode: pn.widgets.Select):
if mode == "All Data - Raw Stats":
df = all_stats
elif mode == "All Data - Ranked":
df = all_rankings
elif mode == "Categories - Raw Stats":
df = cat_stats
elif mode == "Categories - Rankings":
df = cat_rankings
return df


mode = pn.widgets.Select(
name="Mode",
options=[
"Categories - Rankings",
"Categories - Raw Stats",
"All Data - Ranked",
"All Data - Raw Stats",
],
)

interactive_df = pn.bind(get_dataframe, mode)

interactive_table = pn.widgets.Tabulator(interactive_df, theme="site", show_index=False)

pn.template.ReactTemplate(
title="Space Jam Dashboard",
sidebar=[mode],
main=[interactive_table],
sidebar_width=150,
).servable()
Loading

0 comments on commit dd9c8f0

Please sign in to comment.