Spaces:
Running
Running
File size: 9,427 Bytes
f8a3528 4843a50 f8a3528 4843a50 f8a3528 4843a50 f8a3528 |
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 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import pulp
import numpy as np
import pandas as pd
import streamlit as st
import gspread
from itertools import combinations
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\n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9fpUc6H8cH1SINZpzajqQA74bPwELJjnzrCnH79TnHG\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)
st.set_page_config(layout="wide")
wrong_acro = ['WSH', 'AZ', 'CWS']
right_acro = ['WAS', 'ARI', 'CHW']
game_format = {'Win Percentage': '{:.2%}','First Inning Lead Percentage': '{:.2%}',
'Fifth Inning Lead Percentage': '{:.2%}', '8+ runs': '{:.2%}', 'DK LevX': '{:.2%}', 'FD LevX': '{:.2%}'}
team_roo_format = {'Top Score%': '{:.2%}','0 Runs': '{:.2%}', '1 Run': '{:.2%}', '2 Runs': '{:.2%}', '3 Runs': '{:.2%}', '4 Runs': '{:.2%}',
'5 Runs': '{:.2%}','6 Runs': '{:.2%}', '7 Runs': '{:.2%}', '8 Runs': '{:.2%}', '9 Runs': '{:.2%}', '10 Runs': '{:.2%}'}
dk_player_projections = 'https://docs.google.com/spreadsheets/d/1MdzPFqIT0MFid2IhegWf39VNR8IXUyo_Fb5dolOSt3o/edit#gid=340831852'
fd_player_projections = 'https://docs.google.com/spreadsheets/d/1MdzPFqIT0MFid2IhegWf39VNR8IXUyo_Fb5dolOSt3o/edit#gid=340831852'
secondary_dk_player_projections = 'https://docs.google.com/spreadsheets/d/1lP4t8N7UhjR94MEwPn6powRyLl_cQBDUMSCs6cbL9ms/edit#gid=340831852'
secondary_fd_player_projections = 'https://docs.google.com/spreadsheets/d/1lP4t8N7UhjR94MEwPn6powRyLl_cQBDUMSCs6cbL9ms/edit#gid=340831852'
all_dk_player_projections = 'https://docs.google.com/spreadsheets/d/1f42Ergav8K1VsOLOK9MUn7DM_MLMvv4GR2Fy7EfnZTc/edit#gid=500994479'
all_fd_player_projections = 'https://docs.google.com/spreadsheets/d/1f42Ergav8K1VsOLOK9MUn7DM_MLMvv4GR2Fy7EfnZTc/edit#gid=500994479'
final_Proj = 0
@st.cache_data
def load_time():
sh = gc.open_by_url(all_dk_player_projections)
worksheet = sh.worksheet('Timestamp')
raw_stamp = worksheet.acell('a1').value
t_stamp = f"Last update was at {raw_stamp}"
return t_stamp
@st.cache_data
def set_slate_teams():
sh = gc.open_by_url(all_dk_player_projections)
worksheet = sh.worksheet('Site_Info')
raw_display = pd.DataFrame(worksheet.get_all_records())
for checkVar in range(len(wrong_acro)):
raw_display['DK Main'] = raw_display['DK Main'].replace(wrong_acro, right_acro)
for checkVar in range(len(wrong_acro)):
raw_display['DK Secondary'] = raw_display['DK Secondary'].replace(wrong_acro, right_acro)
for checkVar in range(len(wrong_acro)):
raw_display['DK Overall'] = raw_display['DK Overall'].replace(wrong_acro, right_acro)
for checkVar in range(len(wrong_acro)):
raw_display['FD Main'] = raw_display['FD Main'].replace(wrong_acro, right_acro)
for checkVar in range(len(wrong_acro)):
raw_display['FD Secondary'] = raw_display['FD Secondary'].replace(wrong_acro, right_acro)
for checkVar in range(len(wrong_acro)):
raw_display['FD Overall'] = raw_display['FD Overall'].replace(wrong_acro, right_acro)
return raw_display
@st.cache_data
def load_team_roo_table(URL):
sh = gc.open_by_url(URL)
worksheet = sh.worksheet('Team_ROO')
raw_display = pd.DataFrame(worksheet.get_all_records())
raw_display = raw_display[['teams', 'Opp SP', 'Top Score%', '0 Runs', '1 Run', '2 Runs', '3 Runs', '4 Runs', '5 Runs', '6 Runs', '7 Runs', '8 Runs', '9 Runs', '10 Runs']]
for checkVar in range(len(wrong_acro)):
raw_display['teams'] = raw_display['teams'].replace(wrong_acro, right_acro)
return raw_display
@st.cache_data
def convert_df_to_csv(df):
return df.to_csv().encode('utf-8')
t_stamp = load_time()
site_slates = set_slate_teams()
with st.container():
col1, col2, col3 = st.columns([3, 3, 3])
with col1:
if st.button("Load/Reset Data", key='reset1'):
st.cache_data.clear()
t_stamp = load_time()
site_slates = set_slate_teams()
st.info(t_stamp)
with col2:
site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'), key='site_var1')
with col3:
slate_var1 = st.radio("Which data are you loading?", ('Main Slate', 'Secondary Slate', 'All Games'), key='slate_var1')
if site_var1 == 'Draftkings':
if slate_var1 == 'Main Slate':
team_roo_table = load_team_roo_table(all_dk_player_projections)
slate_teams = site_slates['DK Main'].values.tolist()
team_roo_table = team_roo_table[team_roo_table['teams'].isin(slate_teams)]
elif slate_var1 == 'Secondary Slate':
team_roo_table = load_team_roo_table(all_dk_player_projections)
slate_teams = site_slates['DK Secondary'].values.tolist()
team_roo_table = team_roo_table[team_roo_table['teams'].isin(slate_teams)]
elif slate_var1 == 'All Games':
team_roo_table = load_team_roo_table(all_dk_player_projections)
slate_teams = site_slates['DK Overall'].values.tolist()
team_roo_table = team_roo_table[team_roo_table['teams'].isin(slate_teams)]
elif site_var1 == 'Fanduel':
if slate_var1 == 'Main Slate':
team_roo_table = load_team_roo_table(all_dk_player_projections)
slate_teams = site_slates['FD Main'].values.tolist()
team_roo_table = team_roo_table[team_roo_table['teams'].isin(slate_teams)]
elif slate_var1 == 'Secondary Slate':
team_roo_table = load_team_roo_table(all_dk_player_projections)
slate_teams = site_slates['FD Secondary'].values.tolist()
team_roo_table = team_roo_table[team_roo_table['teams'].isin(slate_teams)]
elif slate_var1 == 'All Games':
team_roo_table = load_team_roo_table(all_dk_player_projections)
slate_teams = site_slates['FD Overall'].values.tolist()
team_roo_table = team_roo_table[team_roo_table['teams'].isin(slate_teams)]
team_roo_table = team_roo_table.sort_values(by='Top Score%', ascending=False)
team_roo_table = team_roo_table.drop_duplicates(subset='teams')
team_roo_table.rename(columns={"teams": "Names"}, inplace = True)
team_roo_table = team_roo_table.set_index('Names')
st.dataframe(team_roo_table.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(team_roo_format,precision=2), use_container_width = True)
st.download_button(
label="Export Tables",
data=convert_df_to_csv(team_roo_table),
file_name='MLB_Team_ROO_export.csv',
mime='text/csv',
) |