Jon Solow
commited on
Commit
·
3e6686b
1
Parent(s):
21c4781
Implement ability to save roster
Browse files- src/login.py +23 -1
- src/pages/10_Set_Your_Lineup.py +56 -31
src/login.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import hmac
|
| 2 |
import json
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
from streamlit_gsheets import GSheetsConnection
|
| 5 |
from streamlit.runtime.secrets import AttrDict, secrets_singleton
|
|
@@ -40,6 +41,7 @@ def get_user_id_from_email_if_exists(email: str) -> int:
|
|
| 40 |
df_email = conn.read(
|
| 41 |
worksheet="users",
|
| 42 |
usecols=[0, 1],
|
|
|
|
| 43 |
)
|
| 44 |
df_email = df_email[df_email.email.notna()]
|
| 45 |
for row in df_email.itertuples():
|
|
@@ -51,8 +53,9 @@ def get_user_id_from_email_if_exists(email: str) -> int:
|
|
| 51 |
def get_password_from_user_id(user_id: int) -> str | None:
|
| 52 |
try:
|
| 53 |
df_pass = conn.read(
|
| 54 |
-
worksheet=f"user-{user_id}",
|
| 55 |
usecols=[0],
|
|
|
|
| 56 |
)
|
| 57 |
return df_pass.password[0]
|
| 58 |
except Exception:
|
|
@@ -95,3 +98,22 @@ def check_password():
|
|
| 95 |
if st.session_state.get("password_correct", True) is False:
|
| 96 |
st.error("😕 User not known or password incorrect")
|
| 97 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import hmac
|
| 2 |
import json
|
| 3 |
+
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
from streamlit_gsheets import GSheetsConnection
|
| 6 |
from streamlit.runtime.secrets import AttrDict, secrets_singleton
|
|
|
|
| 41 |
df_email = conn.read(
|
| 42 |
worksheet="users",
|
| 43 |
usecols=[0, 1],
|
| 44 |
+
ttl=3600,
|
| 45 |
)
|
| 46 |
df_email = df_email[df_email.email.notna()]
|
| 47 |
for row in df_email.itertuples():
|
|
|
|
| 53 |
def get_password_from_user_id(user_id: int) -> str | None:
|
| 54 |
try:
|
| 55 |
df_pass = conn.read(
|
| 56 |
+
worksheet=f"user-{user_id}-password",
|
| 57 |
usecols=[0],
|
| 58 |
+
ttl=0,
|
| 59 |
)
|
| 60 |
return df_pass.password[0]
|
| 61 |
except Exception:
|
|
|
|
| 98 |
if st.session_state.get("password_correct", True) is False:
|
| 99 |
st.error("😕 User not known or password incorrect")
|
| 100 |
return False
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def get_user_team():
|
| 104 |
+
user_id = st.session_state.get("logged_in_user")
|
| 105 |
+
if not user_id:
|
| 106 |
+
return {}
|
| 107 |
+
df_team = conn.read(
|
| 108 |
+
worksheet=f"user-{user_id}-roster",
|
| 109 |
+
ttl=0,
|
| 110 |
+
)
|
| 111 |
+
return df_team.loc[0].to_dict()
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def save_user_team(team_selections):
|
| 115 |
+
user_id = st.session_state.get("logged_in_user")
|
| 116 |
+
conn.update(
|
| 117 |
+
worksheet=f"user-{user_id}-roster",
|
| 118 |
+
data=pd.DataFrame(team_selections, index=[0]),
|
| 119 |
+
)
|
src/pages/10_Set_Your_Lineup.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 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 |
-
from login import check_password
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
@dataclass
|
|
@@ -45,7 +47,7 @@ def player_options_from_df(df_options, position_filter: str) -> list[PlayerOptio
|
|
| 45 |
)
|
| 46 |
|
| 47 |
|
| 48 |
-
|
| 49 |
def load_options():
|
| 50 |
# filter active only
|
| 51 |
df_rosters = get_weekly_rosters()
|
|
@@ -58,7 +60,7 @@ def load_options():
|
|
| 58 |
rb_options = player_options_from_df(df_rosters, "RB")
|
| 59 |
te_options = player_options_from_df(df_rosters, "TE")
|
| 60 |
k_options = player_options_from_df(df_rosters, "K")
|
| 61 |
-
return qb_options, wr_options, rb_options, te_options, k_options
|
| 62 |
|
| 63 |
|
| 64 |
def format_player_option(player_opt: PlayerOption) -> str:
|
|
@@ -71,6 +73,32 @@ def display_player(player_opt: PlayerOption | None):
|
|
| 71 |
st.image(player_opt.headshot_url, caption=player_opt.full_name)
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def get_page():
|
| 75 |
page_title = "Select Your Team"
|
| 76 |
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
|
|
@@ -80,34 +108,31 @@ def get_page():
|
|
| 80 |
st.write("Sorry, you must be logged in first to play")
|
| 81 |
st.stop()
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
with selection_cols[7]:
|
| 109 |
-
def_1 = st.selectbox("DEF", options=[], format_func=format_player_option)
|
| 110 |
-
display_player(def_1)
|
| 111 |
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
|
|
|
| 1 |
from dataclasses import dataclass
|
| 2 |
+
import numpy as np
|
| 3 |
import streamlit as st
|
| 4 |
|
| 5 |
from config import DEFAULT_ICON
|
| 6 |
from shared_page import common_page_config
|
| 7 |
|
| 8 |
from queries.nflverse.github_data import get_weekly_rosters
|
| 9 |
+
from login import check_password, get_user_team, save_user_team
|
| 10 |
+
from domain.playoffs import PLAYOFF_WEEK_TO_NAME
|
| 11 |
|
| 12 |
|
| 13 |
@dataclass
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
+
@st.cache_data(ttl=60 * 60 * 24)
|
| 51 |
def load_options():
|
| 52 |
# filter active only
|
| 53 |
df_rosters = get_weekly_rosters()
|
|
|
|
| 60 |
rb_options = player_options_from_df(df_rosters, "RB")
|
| 61 |
te_options = player_options_from_df(df_rosters, "TE")
|
| 62 |
k_options = player_options_from_df(df_rosters, "K")
|
| 63 |
+
return qb_options, wr_options, rb_options, te_options, k_options
|
| 64 |
|
| 65 |
|
| 66 |
def format_player_option(player_opt: PlayerOption) -> str:
|
|
|
|
| 73 |
st.image(player_opt.headshot_url, caption=player_opt.full_name)
|
| 74 |
|
| 75 |
|
| 76 |
+
def position_cell(pos_label: str, options_list: list[PlayerOption], existing_selection_map):
|
| 77 |
+
if selected_player := existing_selection_map.get(pos_label) != np.nan:
|
| 78 |
+
try:
|
| 79 |
+
selected_option_idx, _ = next((i, v) for i, v in enumerate(options_list) if selected_player == v.gsis_id)
|
| 80 |
+
except StopIteration:
|
| 81 |
+
selected_option_idx = 0
|
| 82 |
+
else:
|
| 83 |
+
selected_option_idx = 0
|
| 84 |
+
|
| 85 |
+
selected_player_from_box = st.selectbox(
|
| 86 |
+
pos_label,
|
| 87 |
+
options=options_list,
|
| 88 |
+
format_func=format_player_option,
|
| 89 |
+
index=selected_option_idx,
|
| 90 |
+
)
|
| 91 |
+
if selected_player_from_box:
|
| 92 |
+
if selected_player_from_box.gsis_id and selected_player_from_box.gsis_id != selected_player:
|
| 93 |
+
update_and_save_selection(pos_label, selected_player_from_box.gsis_id, existing_selection_map),
|
| 94 |
+
display_player(selected_player_from_box)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def update_and_save_selection(pos_label: str, selection_id: str, existing_selection_map):
|
| 98 |
+
existing_selection_map[pos_label] = selection_id
|
| 99 |
+
save_user_team(existing_selection_map)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
def get_page():
|
| 103 |
page_title = "Select Your Team"
|
| 104 |
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
|
|
|
|
| 108 |
st.write("Sorry, you must be logged in first to play")
|
| 109 |
st.stop()
|
| 110 |
|
| 111 |
+
existing_selections = get_user_team()
|
| 112 |
+
st.write(get_user_team())
|
| 113 |
+
|
| 114 |
+
qb_options, wr_options, rb_options, te_options, k_options = load_options()
|
| 115 |
+
|
| 116 |
+
for week in range(1, 5):
|
| 117 |
+
st.header(PLAYOFF_WEEK_TO_NAME[week])
|
| 118 |
+
selection_cols = st.columns(8)
|
| 119 |
+
|
| 120 |
+
with selection_cols[0]:
|
| 121 |
+
position_cell(f"{week}-QB-1", qb_options, existing_selections)
|
| 122 |
+
with selection_cols[1]:
|
| 123 |
+
position_cell(f"{week}-RB-1", rb_options, existing_selections)
|
| 124 |
+
with selection_cols[2]:
|
| 125 |
+
position_cell(f"{week}-RB-2", rb_options, existing_selections)
|
| 126 |
+
with selection_cols[3]:
|
| 127 |
+
position_cell(f"{week}-WR-1", wr_options, existing_selections)
|
| 128 |
+
with selection_cols[4]:
|
| 129 |
+
position_cell(f"{week}-WR-2", wr_options, existing_selections)
|
| 130 |
+
with selection_cols[5]:
|
| 131 |
+
position_cell(f"{week}-TE-1", te_options, existing_selections)
|
| 132 |
+
with selection_cols[6]:
|
| 133 |
+
position_cell(f"{week}-K-1", k_options, existing_selections)
|
| 134 |
+
with selection_cols[7]:
|
| 135 |
+
position_cell(f"{week}-DEF-1", [], existing_selections)
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|