import streamlit as st st.set_page_config(layout="wide") for name in dir(): if not name.startswith('_'): del globals()[name] import numpy as np import pandas as pd import streamlit as st import gspread from database import gcservice_account, NHL_data percentages_format = {'Shots': '{:.2%}', 'HDCF': '{:.2%}', 'Goals': '{:.2%}', 'Assists': '{:.2%}', 'Blocks': '{:.2%}', 'L14_Shots': '{:.2%}', 'L14_HDCF': '{:.2%}', 'L14_Goals': '{:.2%}', 'L14_Assists': '{:.2%}', 'L14_Blocks': '{:.2%}', 'Max Goal%': '{:.2%}', 'L14 Max Goal%': '{:.2%}'} matchups_format = {'HDCF%': '{:.2%}', 'o_HDCA%': '{:.2%}', 'HDCF_m%': '{:.2%}'} @st.cache_resource(ttl = 599) def init_baselines(): parse_hold = pd.DataFrame(columns=['Line', 'SK1', 'SK2', 'SK3', 'Cost', 'Team Total', 'Shots', 'HDCF', 'Goals', 'Assists', 'Blocks', 'L14_Shots', 'L14_HDCF', 'L14_Goals', 'L14_Assists', 'L14_Blocks', 'Max Goal%']) sh = gcservice_account.open_by_url(NHL_data) worksheet = sh.worksheet('Player_Level_ROO') raw_display = pd.DataFrame(worksheet.get_values()) raw_display.columns = raw_display.iloc[0] raw_display = raw_display[1:] raw_display = raw_display.reset_index(drop=True) raw_display = raw_display[raw_display['Opp'] != ""] team_frame = raw_display[['Team', 'Opp']] team_list = team_frame['Team'].unique() team_dict = dict(zip(team_frame['Team'], team_frame['Opp'])) worksheet = sh.worksheet('Matchups') raw_display = pd.DataFrame(worksheet.get_values()) raw_display.columns = raw_display.iloc[0] raw_display = raw_display[1:] raw_display = raw_display.reset_index(drop=True) raw_display = raw_display[raw_display['Opp'] != ""] matchups = raw_display[['Team', 'Opp', 'FL1$', 'FL2$', 'FL3$', 'Team Total', 'Game Pace', 'SF', 'o_SA', 'SF_m', 'HDCF', 'o_HDCA', 'HDCF_m', 'HDCF%', 'o_HDCA%', 'HDCF_m%', 'HDSF+']] data_cols = matchups.columns.drop(['Team', 'Opp']) matchups[data_cols] = matchups[data_cols].apply(pd.to_numeric, errors='coerce') matchups = matchups.dropna(subset='HDSF+') matchups = matchups.sort_values(by='HDCF_m', ascending=False) worksheet = sh.worksheet('Marketshares') raw_display = pd.DataFrame(worksheet.get_values()) raw_display.columns = raw_display.iloc[0] raw_display = raw_display[1:] raw_display = raw_display.reset_index(drop=True) # raw_display = raw_display[raw_display['Line'] != ""] overall_ms = raw_display[['Line', 'SK1', 'SK2', 'SK3', 'Cost', 'Team Total', 'Shots', 'HDCF', 'Goals', 'Assists', 'Blocks', 'L14_Shots', 'L14_HDCF', 'L14_Goals', 'L14_Assists', 'L14_Blocks']] pat = '|'.join(team_list) s = overall_ms['Line'].str.extract('('+ pat + ')', expand=False) overall_ms['Max Goal%'] = overall_ms.groupby(s)['Goals'].transform('max') overall_ms['L14 Max Goal%'] = overall_ms.groupby(s)['L14_Goals'].transform('max') data_cols = overall_ms.columns.drop(['Line', 'SK1', 'SK2', 'SK3']) overall_ms[data_cols] = overall_ms[data_cols].apply(pd.to_numeric, errors='coerce') overall_ms['Proj Goal'] = overall_ms['Goals'] * overall_ms['Team Total'] overall_ms['L14 Proj Goal'] = overall_ms['L14_Goals'] * overall_ms['Team Total'] overall_ms = overall_ms[['Line', 'SK1', 'SK2', 'SK3', 'Cost', 'Team Total', 'Shots', 'HDCF', 'Goals', 'Max Goal%', 'Proj Goal', 'Assists', 'Blocks', 'L14_Shots', 'L14_HDCF', 'L14_Goals', 'L14 Max Goal%', 'L14 Proj Goal', 'L14_Assists', 'L14_Blocks']] overall_ms = overall_ms.sort_values(by='Shots', ascending=False) return matchups, overall_ms, team_frame, team_list, team_dict def convert_df_to_csv(df): return df.to_csv().encode('utf-8') matchups, overall_ms, team_frame, team_list, team_dict = init_baselines() col1, col2 = st.columns([1, 9]) with col1: if st.button("Reset Data", key='reset1'): st.cache_data.clear() matchups, overall_ms, team_frame, team_list, team_dict = init_baselines() split_var1 = st.radio("View matchups or line marketshares?", ('Slate Matchups', 'Line Marketshares'), key='split_var1') if split_var1 == "Line Marketshares": team_var = st.radio("View all teams or specific teams?", ('All Teams', 'Specific Teams'), key='team_var') if team_var == "All Teams": team_split = team_frame.Team.values.tolist() elif team_var == "Specific Teams": team_split = st.multiselect('Which teams would you like to include in the tables?', options = team_frame['Team'].unique(), key='team_var1') with col2: if split_var1 == 'Slate Matchups': display_table = matchups display_table = display_table.set_index('Team') st.dataframe(display_table.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(matchups_format, precision=2), height=500, use_container_width = True) elif split_var1 == 'Line Marketshares': display_table = overall_ms display_parsed = display_table[display_table['Line'].str.contains('|'.join(team_split))] # display_parsed = display_parsed.set_index('Line') st.dataframe(display_parsed.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(percentages_format, precision=2), height=500, use_container_width = True) if split_var1 == 'Line Marketshares': st.download_button( label="Export Marketshares (CSV)", data=convert_df_to_csv(display_table), file_name='Marketshares_export.csv', mime='text/csv', ) elif split_var1 == 'Slate Matchups': st.download_button( label="Export Matchups (CSV)", data=convert_df_to_csv(display_table), file_name='Matchups_export.csv', mime='text/csv', )