File size: 7,940 Bytes
1878852
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa8c500
1878852
 
 
 
 
 
 
 
 
 
 
aa8c500
1878852
aa8c500
1878852
 
 
 
 
aa8c500
 
 
 
 
 
 
 
 
 
 
 
 
1878852
aa8c500
1878852
aa8c500
1878852
 
 
 
aa8c500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
st.set_page_config(layout="wide")

for name in dir():
    if not name.startswith('_'):
        del globals()[name]

import numpy as np
import pandas as pd
import streamlit as st
import gspread
import plotly.express as px
import random
import gc

@st.cache_resource
def init_conn():
        scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

        credentials = {
          "type": "service_account",
          "project_id": "dfsnew",
          "private_key_id": "2432f6c3771f70a410c5c878d1359869fc9dddc8",
          "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDBNBDU2aJuEr6n\ne0o7pDY8gjg1+g1e3oHlpyY/CHMByZuEwfXewsZYP/TApfr8zxXDNG9X31CloWXH\n6ef8H0h6TjhRppE/2YCUZlbgtvpwlDg+1aKTKY5Lc/L937I6V512mgMDhDmTwX+p\noV0vhPuJnyFy+Fuo+xu8D9A46lhTTIK4EZhHc04SUBxUI3pD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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",
          "client_email": "[email protected]",
          "client_id": "105107448378741046480",
          "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/dfsapps%40dfsnew.iam.gserviceaccount.com",
          "universe_domain": "googleapis.com"
        }

        header= {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ' 
              'AppleWebKit/537.11 (KHTML, like Gecko) '
              'Chrome/23.0.1271.64 Safari/537.11',
              'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
              'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
              'Accept-Encoding': 'none',
              'Accept-Language': 'en-US,en;q=0.8',
              'Connection': 'keep-alive'}

        gc_con = gspread.service_account_from_dict(credentials, scope)
      
        return gc_con

gcservice_account = init_conn()

NBAGetGameData = 'https://docs.google.com/spreadsheets/d/1tRQrF_I5rS7Q0g9vE8NrENDZ2P3_DvtbBZzKEakwOI0/edit#gid=1373653837'
NCAAMensBasketball = 'https://docs.google.com/spreadsheets/d/1flWwlx1T7Xu68AjOkeq7u4Y7nWzukZzKTEI17RJ6MfE/edit#gid=1975368022'
NBABettingModel = 'https://docs.google.com/spreadsheets/d/1WBnvOHQi_zVTGF63efejK5ho02AY00HiYrMHnMJXY1E/edit#gid=1157978351'

game_format = {'Injury and Rotation Adjusted Win %': '{:.2%}'}

percentages_format = {'Playoff Odds': '{:.2%}', 'Division Odds': '{:.2%}', 'Top 4 Seed Odds': '{:.2%}', '1 Seed Odds': '{:.2%}', 'Win 1st Round': '{:.2%}',
                      'Win 2nd Round': '{:.2%}', 'Win Conference': '{:.2%}', 'Win Title': '{:.2%}', '1': '{:.2%}', '2': '{:.2%}', '3': '{:.2%}',
                      '4': '{:.2%}', '5': '{:.2%}', '6': '{:.2%}', '7': '{:.2%}', '8': '{:.2%}', '9': '{:.2%}', '10': '{:.2%}', '11': '{:.2%}',
                      '12': '{:.2%}', '13': '{:.2%}', '14': '{:.2%}', '15': '{:.2%}'}

@st.cache_resource(ttl = 300)
def init_baselines():
    sh = gcservice_account.open_by_url(NCAAMensBasketball)

    worksheet = sh.worksheet('ActiveBets')
    raw_display = pd.DataFrame(worksheet.get_values())
    raw_display.columns = raw_display.iloc[0]
    raw_display = raw_display[1:]
    raw_display = raw_display.reset_index(drop=True)
    raw_display.replace('', np.nan, inplace=True)
    game_model = raw_display[['#', 'date', 'gameid', 'HomeTeam', 'HomeDiv', 'HomeConf', 'AwayTeam', 'AwayDiv', 'AwayConf',
                              'total', 'mlHome', 'mlAway', 'spreadHome', 'spreadAway', 'booksid', 'inconf', 'Neutral', 'Home Team PM',
                              'Road Team PM', 'HCA', 'Home Team Margin', 'Home Team Win', 'Home ML Break even %',
                              'Road ML Break Even %', 'Home Win Edge', 'Road Win Edge', 'Moneyline Bet Rec', 'Home Spread Edge',
                              'Road Team Edge', 'Home Spread HKelly', 'Away Spread HKelly', 'Spread Bet Rec', 'Edge for Spread']]
    data_cols = ['#', 'gameid', 'total', 'mlHome', 'mlAway', 'spreadHome', 'spreadAway', 'Neutral', 'Home Team PM',
                 'Road Team PM', 'HCA', 'Home Team Margin', 'Home Team Win', 'Home ML Break even %',
                 'Road ML Break Even %', 'Home Win Edge', 'Road Win Edge', 'Home Spread Edge',
                 'Road Team Edge', 'Home Spread HKelly', 'Away Spread HKelly', 'Edge for Spread']
    game_model[data_cols] = game_model[data_cols].apply(pd.to_numeric, errors='coerce')
    game_model = game_model[game_model['Injury and Rotation Adjusted Win %'] != ""]
    just_win_probs = game_model[['#', 'date', 'gameid', 'HomeTeam', 'AwayTeam', 'total', 'mlHome', 'mlAway', 'spreadHome', 'spreadAway', 'booksid',
                              'Home Win Edge', 'Road Win Edge', 'Moneyline Bet Rec', 'Home Spread Edge', 'Road Team Edge', 'Spread Bet Rec']]
    
    timestamp = raw_display['Time'].head(1)[0]

    return game_model, just_win_probs, timestamp

def convert_df_to_csv(df):
    return df.to_csv().encode('utf-8')

game_model, just_win_probs, timestamp = init_baselines()
t_stamp = f"Last Update: " + str(timestamp) + f" EST"


st.info(t_stamp)
col1, col2 = st.columns([1, 9])
with col1:
    if st.button("Reset Data", key='reset1'):
              st.cache_data.clear()
              game_model, just_win_probs, timestamp = init_baselines()
              t_stamp = f"Last Update: " + str(timestamp) + f" EST"
    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')

with col2:
    if view_var1 == 'Just win probs':
        game_display = just_win_probs
        st.dataframe(game_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
        st.download_button(
            label="Export Game Model",
            data=convert_df_to_csv(game_model),
            file_name='AmericanNumbers_Game_Model_export.csv',
            mime='text/csv',
        )
    elif view_var1 == 'Gimme details':
        game_display = game_model
        st.dataframe(game_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
        st.download_button(
            label="Export Game Model",
            data=convert_df_to_csv(game_model),
            file_name='AmericanNumbers_NCAA_BBall_export.csv',
            mime='text/csv',
        )