File size: 6,124 Bytes
b441403
 
 
c24fc80
b441403
 
 
3b2782b
b441403
 
 
 
 
 
 
 
 
ca30ea3
 
 
 
 
b441403
 
 
 
 
 
 
 
c24fc80
 
 
 
 
b441403
 
 
 
 
 
 
 
 
 
 
 
 
95bf27b
b441403
 
a0c0946
c24fc80
a0c0946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
577d253
 
 
 
 
 
 
 
 
 
 
 
 
99c2775
 
 
 
 
 
 
577d253
99c2775
577d253
 
a0c0946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b441403
3b2782b
 
 
 
 
b441403
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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()