Spaces:
Running
Running
File size: 8,897 Bytes
dfb2f96 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import pulp
import numpy as np
import pandas as pd
import streamlit as st
import gspread
import time
import random
import scipy.stats
@st.cache_resource
def init_conn():
scope = ['https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive"]
credentials = {
"type": "service_account",
"project_id": "sheets-api-connect-378620",
"private_key_id": "1005124050c80d085e2c5b344345715978dd9cc9",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtKa01beXwc88R\nnPZVQTNPVQuBnbwoOfc66gW3547ja/UEyIGAF112dt/VqHprRafkKGmlg55jqJNt\na4zceLKV+wTm7vBu7lDISTJfGzCf2TrxQYNqwMKE2LOjI69dBM8u4Dcb4k0wcp9v\ntW1ZzLVVuwTvmrg7JBHjiSaB+x5wxm/r3FOiJDXdlAgFlytzqgcyeZMJVKKBQHyJ\njEGg/1720A0numuOCt71w/2G0bDmijuj1e6tH32MwRWcvRNZ19K9ssyDz2S9p68s\nYDhIxX69OWxwScTIHLY6J2t8txf/XMivL/636fPlDADvBEVTdlT606n8CcKUVQeq\npUVdG+lfAgMBAAECggEAP38SUA7B69eTfRpo658ycOs3Amr0JW4H/bb1rNeAul0K\nZhwd/HnU4E07y81xQmey5kN5ZeNrD5EvqkZvSyMJHV0EEahZStwhjCfnDB/cxyix\nZ+kFhv4y9eK+kFpUAhBy5nX6T0O+2T6WvzAw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G\nJuElxA33rFEjbgbzdyrE768=\n-----END PRIVATE KEY-----\n",
"client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com",
"client_id": "106625872877651920064",
"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%40sheets-api-connect-378620.iam.gserviceaccount.com"
}
gc = gspread.service_account_from_dict(credentials)
return gc
st.set_page_config(layout="wide")
gc = init_conn()
game_format = {'Dropback% Proj': '{:.2%}', 'DesRush%': '{:.2%}', 'Rush%': '{:.2%}'}
rb_util = {'Player Snaps%': '{:.2%}','Rush Att%': '{:.2%}', 'Routes%': '{:.2%}', 'Targets%': '{:.2%}', 'SDD Snaps%': '{:.2%}', 'i5 Rush%': '{:.2%}',
'LDD Snaps%': '{:.2%}','2-min%': '{:.2%}'}
wr_te_util = {'Routes%': '{:.2%}','Targets%': '{:.2%}', 'Air Yards%': '{:.2%}', 'Endzone Targets%': '{:.2%}', 'Third/Fourth%': '{:.2%}', 'Third/Fourth Targets%': '{:.2%}',
'Play Action Targets%': '{:.2%}','2-min%': '{:.2%}'}
all_dk_player_projections = 'https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=179416653'
@st.cache_resource(ttl = 300)
def rb_util_season():
sh = gc.open_by_url(all_dk_player_projections)
worksheet = sh.worksheet('RB_Util')
raw_display = pd.DataFrame(worksheet.get_all_records())
raw_display = raw_display.replace('', np.nan)
raw_display = raw_display[['player_name', 'position', 'week', 'team_season', 'player_snaps_per', 'rush_attempts_per', 'routes_per', 'targets_per',
'tprr', 'player_SDD_snaps_per', 'inside_five_rush_per', 'player_LDD_snaps_per', 'two_min_per', 'exPPR', 'ppr_fantasy', 'UR_Rank']]
raw_display = raw_display.set_axis(['Player', 'Position', 'Week', 'Team-Season', 'Player Snaps%', 'Rush Att%', 'Routes%', 'Targets%',
'TPRR', 'SDD Snaps%', 'i5 Rush%', 'LDD Snaps%', '2-min%', 'Expected PPR', 'PPR', 'Utilization Rank'], axis='columns')
raw_display = raw_display.sort_values(by='Utilization Rank', ascending=True)
return raw_display
@st.cache_resource(ttl = 300)
def wr_te_util_season():
sh = gc.open_by_url(all_dk_player_projections)
worksheet = sh.worksheet('WR_TE_Util')
raw_display = pd.DataFrame(worksheet.get_all_records())
raw_display = raw_display.replace('', np.nan)
raw_display = raw_display[['player_name', 'position', 'week', 'team_season', 'routes_per', 'targets_per', 'tprr' , 'adot', 'air_yards_per',
'ayprr', 'endzone_targets_per', 'third_fourth_per', 'third_fourth_target_per', 'play_action_targets_per', 'exPPR', 'ppr_fantasy', 'UR_Rank']]
raw_display = raw_display.set_axis(['Player', 'Position', 'Week', 'Team-Season', 'Routes%', 'Targets%', 'TPRR' , 'ADOT', 'Air Yards%',
'AYPRR', 'Endzone Targets%', 'Third/Fourth%', 'Third/Fourth Targets%', 'Play Action Targets%', 'Expected PPR', 'PPR', 'Utilization Rank'], axis='columns')
raw_display = raw_display.sort_values(by='Utilization Rank', ascending=True)
return raw_display
@st.cache_resource(ttl = 300)
def macro_pull():
sh = gc.open_by_url(all_dk_player_projections)
worksheet = sh.worksheet('FL_Macro')
raw_display = pd.DataFrame(worksheet.get_all_records())
raw_display = raw_display.sort_values(by='Team Total', ascending=False)
return raw_display
@st.cache_data
def convert_df_to_csv(df):
return df.to_csv().encode('utf-8')
rb_search = rb_util_season()
wr_search = wr_te_util_season()
macro_data = macro_pull()
pos_list = ['RB', 'WR', 'TE']
col1, col2 = st.columns([1, 8])
with col1:
if st.button("Load/Reset Data", key='reset1'):
st.cache_data.clear()
rb_search = rb_util_season()
wr_search = wr_te_util_season()
macro_data = macro_pull()
stat_type_var1 = st.radio("What table are you loading?", ('Macro Table', 'RB Usage', 'WR/TE Usage'), key='stat_type_var1')
split_var1 = st.radio("Are you running the the whole league or certain teams?", ('All Teams', 'Specific Teams'), key='split_var1')
pos_split1 = st.radio("Are you viewing all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split1')
if pos_split1 == 'Specific Positions':
pos_var1 = st.multiselect('What Positions would you like to view?', options = ['RB', 'WR', 'TE'])
elif pos_split1 == 'All Positions':
pos_var1 = pos_list
if split_var1 == 'Specific Teams':
team_var1 = st.multiselect('Which teams would you like to include in the Table?', options = rb_search['Team-Season'].unique(), key='team_var1')
elif split_var1 == 'All Teams':
team_var1 = rb_search['Team-Season'].unique().tolist()
if stat_type_var1 == 'Macro Table':
table_instance = macro_data
table_instance = table_instance.set_index('team')
elif stat_type_var1 == 'RB Usage':
table_instance = rb_search
table_instance = table_instance[table_instance['Team-Season'].isin(team_var1)]
table_instance = table_instance[table_instance['Position'].isin(pos_var1)]
elif stat_type_var1 == 'WR/TE Usage':
table_instance = wr_search
table_instance = table_instance[table_instance['Team-Season'].isin(team_var1)]
table_instance = table_instance[table_instance['Position'].isin(pos_var1)]
with col2:
if stat_type_var1 == 'Macro Table':
st.dataframe(table_instance.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').format(game_format, precision=2), use_container_width = True)
elif stat_type_var1 == 'RB Usage':
st.dataframe(table_instance.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').background_gradient(cmap='RdYlGn_r', subset = 'Utilization Rank').format(rb_util, precision=2), use_container_width = True)
elif stat_type_var1 == 'WR/TE Usage':
st.dataframe(table_instance.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').background_gradient(cmap='RdYlGn_r', subset = 'Utilization Rank').format(wr_te_util, precision=2), use_container_width = True)
st.download_button(
label="Export Tables",
data=convert_df_to_csv(table_instance),
file_name='MLB_Research_export.csv',
mime='text/csv',
)
|