Spaces:
Running
Running
Upload streamlit_app.py
Browse files- streamlit_app.py +133 -0
streamlit_app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pulp
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import gspread
|
| 6 |
+
import time
|
| 7 |
+
import random
|
| 8 |
+
import scipy.stats
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def init_conn():
|
| 12 |
+
scope = ['https://www.googleapis.com/auth/spreadsheets',
|
| 13 |
+
"https://www.googleapis.com/auth/drive"]
|
| 14 |
+
|
| 15 |
+
credentials = {
|
| 16 |
+
"type": "service_account",
|
| 17 |
+
"project_id": "sheets-api-connect-378620",
|
| 18 |
+
"private_key_id": "1005124050c80d085e2c5b344345715978dd9cc9",
|
| 19 |
+
"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+2T6WvzAwbmbVsZ+X8kJyPuF9m8ldcPlD0sce\ntj8NwVq1ys52eosqs7zi2vjt+eMcaY393l4ls+vNq8Yf27cfyFw45W45CH/97/Nu\n5AmuzlCOAfFF+z4OC5g4rei4E/Qgpxa7/uom+BVfv9G0DIGW/tU6Sne0+37uoGKt\nW6DzhgtebUtoYkG7ZJ05BTXGp2lwgVcNRoPwnKJDxQKBgQDT5wYPUBDW+FHbvZSp\nd1m1UQuXyerqOTA9smFaM8sr/UraeH85DJPEIEk8qsntMBVMhvD3Pw8uIUeFNMYj\naLmZFObsL+WctepXrVo5NB6RtLB/jZYxiKMatMLUJIYtcKIp+2z/YtKiWcLnwotB\nWdCjVnPTxpkurmF2fWP/eewZ+wKBgQDRMtJg7etjvKyjYNQ5fARnCc+XsI3gkBe1\nX9oeXfhyfZFeBXWnZzN1ITgFHplDznmBdxAyYGiQdbbkdKQSghviUQ0igBvoDMYy\n1rWcy+a17Mj98uyNEfmb3X2cC6WpvOZaGHwg9+GY67BThwI3FqHIbyk6Ko09WlTX\nQpRQjMzU7QKBgAfi1iflu+q0LR+3a3vvFCiaToskmZiD7latd9AKk2ocsBd3Woy9\n+hXXecJHPOKV4oUJlJgvAZqe5HGBqEoTEK0wyPNLSQlO/9ypd+0fEnArwFHO7CMF\nycQprAKHJXM1eOOFFuZeQCaInqdPZy1UcV5Szla4UmUZWkk1m24blHzXAoGBAMcA\nyH4qdbxX9AYrC1dvsSRvgcnzytMvX05LU0uF6tzGtG0zVlub4ahvpEHCfNuy44UT\nxRWW/oFFaWjjyFxO5sWggpUqNuHEnRopg3QXx22SRRTGbN45li/+QAocTkgsiRh1\nqEcYZsO4mPCsQqAy6E2p6RcK+Xa+omxvSnVhq0x1AoGAKr8GdkCl4CF6rieLMAQ7\nLNBuuoYGaHoh8l5E2uOQpzwxVy/nMBcAv+2+KqHEzHryUv1owOi6pMLv7A9mTFoS\n18B0QRLuz5fSOsVnmldfC9fpUc6H8cH1SINZpzajqQA74bPwELJjnzrCnH79TnHG\nJuElxA33rFEjbgbzdyrE768=\n-----END PRIVATE KEY-----\n",
|
| 20 |
+
"client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com",
|
| 21 |
+
"client_id": "106625872877651920064",
|
| 22 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 23 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
| 24 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 25 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
gc = gspread.service_account_from_dict(credentials)
|
| 29 |
+
return gc
|
| 30 |
+
|
| 31 |
+
st.set_page_config(layout="wide")
|
| 32 |
+
|
| 33 |
+
gc = init_conn()
|
| 34 |
+
|
| 35 |
+
game_format = {'Dropback% Proj': '{:.2%}', 'DesRush%': '{:.2%}', 'Rush%': '{:.2%}'}
|
| 36 |
+
|
| 37 |
+
rb_util = {'Player Snaps%': '{:.2%}','Rush Att%': '{:.2%}', 'Routes%': '{:.2%}', 'Targets%': '{:.2%}', 'SDD Snaps%': '{:.2%}', 'i5 Rush%': '{:.2%}',
|
| 38 |
+
'LDD Snaps%': '{:.2%}','2-min%': '{:.2%}'}
|
| 39 |
+
|
| 40 |
+
wr_te_util = {'Routes%': '{:.2%}','Targets%': '{:.2%}', 'Air Yards%': '{:.2%}', 'Endzone Targets%': '{:.2%}', 'Third/Fourth%': '{:.2%}', 'Third/Fourth Targets%': '{:.2%}',
|
| 41 |
+
'Play Action Targets%': '{:.2%}','2-min%': '{:.2%}'}
|
| 42 |
+
|
| 43 |
+
all_dk_player_projections = 'https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=179416653'
|
| 44 |
+
|
| 45 |
+
@st.cache_resource(ttl = 300)
|
| 46 |
+
def rb_util_season():
|
| 47 |
+
sh = gc.open_by_url(all_dk_player_projections)
|
| 48 |
+
worksheet = sh.worksheet('RB_Util')
|
| 49 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
| 50 |
+
raw_display = raw_display.replace('', np.nan)
|
| 51 |
+
raw_display = raw_display[['player_name', 'position', 'week', 'team_season', 'player_snaps_per', 'rush_attempts_per', 'routes_per', 'targets_per',
|
| 52 |
+
'tprr', 'player_SDD_snaps_per', 'inside_five_rush_per', 'player_LDD_snaps_per', 'two_min_per', 'exPPR', 'ppr_fantasy', 'UR_Rank']]
|
| 53 |
+
raw_display = raw_display.set_axis(['Player', 'Position', 'Week', 'Team-Season', 'Player Snaps%', 'Rush Att%', 'Routes%', 'Targets%',
|
| 54 |
+
'TPRR', 'SDD Snaps%', 'i5 Rush%', 'LDD Snaps%', '2-min%', 'Expected PPR', 'PPR', 'Utilization Rank'], axis='columns')
|
| 55 |
+
raw_display = raw_display.sort_values(by='Utilization Rank', ascending=True)
|
| 56 |
+
return raw_display
|
| 57 |
+
|
| 58 |
+
@st.cache_resource(ttl = 300)
|
| 59 |
+
def wr_te_util_season():
|
| 60 |
+
sh = gc.open_by_url(all_dk_player_projections)
|
| 61 |
+
worksheet = sh.worksheet('WR_TE_Util')
|
| 62 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
| 63 |
+
raw_display = raw_display.replace('', np.nan)
|
| 64 |
+
raw_display = raw_display[['player_name', 'position', 'week', 'team_season', 'routes_per', 'targets_per', 'tprr' , 'adot', 'air_yards_per',
|
| 65 |
+
'ayprr', 'endzone_targets_per', 'third_fourth_per', 'third_fourth_target_per', 'play_action_targets_per', 'exPPR', 'ppr_fantasy', 'UR_Rank']]
|
| 66 |
+
raw_display = raw_display.set_axis(['Player', 'Position', 'Week', 'Team-Season', 'Routes%', 'Targets%', 'TPRR' , 'ADOT', 'Air Yards%',
|
| 67 |
+
'AYPRR', 'Endzone Targets%', 'Third/Fourth%', 'Third/Fourth Targets%', 'Play Action Targets%', 'Expected PPR', 'PPR', 'Utilization Rank'], axis='columns')
|
| 68 |
+
raw_display = raw_display.sort_values(by='Utilization Rank', ascending=True)
|
| 69 |
+
return raw_display
|
| 70 |
+
|
| 71 |
+
@st.cache_resource(ttl = 300)
|
| 72 |
+
def macro_pull():
|
| 73 |
+
sh = gc.open_by_url(all_dk_player_projections)
|
| 74 |
+
worksheet = sh.worksheet('FL_Macro')
|
| 75 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
| 76 |
+
raw_display = raw_display.sort_values(by='Team Total', ascending=False)
|
| 77 |
+
|
| 78 |
+
return raw_display
|
| 79 |
+
|
| 80 |
+
@st.cache_data
|
| 81 |
+
def convert_df_to_csv(df):
|
| 82 |
+
return df.to_csv().encode('utf-8')
|
| 83 |
+
|
| 84 |
+
rb_search = rb_util_season()
|
| 85 |
+
wr_search = wr_te_util_season()
|
| 86 |
+
macro_data = macro_pull()
|
| 87 |
+
pos_list = ['RB', 'WR', 'TE']
|
| 88 |
+
|
| 89 |
+
col1, col2 = st.columns([1, 8])
|
| 90 |
+
|
| 91 |
+
with col1:
|
| 92 |
+
if st.button("Load/Reset Data", key='reset1'):
|
| 93 |
+
st.cache_data.clear()
|
| 94 |
+
rb_search = rb_util_season()
|
| 95 |
+
wr_search = wr_te_util_season()
|
| 96 |
+
macro_data = macro_pull()
|
| 97 |
+
stat_type_var1 = st.radio("What table are you loading?", ('Macro Table', 'RB Usage', 'WR/TE Usage'), key='stat_type_var1')
|
| 98 |
+
split_var1 = st.radio("Are you running the the whole league or certain teams?", ('All Teams', 'Specific Teams'), key='split_var1')
|
| 99 |
+
pos_split1 = st.radio("Are you viewing all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split1')
|
| 100 |
+
if pos_split1 == 'Specific Positions':
|
| 101 |
+
pos_var1 = st.multiselect('What Positions would you like to view?', options = ['RB', 'WR', 'TE'])
|
| 102 |
+
elif pos_split1 == 'All Positions':
|
| 103 |
+
pos_var1 = pos_list
|
| 104 |
+
if split_var1 == 'Specific Teams':
|
| 105 |
+
team_var1 = st.multiselect('Which teams would you like to include in the Table?', options = rb_search['Team-Season'].unique(), key='team_var1')
|
| 106 |
+
elif split_var1 == 'All Teams':
|
| 107 |
+
team_var1 = rb_search['Team-Season'].unique().tolist()
|
| 108 |
+
if stat_type_var1 == 'Macro Table':
|
| 109 |
+
table_instance = macro_data
|
| 110 |
+
table_instance = table_instance.set_index('team')
|
| 111 |
+
elif stat_type_var1 == 'RB Usage':
|
| 112 |
+
table_instance = rb_search
|
| 113 |
+
table_instance = table_instance[table_instance['Team-Season'].isin(team_var1)]
|
| 114 |
+
table_instance = table_instance[table_instance['Position'].isin(pos_var1)]
|
| 115 |
+
elif stat_type_var1 == 'WR/TE Usage':
|
| 116 |
+
table_instance = wr_search
|
| 117 |
+
table_instance = table_instance[table_instance['Team-Season'].isin(team_var1)]
|
| 118 |
+
table_instance = table_instance[table_instance['Position'].isin(pos_var1)]
|
| 119 |
+
|
| 120 |
+
with col2:
|
| 121 |
+
if stat_type_var1 == 'Macro Table':
|
| 122 |
+
st.dataframe(table_instance.style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').format(game_format, precision=2), use_container_width = True)
|
| 123 |
+
elif stat_type_var1 == 'RB Usage':
|
| 124 |
+
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)
|
| 125 |
+
elif stat_type_var1 == 'WR/TE Usage':
|
| 126 |
+
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)
|
| 127 |
+
|
| 128 |
+
st.download_button(
|
| 129 |
+
label="Export Tables",
|
| 130 |
+
data=convert_df_to_csv(table_instance),
|
| 131 |
+
file_name='MLB_Research_export.csv',
|
| 132 |
+
mime='text/csv',
|
| 133 |
+
)
|