Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pulp
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import random
|
5 |
+
import sys
|
6 |
+
import openpyxl
|
7 |
+
import re
|
8 |
+
import time
|
9 |
+
import streamlit as st
|
10 |
+
import matplotlib
|
11 |
+
from matplotlib.colors import LinearSegmentedColormap
|
12 |
+
from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, DataReturnMode
|
13 |
+
import json
|
14 |
+
import requests
|
15 |
+
import gspread
|
16 |
+
import plotly.figure_factory as ff
|
17 |
+
|
18 |
+
scope = ['https://www.googleapis.com/auth/spreadsheets',
|
19 |
+
"https://www.googleapis.com/auth/drive"]
|
20 |
+
|
21 |
+
credentials = {
|
22 |
+
"type": "service_account",
|
23 |
+
"project_id": "sheets-api-connect-378620",
|
24 |
+
"private_key_id": "1005124050c80d085e2c5b344345715978dd9cc9",
|
25 |
+
"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",
|
26 |
+
"client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com",
|
27 |
+
"client_id": "106625872877651920064",
|
28 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
29 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
30 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
31 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
|
32 |
+
}
|
33 |
+
|
34 |
+
gc = gspread.service_account_from_dict(credentials)
|
35 |
+
|
36 |
+
st.set_page_config(layout="wide")
|
37 |
+
|
38 |
+
dk_player_url = 'https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqrYE6Yrlbb_wEkmH-ot0/edit#gid=172632260'
|
39 |
+
fd_player_url = 'https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqrYE6Yrlbb_wEkmH-ot0/edit#gid=172632260'
|
40 |
+
|
41 |
+
@st.cache_data
|
42 |
+
def load_overall_stats(URL):
|
43 |
+
sh = gc.open_by_url(URL)
|
44 |
+
worksheet = sh.get_worksheet(8)
|
45 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
46 |
+
raw_display.rename(columns={"Name": "Player", "Nickname": "Player", "Fantasy": "Median"}, inplace = True)
|
47 |
+
raw_display.replace("", 'Welp', inplace=True)
|
48 |
+
raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
|
49 |
+
raw_display = raw_display.loc[raw_display['Median'] > 0]
|
50 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
|
51 |
+
raw_display = raw_display.sort_values(by='Median', ascending=False)
|
52 |
+
|
53 |
+
return raw_display
|
54 |
+
|
55 |
+
@st.cache_data
|
56 |
+
def load_fd_overall_stats(URL):
|
57 |
+
sh = gc.open_by_url(URL)
|
58 |
+
worksheet = sh.get_worksheet(9)
|
59 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
60 |
+
raw_display.rename(columns={"Name": "Player", "Nickname": "Player", "Fantasy": "Median"}, inplace = True)
|
61 |
+
raw_display.replace("", 'Welp', inplace=True)
|
62 |
+
raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
|
63 |
+
raw_display = raw_display.loc[raw_display['Median'] > 0]
|
64 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
|
65 |
+
raw_display = raw_display.sort_values(by='Median', ascending=False)
|
66 |
+
|
67 |
+
return raw_display
|
68 |
+
|
69 |
+
raw_baselines = load_overall_stats(dk_player_url)
|
70 |
+
|
71 |
+
tab1, tab2 = st.tabs(["Range of Outcomes Model", "Optimizer (Coming soon)"])
|
72 |
+
|
73 |
+
def convert_df_to_csv(df):
|
74 |
+
return df.to_csv().encode('utf-8')
|
75 |
+
|
76 |
+
with tab1:
|
77 |
+
|
78 |
+
col1, col2 = st.columns([1, 5])
|
79 |
+
|
80 |
+
with col1:
|
81 |
+
if st.button("Load/Reset Data", key='reset1'):
|
82 |
+
st.cache_data.clear()
|
83 |
+
raw_baselines = load_overall_stats(dk_player_url)
|
84 |
+
site_var1 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var1')
|
85 |
+
if site_var1 == 'Draftkings':
|
86 |
+
raw_baselines = load_overall_stats(dk_player_url)
|
87 |
+
elif site_var1 == 'Fanduel':
|
88 |
+
raw_baselines = load_fd_overall_stats(fd_player_url)
|
89 |
+
split_var1 = st.radio("Are you running the full slate or crtain games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
|
90 |
+
if split_var1 == 'Specific Games':
|
91 |
+
team_var1 = st.multiselect('Which teams would you like to include in the ROO?', options = raw_baselines['Team'].unique(), key='team_var1')
|
92 |
+
elif split_var1 == 'Full Slate Run':
|
93 |
+
team_var1 = raw_baselines.Team.values.tolist()
|
94 |
+
pos_var1 = st.selectbox('View specific position?', options = ['All', 'PG', 'SG', 'SF', 'PF', 'C'])
|
95 |
+
|
96 |
+
with col2:
|
97 |
+
hold_container = st.empty()
|
98 |
+
if st.button('Create Range of Outcomes for Slate'):
|
99 |
+
with hold_container:
|
100 |
+
if site_var1 == 'Draftkings':
|
101 |
+
raw_baselines = load_overall_stats(dk_player_url)
|
102 |
+
elif site_var1 == 'Fanduel':
|
103 |
+
raw_baselines = load_fd_overall_stats(fd_player_url)
|
104 |
+
|
105 |
+
working_roo = raw_baselines
|
106 |
+
working_roo = working_roo[working_roo['Team'].isin(team_var1)]
|
107 |
+
own_dict = dict(zip(working_roo.Player, working_roo.Own))
|
108 |
+
min_dict = dict(zip(working_roo.Player, working_roo.Minutes))
|
109 |
+
team_dict = dict(zip(working_roo.Player, working_roo.Team))
|
110 |
+
total_sims = 1000
|
111 |
+
|
112 |
+
flex_file = working_roo[['Player', 'Position', 'Salary', 'Median']]
|
113 |
+
flex_file.rename(columns={"Agg": "Median"}, inplace = True)
|
114 |
+
flex_file['Floor'] = flex_file['Median']*.25
|
115 |
+
flex_file['Ceiling'] = flex_file['Median'] + (flex_file['Floor'])
|
116 |
+
flex_file['STD'] = (flex_file['Median']/4)
|
117 |
+
flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
|
118 |
+
hold_file = flex_file
|
119 |
+
overall_file = flex_file
|
120 |
+
salary_file = flex_file
|
121 |
+
|
122 |
+
overall_players = overall_file[['Player']]
|
123 |
+
|
124 |
+
for x in range(0,total_sims):
|
125 |
+
salary_file[x] = salary_file['Salary']
|
126 |
+
|
127 |
+
salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
128 |
+
salary_file.astype('int').dtypes
|
129 |
+
|
130 |
+
salary_file = salary_file.div(1000)
|
131 |
+
|
132 |
+
for x in range(0,total_sims):
|
133 |
+
overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
|
134 |
+
|
135 |
+
overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
136 |
+
overall_file.astype('int').dtypes
|
137 |
+
|
138 |
+
players_only = hold_file[['Player']]
|
139 |
+
raw_lineups_file = players_only
|
140 |
+
|
141 |
+
for x in range(0,total_sims):
|
142 |
+
maps_dict = {'proj_map':dict(zip(hold_file.Player,hold_file[x]))}
|
143 |
+
raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
|
144 |
+
players_only[x] = raw_lineups_file[x].rank(ascending=False)
|
145 |
+
|
146 |
+
players_only=players_only.drop(['Player'], axis=1)
|
147 |
+
players_only.astype('int').dtypes
|
148 |
+
|
149 |
+
salary_2x_check = (overall_file - (salary_file*4))
|
150 |
+
salary_3x_check = (overall_file - (salary_file*5))
|
151 |
+
salary_4x_check = (overall_file - (salary_file*6))
|
152 |
+
gpp_check = (overall_file - ((salary_file*5)+10))
|
153 |
+
|
154 |
+
players_only['Average_Rank'] = players_only.mean(axis=1)
|
155 |
+
players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
|
156 |
+
players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
|
157 |
+
players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
|
158 |
+
players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
|
159 |
+
players_only['3x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
|
160 |
+
players_only['4x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
|
161 |
+
players_only['5x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
|
162 |
+
players_only['GPP%'] = salary_4x_check[gpp_check >= 1].count(axis=1)/float(total_sims)
|
163 |
+
|
164 |
+
players_only['Player'] = hold_file[['Player']]
|
165 |
+
|
166 |
+
final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%']]
|
167 |
+
|
168 |
+
final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
|
169 |
+
final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%']]
|
170 |
+
|
171 |
+
final_Proj['Own'] = final_Proj['Player'].map(own_dict)
|
172 |
+
final_Proj['Minutes Proj'] = final_Proj['Player'].map(min_dict)
|
173 |
+
final_Proj['Team'] = final_Proj['Player'].map(team_dict)
|
174 |
+
final_Proj['Own'] = final_Proj['Own'].astype('float')
|
175 |
+
final_Proj['LevX'] = ((final_Proj[['Top_finish', '4x%', 'Top_5_finish']].mean(axis=1))*100) - final_Proj['Own']
|
176 |
+
final_Proj['ValX'] = ((final_Proj[['4x%', '5x%']].mean(axis=1))*100) + final_Proj['LevX']
|
177 |
+
|
178 |
+
final_Proj = final_Proj[['Player', 'Minutes Proj', 'Position', 'Team', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '3x%', '4x%', '5x%', 'GPP%', 'Own', 'LevX', 'ValX']]
|
179 |
+
final_Proj = final_Proj.set_index('Player')
|
180 |
+
final_Proj = final_Proj.sort_values(by='Median', ascending=False)
|
181 |
+
with hold_container:
|
182 |
+
hold_container = st.empty()
|
183 |
+
|
184 |
+
if pos_var1 == 'All':
|
185 |
+
final_Proj = final_Proj
|
186 |
+
elif pos_var1 != 'All':
|
187 |
+
final_Proj = final_Proj[final_Proj['Position'].str.contains(pos_var1)]
|
188 |
+
st.dataframe(final_Proj.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
189 |
+
|
190 |
+
st.download_button(
|
191 |
+
label="Export Tables",
|
192 |
+
data=convert_df_to_csv(final_Proj),
|
193 |
+
file_name='Custom_NBA_export.csv',
|
194 |
+
mime='text/csv',
|
195 |
+
)
|
196 |
+
|
197 |
+
with tab2:
|
198 |
+
st.info('Nothing here yet, but will be porting in a simple lineup optimizer soon')
|