Spaces:
Sleeping
Sleeping
Jon Solow
commited on
Commit
·
4569c98
1
Parent(s):
f23c47c
Add next gen stats page
Browse files- src/pages/11_Next_Gen_Stats.py +72 -0
- src/queries/nflverse/github_data.py +5 -0
- src/streamlit_filter.py +12 -0
src/pages/11_Next_Gen_Stats.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from config import DEFAULT_ICON
|
4 |
+
from shared_page import common_page_config
|
5 |
+
|
6 |
+
from streamlit_filter import get_multiselect_for_df_column
|
7 |
+
from queries.nflverse.github_data import get_nextgen_stats, get_current_tables, SEASON
|
8 |
+
|
9 |
+
|
10 |
+
hide_columns = [
|
11 |
+
"season",
|
12 |
+
"season_type",
|
13 |
+
"player_gsis_id",
|
14 |
+
"player_first_name",
|
15 |
+
"player_last_name",
|
16 |
+
"player_jersey_number",
|
17 |
+
"player_short_name",
|
18 |
+
]
|
19 |
+
|
20 |
+
|
21 |
+
def get_page():
|
22 |
+
page_title = f"Next Gen Stats - {SEASON}"
|
23 |
+
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
|
24 |
+
common_page_config()
|
25 |
+
st.title(page_title)
|
26 |
+
|
27 |
+
stat_category = st.selectbox("Stat Category", ["Passing", "Rushing", "Receiving"])
|
28 |
+
ngs_table_name = f"nextgen_stats_ngs_{stat_category.lower()}"
|
29 |
+
current_tables_list = get_current_tables()
|
30 |
+
|
31 |
+
if ngs_table_name not in current_tables_list:
|
32 |
+
st.write("Data not loaded.")
|
33 |
+
st.write("Check loaded data [here](./Load_Data)")
|
34 |
+
return
|
35 |
+
data = get_nextgen_stats(SEASON, stat_category)
|
36 |
+
|
37 |
+
season_or_week = st.selectbox("Season or Weekly Stats", ["Season", "Week"])
|
38 |
+
if season_or_week == "Season":
|
39 |
+
data = data[data["week"] == 0]
|
40 |
+
data.drop(columns=["week"], inplace=True)
|
41 |
+
else:
|
42 |
+
data = data[data["week"] > 0]
|
43 |
+
week_selection = st.slider(
|
44 |
+
"Filter Week Range:",
|
45 |
+
min_value=data["week"].min(),
|
46 |
+
max_value=data["week"].max(),
|
47 |
+
value=(data["week"].min(), data["week"].max()),
|
48 |
+
step=1,
|
49 |
+
)
|
50 |
+
data = data[data["week"].between(*week_selection)]
|
51 |
+
|
52 |
+
data.drop(columns=hide_columns, inplace=True)
|
53 |
+
positions_selected = get_multiselect_for_df_column(data, "player_position")
|
54 |
+
teams_selected = get_multiselect_for_df_column(data, "team_abbr")
|
55 |
+
|
56 |
+
data = data[(data["player_position"].isin(positions_selected) & data["team_abbr"].isin(teams_selected))]
|
57 |
+
|
58 |
+
with st.container():
|
59 |
+
filtered_data = data
|
60 |
+
st.dataframe(
|
61 |
+
filtered_data,
|
62 |
+
hide_index=True,
|
63 |
+
# height=35 * (len(filtered_data) + 1) + 12,
|
64 |
+
use_container_width=False,
|
65 |
+
column_config={
|
66 |
+
"team_name": st.column_config.TextColumn(label="League Team", help="Name of fantasy League team."),
|
67 |
+
},
|
68 |
+
)
|
69 |
+
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
get_page()
|
src/queries/nflverse/github_data.py
CHANGED
@@ -63,6 +63,11 @@ def get_pbp_participation(season_int: int) -> pd.DataFrame:
|
|
63 |
return df
|
64 |
|
65 |
|
|
|
|
|
|
|
|
|
|
|
66 |
SEASON = "2023"
|
67 |
|
68 |
NFLVERSE_ASSETS = [
|
|
|
63 |
return df
|
64 |
|
65 |
|
66 |
+
def get_nextgen_stats(season_int: int, stat_category: str) -> pd.DataFrame:
|
67 |
+
df = duckdb.sql(f"SELECT * from nextgen_stats_ngs_{stat_category} where season = {season_int}").df()
|
68 |
+
return df
|
69 |
+
|
70 |
+
|
71 |
SEASON = "2023"
|
72 |
|
73 |
NFLVERSE_ASSETS = [
|
src/streamlit_filter.py
CHANGED
@@ -88,3 +88,15 @@ def filter_dataframe(df: pd.DataFrame, force_on: bool = False, force_on_columns:
|
|
88 |
df = df[df[column].astype(str).str.contains(user_text_input)]
|
89 |
|
90 |
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
df = df[df[column].astype(str).str.contains(user_text_input)]
|
89 |
|
90 |
return df
|
91 |
+
|
92 |
+
|
93 |
+
def get_multiselect_for_df_column(df: pd.DataFrame, column_name: str) -> list:
|
94 |
+
options_list = sorted(df[column_name].unique().tolist())
|
95 |
+
if len(options_list) > 1:
|
96 |
+
selected = (
|
97 |
+
st.multiselect(column_name.title(), options_list, placeholder=f"Select a {column_name} to filter")
|
98 |
+
or options_list
|
99 |
+
)
|
100 |
+
else:
|
101 |
+
selected = options_list
|
102 |
+
return selected
|