YFDashboard / src /pages /9_Team_Formations.py
Jon Solow
Allow for grouping of team formations
a4ba037
raw
history blame
2.51 kB
import datetime
import pandas as pd
import streamlit as st
from config import DEFAULT_ICON
from shared_page import common_page_config
from queries.footballguys.constants import YEAR
from queries.nflverse.github_data import get_pbp_participation, get_current_tables, SEASON
def load_data():
data = get_pbp_participation(YEAR)
teams_list = sorted(filter(None, data.possession_team.unique()))
# position_list = data.position.unique()
# weeks_list = sorted(data.week.unique())
data_load_time_str = datetime.datetime.utcnow().strftime("%m/%d/%Y %I:%M %p")
return data, teams_list, data_load_time_str
def get_page():
page_title = f"Team Formations - {YEAR}"
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
common_page_config()
st.title(page_title)
if f"ftn_charting_ftn_charting_{SEASON}" not in get_current_tables():
st.write("Data not loaded.")
st.write("Check loaded data [here](./Load_Data)")
return
data, teams_list, data_load_time_str = load_data()
st.write(f"Data loaded as of: {data_load_time_str} UTC")
default_groups = [
"down",
"play_type",
"offense_personnel",
]
group_options = [
"week",
"down",
"qtr",
"ydstogo",
"play_type",
"pass_length",
"pass_location",
"possession_team",
"offense_formation",
"offense_personnel",
"number_of_pass_rushers",
"defenders_in_box",
"defense_personnel",
]
group_by_selected = st.multiselect("Group by:", group_options) or default_groups
team_selected = st.selectbox("Team:", teams_list)
week_selection = st.slider(
"Filter Week Range:",
min_value=data["week"].min(),
max_value=data["week"].max(),
value=(data["week"].min(), data["week"].max()),
step=1,
)
with st.container():
filtered_data = data[
(data.possession_team == team_selected)
& (data.play_type.isin(["pass", "run"]))
& (data["week"].between(*week_selection))
]
st.dataframe(
pd.pivot_table(
filtered_data,
values="count_col",
index=group_by_selected,
columns="week",
aggfunc={"count_col": "sum"},
# margins=True,
),
use_container_width=False,
)
if __name__ == "__main__":
get_page()