Spaces:
Running
Running
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\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", | |
"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 | |
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 | |
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 | |
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 | |
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', | |
) |