import os import pandas as pd import streamlit as st from tenacity import retry from config import DEFAULT_ICON, LEAGUE_NAME, KEEPER_DATA_URL from shared_page import common_page_config from queries.supabase_db.client import get_selected_keepers, select_keeper, reset_keepers from login_component import is_token_in_session @st.cache_data(ttl=60 * 60 * 24) def load_data(): data = pd.read_csv(os.path.join(os.path.dirname(__file__), KEEPER_DATA_URL), index_col=0) # Hack to get position, replace with better position from yahoo api in future data["position"] = ( data["eligible_positions"] .apply(lambda x: x.split(" ")[0]) .str.replace("[", "") .str.replace("]", "") .str.replace("'", "") ) data.columns = data.columns.str.lower() data.sort_values(["team_name", "keeper_cost"], inplace=True) teams_list = sorted(list(data["team_name"].unique())) return data, teams_list @retry() def get_selected_keepers_with_retry(): return get_selected_keepers(st.session_state.get("logged_in_guid")) def get_page(): page_title = f"{LEAGUE_NAME} - My Keepers" st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide") common_page_config() st.title(page_title) if not is_token_in_session(): st.write( "You must authorize the application to access your account in order to use this feature." " Please click Login button above." ) else: st.warning("Work in progress. Please report any issues.") data, _ = load_data() data_user = data[(data["manager_guid"] == st.session_state.get("logged_in_guid")) & (data["eligible"])] st.header("Selected Keepers") selected_keeper_cost_map = get_selected_keepers_with_retry() if selected_keeper_cost_map: selected_data = data_user[data_user.player_id.isin(selected_keeper_cost_map.keys())] selected_data["keeper_cost"] = selected_data.player_id.map(selected_keeper_cost_map) st.dataframe( selected_data, hide_index=True, height=35 * (len(selected_data) + 1) + 5, column_order=[ "headshot_url", "name", "position", "keeper_cost", "years_eligible", ], column_config={ "headshot_url": st.column_config.ImageColumn(label="", help="Player image"), "name": st.column_config.TextColumn(label="Name", help="Player's name"), "position": st.column_config.TextColumn(label="Position", help="Player's position"), "keeper_cost": st.column_config.NumberColumn( label="Keeper Cost", help="Draft Round Cost to keep player. See Rules for details." ), "years_eligible": st.column_config.NumberColumn( label="Years Eligible", help="Number of further consecutive seasons player can be kept (subject to maximum of 2)", ), }, ) st.header("Add Keeper") if len(selected_keeper_cost_map) >= 3: st.warning( "You have selected your maximum of 3 keepers. Please remove a player if you wish to add another." ) else: selection = st.selectbox( "Add Keeper", data_user[~data_user.player_id.isin(selected_keeper_cost_map.keys())].to_dict(orient="records"), format_func=lambda x: f"{x['name']} - {x['keeper_cost']}", index=None, ) if selection: effective_cost = selection["keeper_cost"] while effective_cost in selected_keeper_cost_map.values(): effective_cost -= 1 if effective_cost != selection["keeper_cost"]: st.info( f"Note: Player cost has been adjusted to round {effective_cost} because another keeper selection has conflicting cost. If you wish to change the effective order, please reset selections and begin your selections with the player you want to take in the latest round." ) if st.button("Add Selection"): select_keeper(selection["player_id"], selection["manager_guid"], effective_cost) st.rerun() st.header("Options") if st.checkbox("Show all my options"): st.dataframe( data_user, hide_index=True, height=35 * (len(data_user) + 1) + 5, column_order=[ "headshot_url", "name", "team", "position", "keeper_cost", "years_eligible", ], column_config={ "headshot_url": st.column_config.ImageColumn(label="", help="Player image"), "name": st.column_config.TextColumn(label="Name", help="Player's name"), "team": st.column_config.TextColumn(label="NFL Team"), "position": st.column_config.TextColumn(label="Position", help="Player's position"), "keeper_cost": st.column_config.NumberColumn( label="Keeper Cost", help="Draft Round Cost to keep player. See Rules for details." ), "years_eligible": st.column_config.NumberColumn( label="Years Eligible", help="Number of further consecutive seasons player can be kept (subject to maximum of 2)", ), }, ) st.header("Reset All Keepers") if st.button("Reset all my selections"): reset_keepers(st.session_state.get("logged_in_guid")) st.rerun() if __name__ == "__main__": get_page()