Spaces:
Sleeping
Sleeping
import os | |
import pandas as pd | |
import streamlit as st | |
from config import DEFAULT_ICON, LEAGUE_NAME, LEAGUE_NUMBER_TEAMS | |
from streamlit_filter import filter_dataframe | |
KEEPER_DATA_URL = "../../tests/mocks/2023_keepers.csv" | |
HEADSHOT_DATA_URL = "../../tests/mocks/2023_player_headshots.csv" | |
def load_player_ids() -> pd.DataFrame: | |
df = pd.read_csv(r"https://raw.githubusercontent.com/dynastyprocess/data/master/files/db_playerids.csv") | |
df["merge_id"] = df["yahoo_id"].combine_first(df["stats_id"]) | |
return df | |
def load_adp() -> pd.DataFrame: | |
df = pd.read_csv(r"https://raw.githubusercontent.com/dynastyprocess/data/master/files/values-players.csv") | |
return df | |
def add_opinionated_keeper_value(df: pd.DataFrame): | |
for score_fmt in ["1qb", "2qb"]: | |
df[f"value_{score_fmt}"] = (df["keeper_cost"] - 0.5) * LEAGUE_NUMBER_TEAMS - df[f"ecr_{score_fmt}"] | |
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: eval(x)[0]) | |
data.columns = data.columns.str.lower() | |
teams_list = sorted(list(data["team_name"].unique())) | |
# Merge player ids | |
df_player_ids = load_player_ids() | |
data = data.merge(df_player_ids, how="left", left_on="player_id", right_on="merge_id", suffixes=("", "_ids")) | |
# Merge ADP | |
df_adp = load_adp() | |
data = data.merge(df_adp, how="left", left_on="fantasypros_id", right_on="fp_id", suffixes=("", "_adp")) | |
add_opinionated_keeper_value(data) | |
return data, teams_list | |
def filtered_keeper_dataframe(data: pd.DataFrame, teams_list: list[str]): | |
teams_selected = st.multiselect("Team:", teams_list) | |
teams_filter = data["team_name"].isin(teams_selected) if teams_selected else data["team_name"].isin(teams_list) | |
show_columns = [ | |
"team_name", | |
"headshot_url", | |
"name", | |
"team", | |
"position", | |
"keeper_cost", | |
"years_eligible", | |
"eligible", | |
"ecr_1qb", | |
"value_1qb", | |
"ecr_2qb", | |
"value_2qb", | |
"ecr_pos", | |
] | |
filtered_data = data.loc[teams_filter, show_columns] | |
st.dataframe( | |
filter_dataframe(filtered_data), | |
hide_index=True, | |
use_container_width=True, | |
column_config={"headshot_url": st.column_config.ImageColumn(label="img")}, | |
) | |
def get_keeper_app(): | |
keeper_title = f"{LEAGUE_NAME} Keeper Options" | |
st.set_page_config(page_title=keeper_title, page_icon=DEFAULT_ICON, layout="wide") | |
st.title(keeper_title) | |
data, teams_list = load_data() | |
with st.container(): | |
filtered_keeper_dataframe(data, teams_list) | |
if __name__ == "__main__": | |
get_keeper_app() | |