File size: 3,017 Bytes
b586854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import streamlit as st

from config import DEFAULT_ICON
from shared_page import common_page_config
from maximum_roster_strategy import data_loader


MINIMUM_WEEK = 6
MAXIMUM_WEEK = 6

MIN_TIER = 1
MAX_TIER = 4

POSITION_OPTIONS = ["RB", "WR", "TE", "QB"]

POSITION_ABBR_FULL_NAME_MAP = {
    "RB": "Running Backs",
    "WR": "Wide Receivers",
    "TE": "Tight Ends",
    "QB": "Quarterbacks (Superflex / 2QB Leagues Only)",
}


@st.cache_data(ttl=60 * 60 * 24)
def load_data():
    return data_loader.get_google_sheet_data()


def get_player_container(player_series: pd.Series):
    with st.expander(label=player_series["Formatted"]):
        st.write(player_series["Hold Condition"])
        if isinstance(notes := player_series["Article Notes"], str):
            st.write(notes)


# https://github.com/alanjones2/alan-jones-article-code/blob/master/stgrid/app/gridapp.py
def make_grid(cols, rows):
    grid = [0] * cols
    for i in range(cols):
        with st.container():
            grid[i] = st.columns(rows[i])
    return grid


def get_position_breakdown(df: pd.DataFrame, position_abbr: str, position_full_str: str):
    with st.container():
        st.header(position_full_str)
        df_pos = df[df["Position"] == position_abbr]
        time_slots = df.sort_values("WeekTimeSlotIndex")["TimeSlotName"].unique().tolist()
        tier_list = list(range(MIN_TIER, MAX_TIER + 1))
        num_time_slots = len(time_slots)
        column_len_list = [num_time_slots] + [1, num_time_slots] * len(tier_list)
        tier_time_grid = make_grid(len(tier_list) * 2 + 1, column_len_list)

        for time_idx, time_slot in enumerate(time_slots):
            with tier_time_grid[0][time_idx]:
                st.markdown(f"<h3 style='text-align: center;'>{time_slot}</h3>", unsafe_allow_html=True)
            for tier_idx, tier in enumerate(tier_list):
                if time_idx == 0:
                    with tier_time_grid[(tier_idx + 1) * 2 - 1][0]:
                        st.markdown(f"<h4 style='text-align: center;'>Tier {tier}</h4>", unsafe_allow_html=True)
                df_tier_slot = df_pos[(df_pos["TimeSlotName"] == time_slot) & (df_pos["Tier"] == tier)]
                with tier_time_grid[(tier_idx + 1) * 2][time_idx]:
                    df_tier_slot.apply(get_player_container, axis=1)


def get_page():
    page_title = "Maximum Roster Strategy"
    st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
    common_page_config()
    st.title(page_title)
    col_select, week_select = st.columns(2, gap="small")
    with col_select:
        position = st.selectbox(label="Position", options=POSITION_OPTIONS, index=0)
    with week_select:
        week = st.selectbox(label="Week", options=list(range(MAXIMUM_WEEK, MINIMUM_WEEK - 1, -1)), index=0)
    df_mrs = load_data()
    df_mrs = df_mrs[df_mrs["Week"] == week]

    get_position_breakdown(df_mrs, position, POSITION_ABBR_FULL_NAME_MAP[position])


if __name__ == "__main__":
    get_page()