Jon Solow
commited on
Commit
·
6d2d129
1
Parent(s):
755c89e
Move keeper load options to module to share
Browse files- src/Home.py +2 -1
- src/load_options.py +117 -0
- src/pages/10_Set_Your_Lineup.py +2 -112
src/Home.py
CHANGED
@@ -15,7 +15,8 @@ def get_app():
|
|
15 |
Additional Pool Rules can be found at this [worksheet](https://docs.google.com/spreadsheets/d/1aCxj383alEIpRSrROwY8vOMFDeVf175lcLUXm_I6Fok/edit?usp=sharing)
|
16 |
|
17 |
"""
|
18 |
-
)
|
|
|
19 |
|
20 |
if __name__ == "__main__":
|
21 |
get_app()
|
|
|
15 |
Additional Pool Rules can be found at this [worksheet](https://docs.google.com/spreadsheets/d/1aCxj383alEIpRSrROwY8vOMFDeVf175lcLUXm_I6Fok/edit?usp=sharing)
|
16 |
|
17 |
"""
|
18 |
+
)
|
19 |
+
|
20 |
|
21 |
if __name__ == "__main__":
|
22 |
get_app()
|
src/load_options.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
from domain.constants import SEASON
|
6 |
+
from domain.playoffs import (
|
7 |
+
PLAYOFF_WEEK_TO_NAME,
|
8 |
+
ROSTER_WEEK_TO_PLAYOFF_WEEK,
|
9 |
+
PLAYOFFS_TEAMS,
|
10 |
+
PLAYOFF_TEAM_DEF_PLAYER,
|
11 |
+
)
|
12 |
+
from domain.teams import SCHEDULE_NAME_TO_PFR_NAME_MAP
|
13 |
+
from queries.nflverse.github_data import get_weekly_rosters
|
14 |
+
from queries.pfr.league_schedule import get_season_time_map
|
15 |
+
|
16 |
+
|
17 |
+
@dataclass
|
18 |
+
class PlayerOption:
|
19 |
+
full_name: str
|
20 |
+
gsis_id: str
|
21 |
+
headshot_url: str
|
22 |
+
position: str
|
23 |
+
team: str
|
24 |
+
gametime: pd.Timestamp | None
|
25 |
+
week: int | None
|
26 |
+
|
27 |
+
@classmethod
|
28 |
+
def from_series(cls, input_series):
|
29 |
+
return cls(
|
30 |
+
full_name=input_series.full_name,
|
31 |
+
gsis_id=input_series.gsis_id,
|
32 |
+
headshot_url=input_series.headshot_url,
|
33 |
+
position=input_series.position,
|
34 |
+
team=input_series.team,
|
35 |
+
gametime=input_series.gametime,
|
36 |
+
week=int(input_series.week),
|
37 |
+
)
|
38 |
+
|
39 |
+
@classmethod
|
40 |
+
def empty_player(cls, week: int | None = None):
|
41 |
+
return cls(full_name="", gsis_id="", headshot_url="", position="", team="", gametime=None, week=week)
|
42 |
+
|
43 |
+
def is_locked(self):
|
44 |
+
if not self.gametime:
|
45 |
+
return False
|
46 |
+
else:
|
47 |
+
date_compare = (pd.Timestamp.now(tz="America/New_York")) + pd.Timedelta(days=0, hours=0)
|
48 |
+
return self.gametime < date_compare
|
49 |
+
|
50 |
+
|
51 |
+
def initialize_empty_options_map() -> dict[str, dict[int, list[PlayerOption]]]:
|
52 |
+
options_map: dict[str, dict[int, list[PlayerOption]]] = {}
|
53 |
+
for pos in ["QB", "RB", "WR", "TE", "K", "DEF"]:
|
54 |
+
options_map[pos] = {}
|
55 |
+
for week in PLAYOFF_WEEK_TO_NAME.keys():
|
56 |
+
options_map[pos][int(week)] = [PlayerOption.empty_player(week=week)]
|
57 |
+
return options_map
|
58 |
+
|
59 |
+
|
60 |
+
def player_options_from_df(df_options) -> dict[str, dict[int, list[PlayerOption]]]:
|
61 |
+
options_map = initialize_empty_options_map()
|
62 |
+
for pos, pos_week_map in options_map.items():
|
63 |
+
for week in pos_week_map.keys():
|
64 |
+
df_pos_week = df_options[((df_options.week == week) & (df_options.position == pos))]
|
65 |
+
if len(df_pos_week) > 0:
|
66 |
+
player_options_list = df_pos_week.apply(PlayerOption.from_series, axis=1).tolist()
|
67 |
+
options_map[pos][int(week)].extend(player_options_list)
|
68 |
+
return options_map
|
69 |
+
|
70 |
+
|
71 |
+
def modify_defensive_players_to_be_team_defense(df_options):
|
72 |
+
for team, player_id in PLAYOFF_TEAM_DEF_PLAYER:
|
73 |
+
if player_id in df_options.gsis_id.values:
|
74 |
+
df_options.loc[df_options.gsis_id == player_id, "position"] = "DEF"
|
75 |
+
df_options.loc[df_options.gsis_id == player_id, "full_name"] = team.team_name
|
76 |
+
|
77 |
+
|
78 |
+
def display_player(player_opt: PlayerOption | None):
|
79 |
+
if player_opt:
|
80 |
+
if player_opt.headshot_url:
|
81 |
+
st.image(player_opt.headshot_url)
|
82 |
+
if player_opt.full_name:
|
83 |
+
st.write(player_opt.full_name)
|
84 |
+
st.write(f"{player_opt.team} - {player_opt.gametime.strftime('%-m/%-d %-I:%M %p')}")
|
85 |
+
|
86 |
+
|
87 |
+
@st.cache_data(ttl=60 * 60 * 24)
|
88 |
+
def load_options():
|
89 |
+
df_rosters = get_weekly_rosters()
|
90 |
+
|
91 |
+
# get game schedules
|
92 |
+
week_game_times = get_season_time_map(SEASON)
|
93 |
+
latest_game_time_defaults = {k: max(v.values()) for k, v in week_game_times.items() if v}
|
94 |
+
|
95 |
+
# sort
|
96 |
+
sort_by_cols = ["position", "week", "fantasy_points"]
|
97 |
+
df_rosters.sort_values(sort_by_cols, ascending=False, inplace=True)
|
98 |
+
|
99 |
+
# filter data from non-playoffs
|
100 |
+
df_rosters = df_rosters[df_rosters.week.isin(ROSTER_WEEK_TO_PLAYOFF_WEEK.keys())]
|
101 |
+
df_rosters["week"] = df_rosters["week"].map(ROSTER_WEEK_TO_PLAYOFF_WEEK)
|
102 |
+
# set gametime
|
103 |
+
if len(df_rosters) == 0:
|
104 |
+
return initialize_empty_options_map()
|
105 |
+
df_rosters["gametime"] = df_rosters.apply(
|
106 |
+
lambda x: week_game_times.get(x.week, {}).get(
|
107 |
+
SCHEDULE_NAME_TO_PFR_NAME_MAP[x.team], latest_game_time_defaults.get(x.week, None)
|
108 |
+
),
|
109 |
+
axis=1,
|
110 |
+
)
|
111 |
+
|
112 |
+
df_rosters["in_playoffs"] = df_rosters.apply(lambda x: x.team in PLAYOFFS_TEAMS[x.week], axis=1)
|
113 |
+
|
114 |
+
df_rosters = df_rosters[df_rosters.in_playoffs]
|
115 |
+
modify_defensive_players_to_be_team_defense(df_rosters)
|
116 |
+
player_options = player_options_from_df(df_rosters)
|
117 |
+
return player_options
|
src/pages/10_Set_Your_Lineup.py
CHANGED
@@ -1,25 +1,18 @@
|
|
1 |
-
from dataclasses import dataclass
|
2 |
-
import pandas as pd
|
3 |
import streamlit as st
|
4 |
import streamlit.components.v1 as components
|
5 |
|
6 |
from config import DEFAULT_ICON
|
7 |
from shared_page import common_page_config
|
8 |
|
9 |
-
from domain.constants import SEASON
|
10 |
from domain.playoffs import (
|
11 |
PLAYOFF_WEEK_TO_NAME,
|
12 |
CURRENT_PLAYOFF_WEEK,
|
13 |
-
ROSTER_WEEK_TO_PLAYOFF_WEEK,
|
14 |
-
PLAYOFFS_TEAMS,
|
15 |
-
PLAYOFF_TEAM_DEF_PLAYER,
|
16 |
)
|
17 |
-
from domain.teams import SCHEDULE_NAME_TO_PFR_NAME_MAP
|
18 |
-
from queries.nflverse.github_data import get_weekly_rosters
|
19 |
-
from queries.pfr.league_schedule import get_season_time_map
|
20 |
from login import check_password
|
21 |
from data_storage import update_selection, get_user_team
|
22 |
|
|
|
|
|
23 |
|
24 |
def set_selectbox_readonly():
|
25 |
components.html(
|
@@ -41,113 +34,10 @@ def set_selectbox_readonly():
|
|
41 |
)
|
42 |
|
43 |
|
44 |
-
@dataclass
|
45 |
-
class PlayerOption:
|
46 |
-
full_name: str
|
47 |
-
gsis_id: str
|
48 |
-
headshot_url: str
|
49 |
-
position: str
|
50 |
-
team: str
|
51 |
-
gametime: pd.Timestamp | None
|
52 |
-
week: int | None
|
53 |
-
|
54 |
-
@classmethod
|
55 |
-
def from_series(cls, input_series):
|
56 |
-
return cls(
|
57 |
-
full_name=input_series.full_name,
|
58 |
-
gsis_id=input_series.gsis_id,
|
59 |
-
headshot_url=input_series.headshot_url,
|
60 |
-
position=input_series.position,
|
61 |
-
team=input_series.team,
|
62 |
-
gametime=input_series.gametime,
|
63 |
-
week=int(input_series.week),
|
64 |
-
)
|
65 |
-
|
66 |
-
@classmethod
|
67 |
-
def empty_player(cls, week: int | None = None):
|
68 |
-
return cls(full_name="", gsis_id="", headshot_url="", position="", team="", gametime=None, week=week)
|
69 |
-
|
70 |
-
def is_locked(self):
|
71 |
-
if not self.gametime:
|
72 |
-
return False
|
73 |
-
else:
|
74 |
-
date_compare = (pd.Timestamp.now(tz="America/New_York")) + pd.Timedelta(days=0, hours=0)
|
75 |
-
return self.gametime < date_compare
|
76 |
-
|
77 |
-
|
78 |
-
def initialize_empty_options_map() -> dict[str, dict[int, list[PlayerOption]]]:
|
79 |
-
options_map: dict[str, dict[int, list[PlayerOption]]] = {}
|
80 |
-
for pos in ["QB", "RB", "WR", "TE", "K", "DEF"]:
|
81 |
-
options_map[pos] = {}
|
82 |
-
for week in PLAYOFF_WEEK_TO_NAME.keys():
|
83 |
-
options_map[pos][int(week)] = [PlayerOption.empty_player(week=week)]
|
84 |
-
return options_map
|
85 |
-
|
86 |
-
|
87 |
-
def player_options_from_df(df_options) -> dict[str, dict[int, list[PlayerOption]]]:
|
88 |
-
options_map = initialize_empty_options_map()
|
89 |
-
for pos, pos_week_map in options_map.items():
|
90 |
-
for week in pos_week_map.keys():
|
91 |
-
df_pos_week = df_options[((df_options.week == week) & (df_options.position == pos))]
|
92 |
-
if len(df_pos_week) > 0:
|
93 |
-
player_options_list = df_pos_week.apply(PlayerOption.from_series, axis=1).tolist()
|
94 |
-
options_map[pos][int(week)].extend(player_options_list)
|
95 |
-
return options_map
|
96 |
-
|
97 |
-
|
98 |
-
def modify_defensive_players_to_be_team_defense(df_options):
|
99 |
-
for team, player_id in PLAYOFF_TEAM_DEF_PLAYER:
|
100 |
-
if player_id in df_options.gsis_id.values:
|
101 |
-
df_options.loc[df_options.gsis_id == player_id, "position"] = "DEF"
|
102 |
-
df_options.loc[df_options.gsis_id == player_id, "full_name"] = team.team_name
|
103 |
-
|
104 |
-
|
105 |
-
@st.cache_data(ttl=60 * 60 * 24)
|
106 |
-
def load_options():
|
107 |
-
df_rosters = get_weekly_rosters()
|
108 |
-
|
109 |
-
# get game schedules
|
110 |
-
week_game_times = get_season_time_map(SEASON)
|
111 |
-
latest_game_time_defaults = {k: max(v.values()) for k, v in week_game_times.items() if v}
|
112 |
-
|
113 |
-
# sort
|
114 |
-
sort_by_cols = ["position", "week", "fantasy_points"]
|
115 |
-
df_rosters.sort_values(sort_by_cols, ascending=False, inplace=True)
|
116 |
-
|
117 |
-
# filter data from non-playoffs
|
118 |
-
df_rosters = df_rosters[df_rosters.week.isin(ROSTER_WEEK_TO_PLAYOFF_WEEK.keys())]
|
119 |
-
df_rosters["week"] = df_rosters["week"].map(ROSTER_WEEK_TO_PLAYOFF_WEEK)
|
120 |
-
# set gametime
|
121 |
-
if len(df_rosters) == 0:
|
122 |
-
return initialize_empty_options_map()
|
123 |
-
df_rosters["gametime"] = df_rosters.apply(
|
124 |
-
lambda x: week_game_times.get(x.week, {}).get(
|
125 |
-
SCHEDULE_NAME_TO_PFR_NAME_MAP[x.team], latest_game_time_defaults.get(x.week, None)
|
126 |
-
),
|
127 |
-
axis=1,
|
128 |
-
)
|
129 |
-
|
130 |
-
df_rosters["in_playoffs"] = df_rosters.apply(lambda x: x.team in PLAYOFFS_TEAMS[x.week], axis=1)
|
131 |
-
|
132 |
-
df_rosters = df_rosters[df_rosters.in_playoffs]
|
133 |
-
modify_defensive_players_to_be_team_defense(df_rosters)
|
134 |
-
player_options = player_options_from_df(df_rosters)
|
135 |
-
return player_options
|
136 |
-
|
137 |
-
|
138 |
def format_player_option(player_opt: PlayerOption) -> str:
|
139 |
return f"{player_opt.team} - {player_opt.full_name}"
|
140 |
|
141 |
|
142 |
-
def display_player(player_opt: PlayerOption | None):
|
143 |
-
if player_opt:
|
144 |
-
if player_opt.headshot_url:
|
145 |
-
st.image(player_opt.headshot_url)
|
146 |
-
if player_opt.full_name:
|
147 |
-
st.write(player_opt.full_name)
|
148 |
-
st.write(f"{player_opt.team} - {player_opt.gametime.strftime('%-m/%-d %-I:%M %p')}")
|
149 |
-
|
150 |
-
|
151 |
def position_cell(
|
152 |
week: str, pos_str: str, pos_idx: int, options_map: dict[str, dict[int, list[PlayerOption]]], existing_selection_map
|
153 |
):
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import streamlit.components.v1 as components
|
3 |
|
4 |
from config import DEFAULT_ICON
|
5 |
from shared_page import common_page_config
|
6 |
|
|
|
7 |
from domain.playoffs import (
|
8 |
PLAYOFF_WEEK_TO_NAME,
|
9 |
CURRENT_PLAYOFF_WEEK,
|
|
|
|
|
|
|
10 |
)
|
|
|
|
|
|
|
11 |
from login import check_password
|
12 |
from data_storage import update_selection, get_user_team
|
13 |
|
14 |
+
from load_options import load_options, PlayerOption, display_player
|
15 |
+
|
16 |
|
17 |
def set_selectbox_readonly():
|
18 |
components.html(
|
|
|
34 |
)
|
35 |
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
def format_player_option(player_opt: PlayerOption) -> str:
|
38 |
return f"{player_opt.team} - {player_opt.full_name}"
|
39 |
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def position_cell(
|
42 |
week: str, pos_str: str, pos_idx: int, options_map: dict[str, dict[int, list[PlayerOption]]], existing_selection_map
|
43 |
):
|