|
from dataclasses import dataclass |
|
import streamlit as st |
|
|
|
from config import DEFAULT_ICON |
|
from shared_page import common_page_config |
|
|
|
from queries.nflverse.github_data import get_weekly_rosters |
|
from login import check_password |
|
|
|
|
|
@dataclass |
|
class PlayerOption: |
|
full_name: str |
|
gsis_id: str |
|
headshot_url: str |
|
position: str |
|
team: str |
|
|
|
@classmethod |
|
def from_series(cls, input_series): |
|
return cls( |
|
full_name=input_series.full_name, |
|
gsis_id=input_series.gsis_id, |
|
headshot_url=input_series.headshot_url, |
|
position=input_series.position, |
|
team=input_series.team, |
|
) |
|
|
|
@classmethod |
|
def empty_player(cls): |
|
return cls( |
|
full_name="", |
|
gsis_id="", |
|
headshot_url="", |
|
position="", |
|
team="", |
|
) |
|
|
|
|
|
def player_options_from_df(df_options, position_filter: str) -> list[PlayerOption]: |
|
empty_first_option_list = [PlayerOption.empty_player()] |
|
return ( |
|
empty_first_option_list |
|
+ df_options[df_options.position == position_filter].apply(PlayerOption.from_series, axis=1).tolist() |
|
) |
|
|
|
|
|
|
|
def load_options(): |
|
|
|
df_rosters = get_weekly_rosters() |
|
df_rosters = df_rosters[df_rosters.status == "ACT"] |
|
|
|
sort_by_cols = ["position", "week", "fantasy_points"] |
|
df_rosters = df_rosters.sort_values(sort_by_cols, ascending=False).drop_duplicates(subset="gsis_id") |
|
qb_options = player_options_from_df(df_rosters, "QB") |
|
wr_options = player_options_from_df(df_rosters, "WR") |
|
rb_options = player_options_from_df(df_rosters, "RB") |
|
te_options = player_options_from_df(df_rosters, "TE") |
|
k_options = player_options_from_df(df_rosters, "K") |
|
return qb_options, wr_options, rb_options, te_options, k_options, df_rosters |
|
|
|
|
|
def format_player_option(player_opt: PlayerOption) -> str: |
|
return f"{player_opt.team} - {player_opt.full_name}" |
|
|
|
|
|
def display_player(player_opt: PlayerOption | None): |
|
if player_opt: |
|
if player_opt.headshot_url: |
|
st.image(player_opt.headshot_url, caption=player_opt.full_name) |
|
|
|
|
|
def get_page(): |
|
page_title = "Select Your Team" |
|
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide") |
|
common_page_config() |
|
st.title(page_title) |
|
if not check_password(): |
|
st.write("Sorry, you must be logged in first to play") |
|
st.stop() |
|
|
|
qb_options, wr_options, rb_options, te_options, k_options, df_rosters = load_options() |
|
|
|
selection_cols = st.columns(8) |
|
|
|
with selection_cols[0]: |
|
qb_1 = st.selectbox("QB", options=qb_options, format_func=format_player_option) |
|
display_player(qb_1) |
|
with selection_cols[1]: |
|
rb_1 = st.selectbox("RB1", options=rb_options, format_func=format_player_option) |
|
display_player(rb_1) |
|
with selection_cols[2]: |
|
rb_2 = st.selectbox("RB2", options=rb_options, format_func=format_player_option) |
|
display_player(rb_2) |
|
with selection_cols[3]: |
|
wr_1 = st.selectbox("WR1", options=wr_options, format_func=format_player_option) |
|
display_player(wr_1) |
|
with selection_cols[4]: |
|
wr_2 = st.selectbox("WR2", options=wr_options, format_func=format_player_option) |
|
display_player(wr_2) |
|
with selection_cols[5]: |
|
te_1 = st.selectbox("TE", options=te_options, format_func=format_player_option) |
|
display_player(te_1) |
|
with selection_cols[6]: |
|
k_1 = st.selectbox("K", options=k_options, format_func=format_player_option) |
|
display_player(k_1) |
|
with selection_cols[7]: |
|
def_1 = st.selectbox("DEF", options=[], format_func=format_player_option) |
|
display_player(def_1) |
|
|
|
|
|
if __name__ == "__main__": |
|
get_page() |
|
|