Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
st.set_page_config(layout="wide")
|
3 |
+
|
4 |
+
for name in dir():
|
5 |
+
if not name.startswith('_'):
|
6 |
+
del globals()[name]
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import pandas as pd
|
10 |
+
import streamlit as st
|
11 |
+
import gspread
|
12 |
+
import plotly.express as px
|
13 |
+
import random
|
14 |
+
import gc
|
15 |
+
|
16 |
+
@st.cache_resource
|
17 |
+
def init_conn():
|
18 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
19 |
+
|
20 |
+
credentials = {
|
21 |
+
"type": "service_account",
|
22 |
+
"project_id": "dfsnew",
|
23 |
+
"private_key_id": "2432f6c3771f70a410c5c878d1359869fc9dddc8",
|
24 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDBNBDU2aJuEr6n\ne0o7pDY8gjg1+g1e3oHlpyY/CHMByZuEwfXewsZYP/TApfr8zxXDNG9X31CloWXH\n6ef8H0h6TjhRppE/2YCUZlbgtvpwlDg+1aKTKY5Lc/L937I6V512mgMDhDmTwX+p\noV0vhPuJnyFy+Fuo+xu8D9A46lhTTIK4EZhHc04SUBxUI3pDdfvuMbjciD/Pskn2\nMwBSEG/FQoe4GYrSmm7jzYdSHItVBakr26xl117m8BrIuceU7IEWrnJGDza8TtTZ\n+4Wp7PY9v6DgVt2+rnnDaF/g7kocLqoj2xWp1eS7OALwmqaIPFljIUkL5AJJiLC1\n+/ve6iwVAgMBAAECggEADTFsPdCvwBL9HGw1nT2BK6AbzQnKfHI2zhMcMD04N0TI\nXygsjT3hM/kIElizOyy7+HS97rLz65+KFvzwx71uIlXxkBfO/txwJJIZeCZeky33\n6kiF3cU+b4YXL4FlRwkhGk55irWuhdm2iUOY3KwYziTE8LgncDJXij/NMPnFtshZ\n/2Dc/7sKLi1tna5tfXr5v4N7LhyFOfHme8ZSZIhnpV+WnFM/VAVghwi+3vfzeV+a\nVgvv+QwRUBF+MYpoW8aDw3Y1jKuKKxcG0qHR1mQQTDK6eAymy28lJ9LfgKkZBLS3\nVEGH8O+gLQj2l8VR8koRxA1FETJ9BnIiV4OF+uLQQQKBgQDyYkeBnpPKnw3MXKgy\nxtpt7hLdrrQiR69PHEvHj9z6b60KTH9jDMKcbCU/ouwbTtLQnvtwta2RoWD/1xk+\n3uaeQv/jOtgKGE+Sa0FvJuDWZwBfUORnyqb+s5G9MpVlqNLLkUmE5myyrDbFdxei\nwzisIjvQxtJDLB3pucTRyd6a1QKBgQDMDoWUfNpQI/up3r0RWVCl3odpwOMnpN0S\nhf8uLyvEvtbcMnpxCQCl+4KWnOiX4GH4N9sZGF8YTPazO2Kd85/GioUoNo5u6vJo\ncxD0BTvg5meyUjfZsmuU620/eVQBa88TRdo3isLmBqUp7SAC+g4vTHpgxn00dRYv\neSfZN0dsQQKBgQDkxR34mVOkyrqbSFj4k/dWCn6D/YDHWiF86ZgcowxO01jff5Q8\nSK7mNKxzg7KVk7Amd+eaWd+YtFh5IOwTCw9gEJy0O7Xs0UVJTTJVVryfoFgZnp/1\n1rAHdjT3/eZELTPILzjU1yeA/Eo11lHYramvzh/mzcFm5RzWnR/HYmFYgQKBgFOy\nbSX/pAgVCkedvc0c5lBymvZMkJ+VJrxPS+Ckpn43jKea6M/uUl7Cb8jZKSoKdgS6\n3FpJvc+Y2eOgKw4AfHuSG5Xn8roaEj23XK/KacoQl130DUZ0wV2+xvuvBz7h+ni8\nQQphFxoEhcBRq7ys1h6ebt+86mQW1ne4aRjWbKxBAoGARA+rBNIC9Z1vyRzMAXfj\nnQ9/wShd/NGpVRNrm7sdUastfoyK8Ip3HkJac3xE1ARpQTvxAz742mdeDxPWI8wZ\nHDsjIrRqGLKMN7tSIoM720y6PY/Tsg89SdY4y0h6M75rrEi4Lv5b7s4EmqAZdfKT\nbEyuT7sCPCLeOX/RLy/lCpA=\n-----END PRIVATE KEY-----\n",
|
25 |
+
"client_email": "[email protected]",
|
26 |
+
"client_id": "105107448378741046480",
|
27 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
28 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
29 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
30 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dfsapps%40dfsnew.iam.gserviceaccount.com",
|
31 |
+
"universe_domain": "googleapis.com"
|
32 |
+
}
|
33 |
+
|
34 |
+
header= {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '
|
35 |
+
'AppleWebKit/537.11 (KHTML, like Gecko) '
|
36 |
+
'Chrome/23.0.1271.64 Safari/537.11',
|
37 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
38 |
+
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
39 |
+
'Accept-Encoding': 'none',
|
40 |
+
'Accept-Language': 'en-US,en;q=0.8',
|
41 |
+
'Connection': 'keep-alive'}
|
42 |
+
|
43 |
+
gc_con = gspread.service_account_from_dict(credentials, scope)
|
44 |
+
|
45 |
+
return gc_con
|
46 |
+
|
47 |
+
gcservice_account = init_conn()
|
48 |
+
|
49 |
+
NBAGetGameData = 'https://docs.google.com/spreadsheets/d/1tRQrF_I5rS7Q0g9vE8NrENDZ2P3_DvtbBZzKEakwOI0/edit#gid=1373653837'
|
50 |
+
NBABettingModel = 'https://docs.google.com/spreadsheets/d/1WBnvOHQi_zVTGF63efejK5ho02AY00HiYrMHnMJXY1E/edit#gid=1157978351'
|
51 |
+
|
52 |
+
game_format = {'Injury and Rotation Adjusted Win %': '{:.2%}'}
|
53 |
+
|
54 |
+
percentages_format = {'Playoff Odds': '{:.2%}', 'Division Odds': '{:.2%}', 'Top 4 Seed Odds': '{:.2%}', '1 Seed Odds': '{:.2%}', 'Win 1st Round': '{:.2%}',
|
55 |
+
'Win 2nd Round': '{:.2%}', 'Win Conference': '{:.2%}', 'Win Title': '{:.2%}', '1': '{:.2%}', '2': '{:.2%}', '3': '{:.2%}',
|
56 |
+
'4': '{:.2%}', '5': '{:.2%}', '6': '{:.2%}', '7': '{:.2%}', '8': '{:.2%}', '9': '{:.2%}', '10': '{:.2%}', '11': '{:.2%}',
|
57 |
+
'12': '{:.2%}', '13': '{:.2%}', '14': '{:.2%}', '15': '{:.2%}'}
|
58 |
+
|
59 |
+
@st.cache_resource(ttl = 300)
|
60 |
+
def init_baselines():
|
61 |
+
sh = gcservice_account.open_by_url(NBABettingModel)
|
62 |
+
|
63 |
+
worksheet = sh.worksheet('ExportTable')
|
64 |
+
raw_display = pd.DataFrame(worksheet.get_values())
|
65 |
+
raw_display.columns = raw_display.iloc[0]
|
66 |
+
raw_display = raw_display[1:]
|
67 |
+
raw_display = raw_display.reset_index(drop=True)
|
68 |
+
raw_display.replace('', np.nan, inplace=True)
|
69 |
+
cols_to_check = ['Win %', 'Injury and Rotation Adjusted Win %']
|
70 |
+
raw_display.loc[:, cols_to_check] = raw_display.loc[:, cols_to_check].replace({'%': ''}, regex=True).astype(float) / 100
|
71 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
72 |
+
raw_display['Team Date'] = raw_display['Team'] + " " + raw_display['Date']
|
73 |
+
raw_display = raw_display.drop(columns=['Day of Season', 'Team', 'Opp', 'Date Num', 'DR Team', 'In Minutes File'])
|
74 |
+
game_model = raw_display[raw_display['Injury and Rotation Adjusted Win %'] != ""]
|
75 |
+
just_win_probs = game_model[['Team Date', 'Date', 'Time', 'Acro', 'Opponent', 'Injury and Rotation Adjusted Win %', 'Total Proj', 'Projected Points', 'Injury and Rotation Adjusted Expected Margin']]
|
76 |
+
|
77 |
+
|
78 |
+
worksheet = sh.worksheet('SeasonExport')
|
79 |
+
raw_display = pd.DataFrame(worksheet.get_values())
|
80 |
+
raw_display.columns = raw_display.iloc[0]
|
81 |
+
raw_display = raw_display[1:]
|
82 |
+
raw_display = raw_display.reset_index(drop=True)
|
83 |
+
raw_display.replace('', 0, inplace=True)
|
84 |
+
cols_to_check = ['Playoff Odds', 'Division Odds', 'Top 4 Seed Odds', '1 Seed Odds', 'Win 1st Round', 'Win 2nd Round', 'Win Conference', 'Win Title',
|
85 |
+
'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
|
86 |
+
raw_display.loc[:, cols_to_check] = raw_display.loc[:, cols_to_check].replace({'%': ''}, regex=True).astype(float) / 100
|
87 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
88 |
+
season_model = raw_display[raw_display['Team'] != ""]
|
89 |
+
title_sims = season_model[['Team', 'Conference', 'Division', 'Power Rank', 'Team PointMarginPerGame', 'SeasonSimLookup', 'Win Projection Now',
|
90 |
+
'Playoff Odds', 'Division Odds', 'Top 4 Seed Odds', '1 Seed Odds', 'Win 1st Round', 'Win 2nd Round', 'Win Conference', 'Win Title']]
|
91 |
+
seed_probs = season_model[['Team', 'Conference', 'Division', 'Avg Seed', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']]
|
92 |
+
|
93 |
+
sh = gcservice_account.open_by_url('https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqrYE6Yrlbb_wEkmH-ot0/edit#gid=1540807349')
|
94 |
+
|
95 |
+
worksheet = sh.worksheet('Arturo Props')
|
96 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
97 |
+
raw_display.replace('', np.nan, inplace=True)
|
98 |
+
timestamp = raw_display['Date'].head(1)[0]
|
99 |
+
|
100 |
+
return game_model, season_model, seed_probs, title_sims, just_win_probs, timestamp
|
101 |
+
|
102 |
+
def convert_df_to_csv(df):
|
103 |
+
return df.to_csv().encode('utf-8')
|
104 |
+
|
105 |
+
game_model, season_model, seed_probs, title_sims, just_win_probs, timestamp = init_baselines()
|
106 |
+
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
107 |
+
|
108 |
+
tab1, tab2 = st.tabs(["Game Betting Model", "Season and Futures"])
|
109 |
+
|
110 |
+
with tab1:
|
111 |
+
st.info(t_stamp)
|
112 |
+
col1, col2 = st.columns([1, 9])
|
113 |
+
with col1:
|
114 |
+
if st.button("Reset Data", key='reset1'):
|
115 |
+
st.cache_data.clear()
|
116 |
+
game_model, season_model, seed_probs, title_sims, just_win_probs, timestamp = init_baselines()
|
117 |
+
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
118 |
+
view_var1 = st.radio("Would you like to view math and stuff or just the win percentages and margins?", ('Just win probs', 'Gimme details'), key='view_var1')
|
119 |
+
split_var1 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var1')
|
120 |
+
if split_var1 == 'Specific Teams':
|
121 |
+
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = game_model['Acro'].unique(), key='team_var1')
|
122 |
+
elif split_var1 == 'All':
|
123 |
+
team_var1 = game_model.Acro.values.tolist()
|
124 |
+
date_split_var1 = st.radio("Would you like to view all Dates or specific ones?", ('All', 'Specific Dates'), key='date_split_var1')
|
125 |
+
if date_split_var1 == 'Specific Dates':
|
126 |
+
date_var1 = st.multiselect('Which Dates would you like to include in the tables?', options = game_model['Date'].unique(), key='date_var1')
|
127 |
+
elif date_split_var1 == 'All':
|
128 |
+
date_var1 = game_model.Date.values.tolist()
|
129 |
+
with col2:
|
130 |
+
if view_var1 == 'Just win probs':
|
131 |
+
game_display = just_win_probs[just_win_probs['Acro'].isin(team_var1)]
|
132 |
+
game_display = game_display[game_display['Date'].isin(date_var1)]
|
133 |
+
game_display = game_display.set_index('Team Date')
|
134 |
+
st.dataframe(game_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), use_container_width = True)
|
135 |
+
st.download_button(
|
136 |
+
label="Export Game Model",
|
137 |
+
data=convert_df_to_csv(game_model),
|
138 |
+
file_name='AmericanNumbers_Game_Model_export.csv',
|
139 |
+
mime='text/csv',
|
140 |
+
)
|
141 |
+
elif view_var1 == 'Gimme details':
|
142 |
+
game_display = game_model[game_model['Acro'].isin(team_var1)]
|
143 |
+
game_display = game_display[game_display['Date'].isin(date_var1)]
|
144 |
+
game_display = game_display.set_index('Team Date')
|
145 |
+
st.dataframe(game_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), use_container_width = True)
|
146 |
+
st.download_button(
|
147 |
+
label="Export Game Model",
|
148 |
+
data=convert_df_to_csv(game_model),
|
149 |
+
file_name='AmericanNumbers_Game_Model_export.csv',
|
150 |
+
mime='text/csv',
|
151 |
+
)
|
152 |
+
|
153 |
+
with tab2:
|
154 |
+
st.info(t_stamp)
|
155 |
+
col1, col2 = st.columns([1, 9])
|
156 |
+
with col1:
|
157 |
+
if st.button("Reset Data", key='reset2'):
|
158 |
+
st.cache_data.clear()
|
159 |
+
game_model, season_model, seed_probs, title_sims, just_win_probs, timestamp = init_baselines()
|
160 |
+
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
161 |
+
view_var2 = st.radio("Would you like to view title odds and win projections or seeding probabilities?", ('Win Odds', 'Seed Probabilities'), key='view_var2')
|
162 |
+
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
|
163 |
+
if split_var2 == 'Specific Teams':
|
164 |
+
team_var2 = st.multiselect('Which teams would you like to include in the tables?', options = season_model['Team'].unique(), key='team_var2')
|
165 |
+
elif split_var2 == 'All':
|
166 |
+
team_var2 = season_model.Team.values.tolist()
|
167 |
+
conf_var2 = st.radio("Would you like to view all conferences or specific ones?", ('All', 'Specific Conferences'), key='conf_var2')
|
168 |
+
if conf_var2 == 'Specific Conferences':
|
169 |
+
conf_choice_var2 = st.multiselect('Which conferences would you like to include in the tables?', options = season_model['Conference'].unique(), key='conf_choice_var2')
|
170 |
+
elif conf_var2 == 'All':
|
171 |
+
conf_choice_var2 = season_model.Conference.values.tolist()
|
172 |
+
div_var2 = st.radio("Would you like to view all divisions or specific ones?", ('All', 'Specific Divisions'), key='div_var2')
|
173 |
+
if div_var2 == 'Specific Divisions':
|
174 |
+
div_choice_var2 = st.multiselect('Which divisions would you like to include in the tables?', options = season_model['Division'].unique(), key='div_choice_var2')
|
175 |
+
elif div_var2 == 'All':
|
176 |
+
div_choice_var2 = season_model.Division.values.tolist()
|
177 |
+
with col2:
|
178 |
+
if view_var2 == 'Win Odds':
|
179 |
+
title_sims = title_sims[title_sims['Team'].isin(team_var2)]
|
180 |
+
title_sims = title_sims[title_sims['Conference'].isin(conf_choice_var2)]
|
181 |
+
title_sims = title_sims[title_sims['Division'].isin(div_choice_var2)]
|
182 |
+
season_display = title_sims.set_index('Team')
|
183 |
+
season_display = season_display.sort_values(by=['Win Projection Now'], ascending=False)
|
184 |
+
st.dataframe(season_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(percentages_format, precision=2), use_container_width = True)
|
185 |
+
st.download_button(
|
186 |
+
label="Export Futures Model",
|
187 |
+
data=convert_df_to_csv(title_sims),
|
188 |
+
file_name='AmericanNumbers_Season_Futures.csv',
|
189 |
+
mime='text/csv',
|
190 |
+
)
|
191 |
+
elif view_var2 == 'Seed Probabilities':
|
192 |
+
seed_probs = seed_probs[seed_probs['Team'].isin(team_var2)]
|
193 |
+
seed_probs = seed_probs[seed_probs['Conference'].isin(conf_choice_var2)]
|
194 |
+
seed_probs = seed_probs[seed_probs['Division'].isin(div_choice_var2)]
|
195 |
+
season_display = seed_probs.set_index('Team')
|
196 |
+
season_display = season_display.sort_values(by=['Avg Seed'], ascending=True)
|
197 |
+
st.dataframe(season_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(percentages_format, precision=2), use_container_width = True)
|
198 |
+
st.download_button(
|
199 |
+
label="Export Futures Model",
|
200 |
+
data=convert_df_to_csv(seed_probs),
|
201 |
+
file_name='AmericanNumbers_Season_Futures.csv',
|
202 |
+
mime='text/csv',
|
203 |
+
)
|