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|
+
import streamlit as st
|
| 2 |
+
st.set_page_config(layout="wide")
|
| 3 |
+
|
| 4 |
+
for name in dir():
|
| 5 |
+
if not name.startswith('_'):
|
| 6 |
+
del globals()[name]
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import streamlit as st
|
| 11 |
+
import gspread
|
| 12 |
+
import gc
|
| 13 |
+
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def init_conn():
|
| 16 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
| 17 |
+
|
| 18 |
+
credentials = {
|
| 19 |
+
"type": "service_account",
|
| 20 |
+
"project_id": "model-sheets-connect",
|
| 21 |
+
"private_key_id": "0e0bc2fdef04e771172fe5807392b9d6639d945e",
|
| 22 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDiu1v/e6KBKOcK\ncx0KQ23nZK3ZVvADYy8u/RUn/EDI82QKxTd/DizRLIV81JiNQxDJXSzgkbwKYEDm\n48E8zGvupU8+Nk76xNPakrQKy2Y8+VJlq5psBtGchJTuUSHcXU5Mg2JhQsB376PJ\nsCw552K6Pw8fpeMDJDZuxpKSkaJR6k9G5Dhf5q8HDXnC5Rh/PRFuKJ2GGRpX7n+2\nhT/sCax0J8jfdTy/MDGiDfJqfQrOPrMKELtsGHR9Iv6F4vKiDqXpKfqH+02E9ptz\nBk+MNcbZ3m90M8ShfRu28ebebsASfarNMzc3dk7tb3utHOGXKCf4tF8yYKo7x8BZ\noO9X4gSfAgMBAAECggEAU8ByyMpSKlTCF32TJhXnVJi/kS+IhC/Qn5JUDMuk4LXr\naAEWsWO6kV/ZRVXArjmuSzuUVrXumISapM9Ps5Ytbl95CJmGDiLDwRL815nvv6k3\nUyAS8EGKjz74RpoIoH6E7EWCAzxlnUgTn+5oP9Flije97epYk3H+e2f1f5e1Nn1d\nYNe8U+1HqJ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VATE KEY-----\n",
|
| 23 |
+
"client_email": "gspread-connection@model-sheets-connect.iam.gserviceaccount.com",
|
| 24 |
+
"client_id": "100369174533302798535",
|
| 25 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 26 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
| 27 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 28 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40model-sheets-connect.iam.gserviceaccount.com"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
gc_con = gspread.service_account_from_dict(credentials, scope)
|
| 32 |
+
|
| 33 |
+
return gc_con
|
| 34 |
+
|
| 35 |
+
gcservice_account = init_conn()
|
| 36 |
+
|
| 37 |
+
NBA_Data = 'https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqrYE6Yrlbb_wEkmH-ot0/edit#gid=1808117109'
|
| 38 |
+
|
| 39 |
+
@st.cache_resource(ttl = 600)
|
| 40 |
+
def init_baselines():
|
| 41 |
+
sh = gcservice_account.open_by_url(NBA_Data)
|
| 42 |
+
|
| 43 |
+
worksheet = sh.worksheet('Gamelog')
|
| 44 |
+
raw_display = pd.DataFrame(worksheet.get_values())
|
| 45 |
+
raw_display.columns = raw_display.iloc[0]
|
| 46 |
+
raw_display = raw_display[1:]
|
| 47 |
+
raw_display = raw_display.reset_index(drop=True)
|
| 48 |
+
gamelog_table = raw_display[raw_display['PLAYER_NAME'] != ""]
|
| 49 |
+
gamelog_table = gamelog_table[['PLAYER_NAME', 'TEAM_NAME', 'SEASON_ID', 'GAME_DATE', 'MATCHUP', 'MIN', 'touches', 'PTS', 'FGM', 'FGA', 'FG_PCT', 'FG3M', 'FG3A',
|
| 50 |
+
'FG3_PCT', 'FTM', 'FTA', 'FT_PCT', 'reboundChancesOffensive', 'OREB', 'reboundChancesDefensive', 'DREB', 'reboundChancesTotal', 'REB',
|
| 51 |
+
'passes', 'secondaryAssists', 'freeThrowAssists', 'assists', 'STL', 'BLK', 'TOV', 'PF', 'DD', 'TD', 'Fantasy', 'FD_Fantasy']]
|
| 52 |
+
gamelog_table['assists'].replace("", 0, inplace=True)
|
| 53 |
+
gamelog_table['reboundChancesTotal'].replace("", 0, inplace=True)
|
| 54 |
+
gamelog_table['passes'].replace("", 0, inplace=True)
|
| 55 |
+
gamelog_table['touches'].replace("", 0, inplace=True)
|
| 56 |
+
gamelog_table['Fantasy'].replace("", 0, inplace=True)
|
| 57 |
+
gamelog_table['FD_Fantasy'].replace("", 0, inplace=True)
|
| 58 |
+
gamelog_table['REB'] = gamelog_table['REB'].astype(int)
|
| 59 |
+
gamelog_table['assists'] = gamelog_table['assists'].astype(int)
|
| 60 |
+
gamelog_table['reboundChancesTotal'] = gamelog_table['reboundChancesTotal'].astype(int)
|
| 61 |
+
gamelog_table['passes'] = gamelog_table['passes'].astype(int)
|
| 62 |
+
gamelog_table['touches'] = gamelog_table['touches'].astype(int)
|
| 63 |
+
gamelog_table['Fantasy'] = gamelog_table['Fantasy'].astype(float)
|
| 64 |
+
gamelog_table['FD_Fantasy'] = gamelog_table['FD_Fantasy'].astype(float)
|
| 65 |
+
gamelog_table['rebound%'] = gamelog_table['REB'] / gamelog_table['reboundChancesTotal']
|
| 66 |
+
gamelog_table['assists_per_pass'] = gamelog_table['assists'] / gamelog_table['passes']
|
| 67 |
+
gamelog_table['Fantasy_per_touch'] = gamelog_table['Fantasy'] / gamelog_table['touches']
|
| 68 |
+
gamelog_table['FD_Fantasy_per_touch'] = gamelog_table['FD_Fantasy'] / gamelog_table['touches']
|
| 69 |
+
data_cols = gamelog_table.columns.drop(['PLAYER_NAME', 'TEAM_NAME', 'SEASON_ID', 'GAME_DATE', 'MATCHUP'])
|
| 70 |
+
gamelog_table[data_cols] = gamelog_table[data_cols].apply(pd.to_numeric, errors='coerce')
|
| 71 |
+
gamelog_table['GAME_DATE'] = pd.to_datetime(gamelog_table['GAME_DATE']).dt.date
|
| 72 |
+
|
| 73 |
+
gamelog_table = gamelog_table.set_axis(['Player', 'Team', 'Season', 'Date', 'Matchup', 'Min', 'Touches', 'Pts', 'FGM', 'FGA', 'FG%', 'FG3M', 'FG3A',
|
| 74 |
+
'FG3%', 'FTM', 'FTA', 'FT%', 'OREB Chance', 'OREB', 'DREB Chance', 'DREB', 'REB Chance', 'REB',
|
| 75 |
+
'Passes', 'Alt Assists', 'FT Assists', 'Assists', 'Stl', 'Blk', 'Tov', 'PF', 'DD', 'TD', 'Fantasy', 'FD_Fantasy',
|
| 76 |
+
'Rebound%', 'Assists/Pass', 'Fantasy/Touch', 'FD Fantasy/Touch'], axis=1)
|
| 77 |
+
|
| 78 |
+
return gamelog_table
|
| 79 |
+
|
| 80 |
+
@st.cache_data(show_spinner=False)
|
| 81 |
+
def seasonlong_build(data_sample):
|
| 82 |
+
season_long_table = data_sample[['Player', 'Team']]
|
| 83 |
+
season_long_table['Min'] = data_sample.groupby(['Player', 'Season'], sort=False)['Min'].transform('mean').astype(float)
|
| 84 |
+
season_long_table['Touches'] = data_sample.groupby(['Player', 'Season'], sort=False)['Touches'].transform('mean').astype(float)
|
| 85 |
+
season_long_table['Pts'] = data_sample.groupby(['Player', 'Season'], sort=False)['Pts'].transform('mean').astype(float)
|
| 86 |
+
season_long_table['FGM'] = data_sample.groupby(['Player', 'Season'], sort=False)['FGM'].transform('mean').astype(float)
|
| 87 |
+
season_long_table['FGA'] = data_sample.groupby(['Player', 'Season'], sort=False)['FGA'].transform('mean').astype(float)
|
| 88 |
+
season_long_table['FG%'] = (data_sample.groupby(['Player', 'Season'], sort=False)['FGM'].transform('sum').astype(int) /
|
| 89 |
+
data_sample.groupby(['Player', 'Season'], sort=False)['FGA'].transform('sum').astype(int))
|
| 90 |
+
season_long_table['FG3M'] = data_sample.groupby(['Player', 'Season'], sort=False)['FG3M'].transform('mean').astype(float)
|
| 91 |
+
season_long_table['FG3A'] = data_sample.groupby(['Player', 'Season'], sort=False)['FG3A'].transform('mean').astype(float)
|
| 92 |
+
season_long_table['FG3%'] = (data_sample.groupby(['Player', 'Season'], sort=False)['FG3M'].transform('sum').astype(int) /
|
| 93 |
+
data_sample.groupby(['Player', 'Season'], sort=False)['FG3A'].transform('sum').astype(int))
|
| 94 |
+
season_long_table['FTM'] = data_sample.groupby(['Player', 'Season'], sort=False)['FTM'].transform('mean').astype(float)
|
| 95 |
+
season_long_table['FTA'] = data_sample.groupby(['Player', 'Season'], sort=False)['FTA'].transform('mean').astype(float)
|
| 96 |
+
season_long_table['FT%'] = (data_sample.groupby(['Player', 'Season'], sort=False)['FTM'].transform('sum').astype(int) /
|
| 97 |
+
data_sample.groupby(['Player', 'Season'], sort=False)['FTA'].transform('sum').astype(int))
|
| 98 |
+
season_long_table['OREB Chance'] = data_sample.groupby(['Player', 'Season'], sort=False)['OREB Chance'].transform('mean').astype(float)
|
| 99 |
+
season_long_table['OREB'] = data_sample.groupby(['Player', 'Season'], sort=False)['OREB'].transform('mean').astype(float)
|
| 100 |
+
season_long_table['DREB Chance'] = data_sample.groupby(['Player', 'Season'], sort=False)['DREB Chance'].transform('mean').astype(float)
|
| 101 |
+
season_long_table['DREB'] = data_sample.groupby(['Player', 'Season'], sort=False)['DREB'].transform('mean').astype(float)
|
| 102 |
+
season_long_table['REB Chance'] = data_sample.groupby(['Player', 'Season'], sort=False)['REB Chance'].transform('mean').astype(float)
|
| 103 |
+
season_long_table['REB'] = data_sample.groupby(['Player', 'Season'], sort=False)['REB'].transform('mean').astype(float)
|
| 104 |
+
season_long_table['Passes'] = data_sample.groupby(['Player', 'Season'], sort=False)['Passes'].transform('mean').astype(float)
|
| 105 |
+
season_long_table['Alt Assists'] = data_sample.groupby(['Player', 'Season'], sort=False)['Alt Assists'].transform('mean').astype(float)
|
| 106 |
+
season_long_table['FT Assists'] = data_sample.groupby(['Player', 'Season'], sort=False)['FT Assists'].transform('mean').astype(float)
|
| 107 |
+
season_long_table['Assists'] = data_sample.groupby(['Player', 'Season'], sort=False)['Assists'].transform('mean').astype(float)
|
| 108 |
+
season_long_table['Stl'] = data_sample.groupby(['Player', 'Season'], sort=False)['Stl'].transform('mean').astype(float)
|
| 109 |
+
season_long_table['Blk'] = data_sample.groupby(['Player', 'Season'], sort=False)['Blk'].transform('mean').astype(float)
|
| 110 |
+
season_long_table['Tov'] = data_sample.groupby(['Player', 'Season'], sort=False)['Tov'].transform('mean').astype(float)
|
| 111 |
+
season_long_table['PF'] = data_sample.groupby(['Player', 'Season'], sort=False)['PF'].transform('mean').astype(float)
|
| 112 |
+
season_long_table['DD'] = data_sample.groupby(['Player', 'Season'], sort=False)['DD'].transform('mean').astype(float)
|
| 113 |
+
season_long_table['TD'] = data_sample.groupby(['Player', 'Season'], sort=False)['TD'].transform('mean').astype(float)
|
| 114 |
+
season_long_table['Fantasy'] = data_sample.groupby(['Player', 'Season'], sort=False)['Fantasy'].transform('mean').astype(float)
|
| 115 |
+
season_long_table['FD_Fantasy'] = data_sample.groupby(['Player', 'Season'], sort=False)['FD_Fantasy'].transform('mean').astype(float)
|
| 116 |
+
season_long_table['Rebound%'] = (data_sample.groupby(['Player', 'Season'], sort=False)['REB'].transform('sum').astype(int) /
|
| 117 |
+
data_sample.groupby(['Player', 'Season'], sort=False)['REB Chance'].transform('sum').astype(int))
|
| 118 |
+
season_long_table['Assists/Pass'] = (data_sample.groupby(['Player', 'Season'], sort=False)['Assists'].transform('sum').astype(int) /
|
| 119 |
+
data_sample.groupby(['Player', 'Season'], sort=False)['Passes'].transform('sum').astype(int))
|
| 120 |
+
season_long_table['Fantasy/Touch'] = (data_sample.groupby(['Player', 'Season'], sort=False)['Fantasy'].transform('sum').astype(int) /
|
| 121 |
+
data_sample.groupby(['Player', 'Season'], sort=False)['Touches'].transform('sum').astype(int))
|
| 122 |
+
season_long_table['FD Fantasy/Touch'] = (data_sample.groupby(['Player', 'Season'], sort=False)['FD_Fantasy'].transform('sum').astype(int) /
|
| 123 |
+
data_sample.groupby(['Player', 'Season'], sort=False)['Touches'].transform('sum').astype(int))
|
| 124 |
+
season_long_table = season_long_table.drop_duplicates(subset='Player')
|
| 125 |
+
|
| 126 |
+
season_long_table = season_long_table.set_axis(['Player', 'Team', 'Min', 'Touches', 'Pts', 'FGM', 'FGA', 'FG%', 'FG3M', 'FG3A',
|
| 127 |
+
'FG3%', 'FTM', 'FTA', 'FT%', 'OREB Chance', 'OREB', 'DREB Chance', 'DREB', 'REB Chance', 'REB',
|
| 128 |
+
'Passes', 'Alt Assists', 'FT Assists', 'Assists', 'Stl', 'Blk', 'Tov', 'PF', 'DD', 'TD', 'Fantasy', 'FD_Fantasy',
|
| 129 |
+
'Rebound%', 'Assists/Pass', 'Fantasy/Touch', 'FD Fantasy/Touch'], axis=1)
|
| 130 |
+
|
| 131 |
+
return season_long_table
|
| 132 |
+
|
| 133 |
+
@st.cache_data(show_spinner=False)
|
| 134 |
+
def run_fantasy_corr(data_sample):
|
| 135 |
+
cor_testing = data_sample
|
| 136 |
+
cor_testing = cor_testing[cor_testing['Season'] == '22023']
|
| 137 |
+
date_list = cor_testing['Date'].unique().tolist()
|
| 138 |
+
player_list = cor_testing['Player'].unique().tolist()
|
| 139 |
+
corr_frame = pd.DataFrame()
|
| 140 |
+
corr_frame['DATE'] = date_list
|
| 141 |
+
for player in player_list:
|
| 142 |
+
player_testing = cor_testing[cor_testing['Player'] == player]
|
| 143 |
+
fantasy_map = dict(zip(player_testing['Date'], player_testing['Fantasy']))
|
| 144 |
+
corr_frame[player] = corr_frame['DATE'].map(fantasy_map)
|
| 145 |
+
players_fantasy = corr_frame.drop('DATE', axis=1)
|
| 146 |
+
corrM = players_fantasy.corr()
|
| 147 |
+
|
| 148 |
+
return corrM
|
| 149 |
+
|
| 150 |
+
@st.cache_data(show_spinner=False)
|
| 151 |
+
def run_min_corr(data_sample):
|
| 152 |
+
cor_testing = data_sample
|
| 153 |
+
cor_testing = cor_testing[cor_testing['Season'] == '22023']
|
| 154 |
+
date_list = cor_testing['Date'].unique().tolist()
|
| 155 |
+
player_list = cor_testing['Player'].unique().tolist()
|
| 156 |
+
corr_frame = pd.DataFrame()
|
| 157 |
+
corr_frame['DATE'] = date_list
|
| 158 |
+
for player in player_list:
|
| 159 |
+
player_testing = cor_testing[cor_testing['Player'] == player]
|
| 160 |
+
fantasy_map = dict(zip(player_testing['Date'], player_testing['Min']))
|
| 161 |
+
corr_frame[player] = corr_frame['DATE'].map(fantasy_map)
|
| 162 |
+
players_fantasy = corr_frame.drop('DATE', axis=1)
|
| 163 |
+
corrM = players_fantasy.corr()
|
| 164 |
+
|
| 165 |
+
return corrM
|
| 166 |
+
|
| 167 |
+
@st.cache_data(show_spinner=False)
|
| 168 |
+
def split_frame(input_df, rows):
|
| 169 |
+
df = [input_df.loc[i : i + rows - 1, :] for i in range(0, len(input_df), rows)]
|
| 170 |
+
return df
|
| 171 |
+
|
| 172 |
+
def convert_df_to_csv(df):
|
| 173 |
+
return df.to_csv().encode('utf-8')
|
| 174 |
+
|
| 175 |
+
gamelog_table = init_baselines()
|
| 176 |
+
indv_teams = gamelog_table.drop_duplicates(subset='Team')
|
| 177 |
+
total_teams = indv_teams.Team.values.tolist()
|
| 178 |
+
indv_players = gamelog_table.drop_duplicates(subset='Player')
|
| 179 |
+
total_players = indv_players.Player.values.tolist()
|
| 180 |
+
total_dates = gamelog_table.Date.values.tolist()
|
| 181 |
+
|
| 182 |
+
tab1, tab2 = st.tabs(['Gamelogs', 'Correlation Matrix'])
|
| 183 |
+
|
| 184 |
+
with tab1:
|
| 185 |
+
col1, col2 = st.columns([1, 9])
|
| 186 |
+
with col1:
|
| 187 |
+
if st.button("Reset Data", key='reset1'):
|
| 188 |
+
st.cache_data.clear()
|
| 189 |
+
gamelog_table = init_baselines()
|
| 190 |
+
indv_teams = gamelog_table.drop_duplicates(subset='Team')
|
| 191 |
+
total_teams = indv_teams.Team.values.tolist()
|
| 192 |
+
indv_players = gamelog_table.drop_duplicates(subset='Player')
|
| 193 |
+
total_players = indv_players.Player.values.tolist()
|
| 194 |
+
total_dates = gamelog_table.Date.values.tolist()
|
| 195 |
+
|
| 196 |
+
split_var1 = st.radio("What table would you like to view?", ('Season Logs', 'Gamelogs'), key='split_var1')
|
| 197 |
+
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
|
| 198 |
+
|
| 199 |
+
if split_var2 == 'Specific Teams':
|
| 200 |
+
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = total_teams, key='team_var1')
|
| 201 |
+
elif split_var2 == 'All':
|
| 202 |
+
team_var1 = total_teams
|
| 203 |
+
|
| 204 |
+
split_var3 = st.radio("Would you like to view all dates or specific ones?", ('All', 'Specific Dates'), key='split_var3')
|
| 205 |
+
|
| 206 |
+
if split_var3 == 'Specific Dates':
|
| 207 |
+
low_date = st.date_input('Min Date:', value=None, format="YYYY-MM-DD", key='low_date')
|
| 208 |
+
if low_date is not None:
|
| 209 |
+
low_date = pd.to_datetime(low_date).date()
|
| 210 |
+
high_date = st.date_input('Max Date:', value=None, format="YYYY-MM-DD", key='high_date')
|
| 211 |
+
if high_date is not None:
|
| 212 |
+
high_date = pd.to_datetime(high_date).date()
|
| 213 |
+
elif split_var3 == 'All':
|
| 214 |
+
low_date = gamelog_table['Date'].min()
|
| 215 |
+
high_date = gamelog_table['Date'].max()
|
| 216 |
+
|
| 217 |
+
split_var4 = st.radio("Would you like to view all players or specific ones?", ('All', 'Specific Players'), key='split_var4')
|
| 218 |
+
|
| 219 |
+
if split_var4 == 'Specific Players':
|
| 220 |
+
player_var1 = st.multiselect('Which players would you like to include in the tables?', options = total_players, key='player_var1')
|
| 221 |
+
elif split_var4 == 'All':
|
| 222 |
+
player_var1 = total_players
|
| 223 |
+
|
| 224 |
+
min_var1 = st.slider("Is there a certain minutes range you want to view?", 0, 60, (0, 60), key='min_var1')
|
| 225 |
+
|
| 226 |
+
with col2:
|
| 227 |
+
if split_var1 == 'Season Logs':
|
| 228 |
+
display = st.container()
|
| 229 |
+
gamelog_table = gamelog_table[gamelog_table['Date'] >= low_date]
|
| 230 |
+
gamelog_table = gamelog_table[gamelog_table['Date'] <= high_date]
|
| 231 |
+
gamelog_table = gamelog_table[gamelog_table['Min'] >= min_var1[0]]
|
| 232 |
+
gamelog_table = gamelog_table[gamelog_table['Min'] <= min_var1[1]]
|
| 233 |
+
gamelog_table = gamelog_table[gamelog_table['Team'].isin(team_var1)]
|
| 234 |
+
gamelog_table = gamelog_table[gamelog_table['Player'].isin(player_var1)]
|
| 235 |
+
season_long_table = seasonlong_build(gamelog_table)
|
| 236 |
+
season_long_table = season_long_table.set_index('Player')
|
| 237 |
+
display.dataframe(season_long_table.style.format(precision=2), height=750, use_container_width = True)
|
| 238 |
+
|
| 239 |
+
elif split_var1 == 'Gamelogs':
|
| 240 |
+
gamelog_table = gamelog_table[gamelog_table['Date'] >= low_date]
|
| 241 |
+
gamelog_table = gamelog_table[gamelog_table['Date'] <= high_date]
|
| 242 |
+
gamelog_table = gamelog_table[gamelog_table['Min'] >= min_var1[0]]
|
| 243 |
+
gamelog_table = gamelog_table[gamelog_table['Min'] <= min_var1[1]]
|
| 244 |
+
gamelog_table = gamelog_table[gamelog_table['Team'].isin(team_var1)]
|
| 245 |
+
gamelog_table = gamelog_table[gamelog_table['Player'].isin(player_var1)]
|
| 246 |
+
gamelog_table = gamelog_table.reset_index(drop=True)
|
| 247 |
+
display = st.container()
|
| 248 |
+
|
| 249 |
+
bottom_menu = st.columns((4, 1, 1))
|
| 250 |
+
with bottom_menu[2]:
|
| 251 |
+
batch_size = st.selectbox("Page Size", options=[25, 50, 100])
|
| 252 |
+
with bottom_menu[1]:
|
| 253 |
+
total_pages = (
|
| 254 |
+
int(len(gamelog_table) / batch_size) if int(len(gamelog_table) / batch_size) > 0 else 1
|
| 255 |
+
)
|
| 256 |
+
current_page = st.number_input(
|
| 257 |
+
"Page", min_value=1, max_value=total_pages, step=1
|
| 258 |
+
)
|
| 259 |
+
with bottom_menu[0]:
|
| 260 |
+
st.markdown(f"Page **{current_page}** of **{total_pages}** ")
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
pages = split_frame(gamelog_table, batch_size)
|
| 264 |
+
# pages = pages.set_index('Player')
|
| 265 |
+
display.dataframe(data=pages[current_page - 1].style.format(precision=2), height=500, use_container_width=True)
|
| 266 |
+
|
| 267 |
+
with tab2:
|
| 268 |
+
col1, col2 = st.columns([1, 9])
|
| 269 |
+
with col1:
|
| 270 |
+
if st.button("Reset Data", key='reset2'):
|
| 271 |
+
st.cache_data.clear()
|
| 272 |
+
gamelog_table = init_baselines()
|
| 273 |
+
indv_teams = gamelog_table.drop_duplicates(subset='Team')
|
| 274 |
+
total_teams = indv_teams.Team.values.tolist()
|
| 275 |
+
indv_players = gamelog_table.drop_duplicates(subset='Player')
|
| 276 |
+
total_players = indv_players.Player.values.tolist()
|
| 277 |
+
total_dates = gamelog_table.Date.values.tolist()
|
| 278 |
+
|
| 279 |
+
corr_var = st.radio("Are you correlating fantasy or minutes?", ('Fantasy', 'Minutes'), key='corr_var')
|
| 280 |
+
|
| 281 |
+
split_var1_t2 = st.radio("Would you like to view specific teams or specific players?", ('Specific Teams', 'Specific Players'), key='split_var1_t2')
|
| 282 |
+
|
| 283 |
+
if split_var1_t2 == 'Specific Teams':
|
| 284 |
+
corr_var1_t2 = st.multiselect('Which teams would you like to include in the correlation?', options = total_teams, key='corr_var1_t2')
|
| 285 |
+
elif split_var1_t2 == 'Specific Players':
|
| 286 |
+
corr_var1_t2 = st.multiselect('Which players would you like to include in the correlation?', options = total_players, key='corr_var1_t2')
|
| 287 |
+
|
| 288 |
+
split_var2_t2 = st.radio("Would you like to view all dates or specific ones?", ('All', 'Specific Dates'), key='split_var3_t2')
|
| 289 |
+
|
| 290 |
+
if split_var2_t2 == 'Specific Dates':
|
| 291 |
+
low_date_t2 = st.date_input('Min Date:', value=None, format="YYYY-MM-DD", key='low_date_t2')
|
| 292 |
+
if low_date_t2 is not None:
|
| 293 |
+
low_date_t2 = pd.to_datetime(low_date_t2).date()
|
| 294 |
+
high_date_t2 = st.date_input('Max Date:', value=None, format="YYYY-MM-DD", key='high_date_t2')
|
| 295 |
+
if high_date_t2 is not None:
|
| 296 |
+
high_date_t2 = pd.to_datetime(high_date_t2).date()
|
| 297 |
+
elif split_var2_t2 == 'All':
|
| 298 |
+
low_date_t2 = gamelog_table['Date'].min()
|
| 299 |
+
high_date_t2 = gamelog_table['Date'].max()
|
| 300 |
+
|
| 301 |
+
min_var1_t2 = st.slider("Is there a certain minutes range you want to view?", 0, 60, (0, 60), key='min_var1_t2')
|
| 302 |
+
|
| 303 |
+
with col2:
|
| 304 |
+
if split_var1_t2 == 'Specific Teams':
|
| 305 |
+
display = st.container()
|
| 306 |
+
gamelog_table = gamelog_table.sort_values(by='Fantasy', ascending=False)
|
| 307 |
+
gamelog_table = gamelog_table[gamelog_table['Date'] >= low_date_t2]
|
| 308 |
+
gamelog_table = gamelog_table[gamelog_table['Date'] <= high_date_t2]
|
| 309 |
+
gamelog_table = gamelog_table[gamelog_table['Min'] >= min_var1_t2[0]]
|
| 310 |
+
gamelog_table = gamelog_table[gamelog_table['Min'] <= min_var1_t2[1]]
|
| 311 |
+
gamelog_table = gamelog_table[gamelog_table['Team'].isin(corr_var1_t2)]
|
| 312 |
+
if corr_var == 'Fantasy':
|
| 313 |
+
corr_display = run_fantasy_corr(gamelog_table)
|
| 314 |
+
elif corr_var == 'Minutes':
|
| 315 |
+
corr_display = run_min_corr(gamelog_table)
|
| 316 |
+
display.dataframe(corr_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height=1000, use_container_width = True)
|
| 317 |
+
|
| 318 |
+
elif split_var1_t2 == 'Specific Players':
|
| 319 |
+
display = st.container()
|
| 320 |
+
gamelog_table = gamelog_table.sort_values(by='Fantasy', ascending=False)
|
| 321 |
+
gamelog_table = gamelog_table[gamelog_table['Date'] >= low_date_t2]
|
| 322 |
+
gamelog_table = gamelog_table[gamelog_table['Date'] <= high_date_t2]
|
| 323 |
+
gamelog_table = gamelog_table[gamelog_table['Min'] >= min_var1_t2[0]]
|
| 324 |
+
gamelog_table = gamelog_table[gamelog_table['Min'] <= min_var1_t2[1]]
|
| 325 |
+
gamelog_table = gamelog_table[gamelog_table['Player'].isin(corr_var1_t2)]
|
| 326 |
+
if corr_var == 'Fantasy':
|
| 327 |
+
corr_display = run_fantasy_corr(gamelog_table)
|
| 328 |
+
elif corr_var == 'Minutes':
|
| 329 |
+
corr_display = run_min_corr(gamelog_table)
|
| 330 |
+
display.dataframe(corr_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|