File size: 2,449 Bytes
78f5251
 
29e969c
78f5251
 
 
 
 
 
 
 
 
 
29e969c
6e3dcdf
29e969c
6e3dcdf
29e969c
e8bc890
29e969c
 
78f5251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e3dcdf
 
29e969c
 
09ddcdd
 
 
 
2988866
 
 
 
29e969c
 
2988866
 
 
 
b71783b
e8bc890
 
 
78f5251
 
 
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
import streamlit as st

from analyze_yahoo import get_grouped_luck, calculate_luck
from config import DEFAULT_ICON, SEASON
from login_component import is_token_in_session
from shared_page import common_page_config


@st.cache_resource(ttl=60 * 60 * 24)
def get_all_league_settings_with_cache(season: int):
    return st.session_state.yahoo_client.get_all_logged_in_user_league_settings(season=season)


@st.cache_resource(ttl=60 * 10)
def get_cached_league_luck(league_key, include_current: bool):
    df_weekly_results = st.session_state.yahoo_client.full_schedule_dataframe(league_key)
    df_weekly_luck = calculate_luck(df_weekly_results, include_current=include_current)
    df_grouped_luck = get_grouped_luck(df_weekly_luck)
    return df_weekly_results, df_weekly_luck, df_grouped_luck


def get_page():
    page_title = "Yahoo League Scoring Summary"
    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:
        selected_season = st.selectbox("Select Season", list(range(SEASON, 2012, -1)))
        user_leagues = get_all_league_settings_with_cache(season=selected_season)
        selected_league = st.selectbox("Select league", user_leagues, format_func=lambda x: x.name)
        st.header(f"{selected_league.name} - {selected_league.season}")

        check_current = st.checkbox("Include week in progress")
        df_weekly_results, df_weekly_luck, df_luck = get_cached_league_luck(selected_league.league_key, check_current)

        st.header("Weekly Top Half Scores")
        luck_pivot = df_weekly_luck.pivot_table(
            columns="week", index="team_name", values="half_wins", aggfunc="sum", margins=True, margins_name="Total"
        )
        luck_pivot.columns = [str(int(x)) if isinstance(x, float) else x for x in luck_pivot.columns]
        st.dataframe(
            luck_pivot,
            height=35 * (len(luck_pivot) + 1) + 3,
        )

        st.header("Luck Summary")
        st.dataframe(
            df_luck,
            height=35 * (len(df_luck) + 1) + 3,
        )

        if st.checkbox("View/Export Full Scores"):
            st.dataframe(df_weekly_results)


if __name__ == "__main__":
    get_page()