import streamlit as st import streamlit.components.v1 as components from config import DEFAULT_ICON from shared_page import common_page_config from domain.playoffs import ( PLAYOFF_WEEK_TO_NAME, CURRENT_PLAYOFF_WEEK, ) from login import check_password from data_storage import update_selection, get_user_team from load_options import load_options, PlayerOption, display_player def set_selectbox_readonly(): components.html( """ """, width=0, height=0, ) def format_player_option(player_opt: PlayerOption) -> str: return f"{player_opt.full_name} - {player_opt.team}" def position_cell( week: str, pos_str: str, pos_idx: int, options_map: dict[str, dict[int, list[PlayerOption]]], existing_selection_map ): pos_label = f"{week}-{pos_str}-{pos_idx}" selected_id = existing_selection_map.get(pos_label) options_list = options_map[pos_str][int(week)] non_locked_options = [x for x in options_list if not x.is_locked()] disabled = False # get selected player by id from options try: selected_player = next(v for v in options_list if str(selected_id) == str(v.gsis_id)) except Exception: selected_player = PlayerOption.empty_player() if int(week) > CURRENT_PLAYOFF_WEEK: # future week, no options options = [] selected_option_idx = 0 disabled = True elif int(week) < CURRENT_PLAYOFF_WEEK or selected_player.is_locked(): # past week, can't change options = [selected_player] selected_option_idx = 0 disabled = True else: options = non_locked_options # set to index of current select. This should exist, but try-except for safety try: selected_option_idx = non_locked_options.index(selected_player) except ValueError: selected_option_idx = 0 selected_player_from_box = st.selectbox( pos_str, options=options, format_func=format_player_option, index=selected_option_idx, key=pos_label, disabled=disabled, ) if selected_player_from_box and int(week) == CURRENT_PLAYOFF_WEEK: if selected_player_from_box.gsis_id and selected_player_from_box.gsis_id != selected_id: if selected_player_from_box.is_locked(): st.warning("Sorry player's game has already started", icon="🚨") display_player(selected_player) return elif selected_player_from_box.gsis_id in existing_selection_map.values(): st.warning("Player already in lineup. Please choose another.", icon="🚨") display_player(selected_player) return else: update_and_save_selection(pos_label, selected_player_from_box.gsis_id), display_player(selected_player_from_box) else: display_player(selected_player) def update_and_save_selection(pos_label: str, selection_id: str): update_selection(st.session_state["logged_in_user"], pos_label, selection_id) st.rerun() def week_selections(week, player_options, existing_selections): selection_cols = st.columns(8) with selection_cols[0]: position_cell(week, "QB", 1, player_options, existing_selections) with selection_cols[1]: position_cell(week, "RB", 1, player_options, existing_selections) with selection_cols[2]: position_cell(week, "RB", 2, player_options, existing_selections) with selection_cols[3]: position_cell(week, "WR", 1, player_options, existing_selections) with selection_cols[4]: position_cell(week, "WR", 2, player_options, existing_selections) with selection_cols[5]: position_cell(week, "TE", 1, player_options, existing_selections) with selection_cols[6]: position_cell(week, "K", 1, player_options, existing_selections) with selection_cols[7]: position_cell(week, "DEF", 1, player_options, existing_selections) def get_page(): page_title = "Select Your Team" st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide") common_page_config() if not check_password(): st.write("Sorry, you must be logged in first to play") st.stop() st.title(page_title) if st.button("Refresh Data"): st.rerun() existing_selections = get_user_team(st.session_state["logged_in_user"]) player_options = load_options() for week in range(1, 5): existing_week_selections = {k: v for k, v in existing_selections.items() if k[0] == str(week)} st.header(PLAYOFF_WEEK_TO_NAME[week]) if week < CURRENT_PLAYOFF_WEEK: with st.expander("Show Previous Week"): week_selections(week, player_options, existing_week_selections) else: week_selections(week, player_options, existing_week_selections) set_selectbox_readonly() if __name__ == "__main__": get_page()