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()