Jon Solow
commited on
Commit
·
d45ec63
1
Parent(s):
203b087
Add set your team page with player images
Browse files- src/pages/10_Set_Your_Lineup.py +110 -0
src/pages/10_Set_Your_Lineup.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
from config import DEFAULT_ICON
|
5 |
+
from shared_page import common_page_config
|
6 |
+
|
7 |
+
from queries.nflverse.github_data import get_weekly_rosters
|
8 |
+
|
9 |
+
|
10 |
+
@dataclass
|
11 |
+
class PlayerOption:
|
12 |
+
full_name: str
|
13 |
+
gsis_id: str
|
14 |
+
headshot_url: str
|
15 |
+
position: str
|
16 |
+
team: str
|
17 |
+
|
18 |
+
@classmethod
|
19 |
+
def from_series(cls, input_series):
|
20 |
+
return cls(
|
21 |
+
full_name=input_series.full_name,
|
22 |
+
gsis_id=input_series.gsis_id,
|
23 |
+
headshot_url=input_series.headshot_url,
|
24 |
+
position=input_series.position,
|
25 |
+
team=input_series.team,
|
26 |
+
)
|
27 |
+
|
28 |
+
@classmethod
|
29 |
+
def empty_player(cls):
|
30 |
+
return cls(
|
31 |
+
full_name="",
|
32 |
+
gsis_id="",
|
33 |
+
headshot_url="",
|
34 |
+
position="",
|
35 |
+
team="",
|
36 |
+
)
|
37 |
+
|
38 |
+
|
39 |
+
def player_options_from_df(df_options, position_filter: str) -> list[PlayerOption]:
|
40 |
+
empty_first_option_list = [PlayerOption.empty_player()]
|
41 |
+
return (
|
42 |
+
empty_first_option_list
|
43 |
+
+ df_options[df_options.position == position_filter].apply(PlayerOption.from_series, axis=1).tolist()
|
44 |
+
)
|
45 |
+
|
46 |
+
|
47 |
+
# @st.cache_data(ttl=60 * 60 * 24)
|
48 |
+
def load_options():
|
49 |
+
# filter active only
|
50 |
+
df_rosters = get_weekly_rosters()
|
51 |
+
df_rosters = df_rosters[df_rosters.status == "ACT"]
|
52 |
+
# select latest
|
53 |
+
sort_by_cols = ["position", "week", "fantasy_points"]
|
54 |
+
df_rosters = df_rosters.sort_values(sort_by_cols, ascending=False).drop_duplicates(subset="gsis_id")
|
55 |
+
qb_options = player_options_from_df(df_rosters, "QB")
|
56 |
+
wr_options = player_options_from_df(df_rosters, "WR")
|
57 |
+
rb_options = player_options_from_df(df_rosters, "RB")
|
58 |
+
te_options = player_options_from_df(df_rosters, "TE")
|
59 |
+
k_options = player_options_from_df(df_rosters, "K")
|
60 |
+
return qb_options, wr_options, rb_options, te_options, k_options, df_rosters
|
61 |
+
|
62 |
+
|
63 |
+
def format_player_option(player_opt: PlayerOption) -> str:
|
64 |
+
return f"{player_opt.team} - {player_opt.full_name}"
|
65 |
+
|
66 |
+
|
67 |
+
def display_player(player_opt: PlayerOption | None):
|
68 |
+
if player_opt:
|
69 |
+
if player_opt.headshot_url:
|
70 |
+
st.image(player_opt.headshot_url, caption=player_opt.full_name)
|
71 |
+
|
72 |
+
|
73 |
+
def get_page():
|
74 |
+
page_title = "Select Your Team"
|
75 |
+
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
|
76 |
+
common_page_config()
|
77 |
+
st.title(page_title)
|
78 |
+
|
79 |
+
qb_options, wr_options, rb_options, te_options, k_options, df_rosters = load_options()
|
80 |
+
|
81 |
+
selection_cols = st.columns(8)
|
82 |
+
|
83 |
+
with selection_cols[0]:
|
84 |
+
qb_1 = st.selectbox("QB", options=qb_options, format_func=format_player_option)
|
85 |
+
display_player(qb_1)
|
86 |
+
with selection_cols[1]:
|
87 |
+
rb_1 = st.selectbox("RB1", options=rb_options, format_func=format_player_option)
|
88 |
+
display_player(rb_1)
|
89 |
+
with selection_cols[2]:
|
90 |
+
rb_2 = st.selectbox("RB2", options=rb_options, format_func=format_player_option)
|
91 |
+
display_player(rb_2)
|
92 |
+
with selection_cols[3]:
|
93 |
+
wr_1 = st.selectbox("WR1", options=wr_options, format_func=format_player_option)
|
94 |
+
display_player(wr_1)
|
95 |
+
with selection_cols[4]:
|
96 |
+
wr_2 = st.selectbox("WR2", options=wr_options, format_func=format_player_option)
|
97 |
+
display_player(wr_2)
|
98 |
+
with selection_cols[5]:
|
99 |
+
te_1 = st.selectbox("TE", options=te_options, format_func=format_player_option)
|
100 |
+
display_player(te_1)
|
101 |
+
with selection_cols[6]:
|
102 |
+
k_1 = st.selectbox("K", options=k_options, format_func=format_player_option)
|
103 |
+
display_player(k_1)
|
104 |
+
with selection_cols[7]:
|
105 |
+
def_1 = st.selectbox("DEF", options=[], format_func=format_player_option)
|
106 |
+
display_player(def_1)
|
107 |
+
|
108 |
+
|
109 |
+
if __name__ == "__main__":
|
110 |
+
get_page()
|