Spaces:
Runtime error
Runtime error
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 | |
import gc | |
def init_conn(): | |
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] | |
credentials = { | |
"type": "service_account", | |
"project_id": "model-sheets-connect", | |
"private_key_id": "0e0bc2fdef04e771172fe5807392b9d6639d945e", | |
"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+1HqJgILcxA1TAUsARBfoD7+K3z/8DVPHI8IpzAh6kTHqhqC23Rram4XoQ6\nzj/ZdVBjvnKuazETfsD+Vl3jGLQA8cKQVV70xdz3xwLcNeHsbPbpGBpZUoF73c65\nkAXOrjYl0JD5yAk+hmYhXr6H9c6z5AieuZGDrhmlFQKBgQDzV6LRXmjn4854DP/J\nI82oX2GcI4eioDZPRukhiQLzYerMQBmyqZIRC+/LTCAhYQSjNgMa+ZKyvLqv48M0\n/x398op/+n3xTs+8L49SPI48/iV+mnH7k0WI/ycd4OOKh8rrmhl/0EWb9iitwJYe\nMjTV/QxNEpPBEXfR1/mvrN/lVQKBgQDuhomOxUhWVRVH6x03slmyRBn0Oiw4MW+r\nrt1hlNgtVmTc5Mu+4G0USMZwYuOB7F8xG4Foc7rIlwS7Ic83jMJxemtqAelwOLdV\nXRLrLWJfX8+O1z/UE15l2q3SUEnQ4esPHbQnZowHLm0mdL14qSVMl1mu1XfsoZ3z\nJZTQb48CIwKBgEWbzQRtKD8lKDupJEYqSrseRbK/ax43DDITS77/DWwHl33D3FYC\nMblUm8ygwxQpR4VUfwDpYXBlklWcJovzamXpSnsfcYVkkQH47NuOXPXPkXQsw+w+\nDYcJzeu7F/vZqk9I7oBkWHUrrik9zPNoUzrfPvSRGtkAoTDSwibhoc5dAoGBAMHE\nK0T/ANeZQLNuzQps6S7G4eqjwz5W8qeeYxsdZkvWThOgDd/ewt3ijMnJm5X05hOn\ni4XF1euTuvUl7wbqYx76Wv3/1ZojiNNgy7ie4rYlyB/6vlBS97F4ZxJdxMlabbCW\n6b3EMWa4EVVXKoA1sCY7IVDE+yoQ1JYsZmq45YzPAoGBANWWHuVueFGZRDZlkNlK\nh5OmySmA0NdNug3G1upaTthyaTZ+CxGliwBqMHAwpkIRPwxUJpUwBTSEGztGTAxs\nWsUOVWlD2/1JaKSmHE8JbNg6sxLilcG6WEDzxjC5dLL1OrGOXj9WhC9KX3sq6qb6\nF/j9eUXfXjAlb042MphoF3ZC\n-----END PRIVATE KEY-----\n", | |
"client_email": "[email protected]", | |
"client_id": "100369174533302798535", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://oauth2.googleapis.com/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40model-sheets-connect.iam.gserviceaccount.com" | |
} | |
gc_con = gspread.service_account_from_dict(credentials, scope) | |
return gc_con | |
gcservice_account = init_conn() | |
NBA_Data = 'https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqrYE6Yrlbb_wEkmH-ot0/edit#gid=1808117109' | |
def init_baselines(): | |
sh = gcservice_account.open_by_url(NBA_Data) | |
worksheet = sh.worksheet('Gamelog') | |
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) | |
gamelog_table = raw_display[raw_display['PLAYER_NAME'] != ""] | |
gamelog_table = gamelog_table[['PLAYER_NAME', 'TEAM_NAME', 'SEASON_ID', 'GAME_DATE', 'MATCHUP', 'MIN', 'touches', 'PTS', 'FGM', 'FGA', 'FG_PCT', 'FG3M', 'FG3A', | |
'FG3_PCT', 'FTM', 'FTA', 'FT_PCT', 'reboundChancesOffensive', 'OREB', 'reboundChancesDefensive', 'DREB', 'reboundChancesTotal', 'REB', | |
'passes', 'secondaryAssists', 'freeThrowAssists', 'assists', 'STL', 'BLK', 'TOV', 'PF', 'DD', 'TD', 'Fantasy', 'FD_Fantasy']] | |
gamelog_table['assists'].replace("", 0, inplace=True) | |
gamelog_table['reboundChancesTotal'].replace("", 0, inplace=True) | |
gamelog_table['passes'].replace("", 0, inplace=True) | |
gamelog_table['touches'].replace("", 0, inplace=True) | |
gamelog_table['Fantasy'].replace("", 0, inplace=True) | |
gamelog_table['FD_Fantasy'].replace("", 0, inplace=True) | |
gamelog_table['REB'] = gamelog_table['REB'].astype(int) | |
gamelog_table['assists'] = gamelog_table['assists'].astype(int) | |
gamelog_table['reboundChancesTotal'] = gamelog_table['reboundChancesTotal'].astype(int) | |
gamelog_table['passes'] = gamelog_table['passes'].astype(int) | |
gamelog_table['touches'] = gamelog_table['touches'].astype(int) | |
gamelog_table['Fantasy'] = gamelog_table['Fantasy'].astype(float) | |
gamelog_table['FD_Fantasy'] = gamelog_table['FD_Fantasy'].astype(float) | |
gamelog_table['rebound%'] = gamelog_table['REB'] / gamelog_table['reboundChancesTotal'] | |
gamelog_table['assists_per_pass'] = gamelog_table['assists'] / gamelog_table['passes'] | |
gamelog_table['Fantasy_per_touch'] = gamelog_table['Fantasy'] / gamelog_table['touches'] | |
gamelog_table['FD_Fantasy_per_touch'] = gamelog_table['FD_Fantasy'] / gamelog_table['touches'] | |
data_cols = gamelog_table.columns.drop(['PLAYER_NAME', 'TEAM_NAME', 'SEASON_ID', 'GAME_DATE', 'MATCHUP']) | |
gamelog_table[data_cols] = gamelog_table[data_cols].apply(pd.to_numeric, errors='coerce') | |
return gamelog_table | |
def seasonlong_build(data_sample): | |
season_long_table = data_sample[['PLAYER_NAME', 'TEAM_NAME']] | |
season_long_table['MIN'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['MIN'].transform('mean').astype(float) | |
season_long_table['touches'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['touches'].transform('mean').astype(float) | |
season_long_table['FGM'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FGM'].transform('mean').astype(float) | |
season_long_table['FGA'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FGA'].transform('mean').astype(float) | |
season_long_table['FG_PCT'] = (data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FGM'].transform('sum').astype(int) / | |
data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FGA'].transform('sum').astype(int)) | |
season_long_table['FG3M'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FG3M'].transform('mean').astype(float) | |
season_long_table['FG3A'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FG3A'].transform('mean').astype(float) | |
season_long_table['FG3_PCT'] = (data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FG3M'].transform('sum').astype(int) / | |
data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FG3A'].transform('sum').astype(int)) | |
season_long_table['FTM'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FTM'].transform('mean').astype(float) | |
season_long_table['FTA'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FTA'].transform('mean').astype(float) | |
season_long_table['FT_PCT'] = (data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FTM'].transform('sum').astype(int) / | |
data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FTA'].transform('sum').astype(int)) | |
season_long_table['reboundChancesOffensive'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['reboundChancesOffensive'].transform('mean').astype(float) | |
season_long_table['OREB'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['OREB'].transform('mean').astype(float) | |
season_long_table['reboundChancesDefensive'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['reboundChancesDefensive'].transform('mean').astype(float) | |
season_long_table['DREB'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['DREB'].transform('mean').astype(float) | |
season_long_table['reboundChancesTotal'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['reboundChancesTotal'].transform('mean').astype(float) | |
season_long_table['REB'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['REB'].transform('mean').astype(float) | |
season_long_table['passes'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['passes'].transform('mean').astype(float) | |
season_long_table['secondaryAssists'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['secondaryAssists'].transform('mean').astype(float) | |
season_long_table['freeThrowAssists'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['freeThrowAssists'].transform('mean').astype(float) | |
season_long_table['assists'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['assists'].transform('mean').astype(float) | |
season_long_table['STL'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['STL'].transform('mean').astype(float) | |
season_long_table['BLK'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['BLK'].transform('mean').astype(float) | |
season_long_table['TOV'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['TOV'].transform('mean').astype(float) | |
season_long_table['PF'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['PF'].transform('mean').astype(float) | |
season_long_table['DD'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['DD'].transform('mean').astype(float) | |
season_long_table['TD'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['TD'].transform('mean').astype(float) | |
season_long_table['Fantasy'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['Fantasy'].transform('mean').astype(float) | |
season_long_table['FD_Fantasy'] = data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FD_Fantasy'].transform('mean').astype(float) | |
season_long_table['rebound%'] = (data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['REB'].transform('sum').astype(int) / | |
data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['reboundChancesTotal'].transform('sum').astype(int)) | |
season_long_table['assists_per_pass'] = (data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['assists'].transform('sum').astype(int) / | |
data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['passes'].transform('sum').astype(int)) | |
season_long_table['Fantasy_per_touch'] = (data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['Fantasy'].transform('sum').astype(int) / | |
data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['touches'].transform('sum').astype(int)) | |
season_long_table['FD_Fantasy_per_touch'] = (data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FD_Fantasy'].transform('sum').astype(int) / | |
data_sample.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['touches'].transform('sum').astype(int)) | |
season_long_table = season_long_table.drop_duplicates(subset='PLAYER_NAME') | |
return season_long_table | |
def split_frame(input_df, rows): | |
df = [input_df.loc[i : i + rows - 1, :] for i in range(0, len(input_df), rows)] | |
return df | |
def convert_df_to_csv(df): | |
return df.to_csv().encode('utf-8') | |
gamelog_table = init_baselines() | |
col1, col2 = st.columns([1, 9]) | |
with col1: | |
if st.button("Reset Data", key='reset1'): | |
st.cache_data.clear() | |
gamelog_table = init_baselines() | |
split_var1 = st.radio("What table would you like to view?", ('Season Logs', 'Gamelogs'), key='split_var1') | |
with col2: | |
if split_var1 == 'Season Logs': | |
display = st.container() | |
season_long_table = seasonlong_build(gamelog_table) | |
display.dataframe(season_long_table.style.format(precision=2), use_container_width = True) | |
elif split_var1 == 'Gamelogs': | |
display = st.container() | |
bottom_menu = st.columns((4, 1, 1)) | |
with bottom_menu[2]: | |
batch_size = st.selectbox("Page Size", options=[25, 50, 100]) | |
with bottom_menu[1]: | |
total_pages = ( | |
int(len(gamelog_table) / batch_size) if int(len(gamelog_table) / batch_size) > 0 else 1 | |
) | |
current_page = st.number_input( | |
"Page", min_value=1, max_value=total_pages, step=1 | |
) | |
with bottom_menu[0]: | |
st.markdown(f"Page **{current_page}** of **{total_pages}** ") | |
pages = split_frame(gamelog_table, batch_size) | |
display.dataframe(data=pages[current_page - 1].style.format(precision=2), use_container_width=True) |