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_all_selected_keepers @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_all_selected_keepers() def get_page(): page_title = f"{LEAGUE_NAME} - Selected Keepers" st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide") common_page_config() st.title(page_title) data, _ = load_data() st.header("Selected Keepers") selected_keepers = get_selected_keepers_with_retry() df_selected = pd.DataFrame(selected_keepers) if len(df_selected): cols_from_options = [ "player_id", "headshot_url", "name", "position", "years_eligible", "team_name", ] selected_data = data[cols_from_options].merge(df_selected, how="inner", on="player_id") st.dataframe( selected_data, hide_index=True, height=35 * (len(selected_data) + 1) + 5, column_order=[ "team_name", "headshot_url", "name", "position", "keeper_cost", "years_eligible", ], column_config={ "team_name": st.column_config.TextColumn(label="Team Name"), "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)", ), }, ) if __name__ == "__main__": get_page()