File size: 12,630 Bytes
4036c64
6ef6fac
 
 
 
6a6dae7
 
 
6ef6fac
4036c64
 
6a6dae7
4036c64
6a6dae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4036c64
6a6dae7
 
6ef6fac
6a6dae7
 
 
6ef6fac
53152e8
4036c64
6a6dae7
 
 
 
 
4036c64
6ef6fac
 
 
 
9143c92
6ef6fac
 
4036c64
 
 
6ef6fac
 
 
 
 
 
4036c64
5b35c1a
765743d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a6dae7
5b35c1a
 
 
 
 
 
 
6ef6fac
53152e8
6887e0b
765743d
6ef6fac
 
 
 
765743d
6887e0b
767e1e6
f85e35e
6ef6fac
767e1e6
 
 
 
6ef6fac
6887e0b
6ef6fac
 
765743d
6887e0b
767e1e6
 
25d03a1
 
53152e8
25d03a1
53152e8
62db76d
 
53152e8
62db76d
53152e8
 
25d03a1
 
 
 
e38d1b9
25d03a1
 
e38d1b9
 
 
d70bdf4
e38d1b9
 
25d03a1
e38d1b9
25d03a1
 
 
 
 
 
53152e8
25d03a1
e38d1b9
25d03a1
 
 
 
 
 
 
 
 
53152e8
 
 
25d03a1
 
53152e8
 
 
 
 
 
 
6ef6fac
53152e8
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import streamlit as st
import numpy as np
import pandas as pd
import streamlit as st
import gspread
import pymongo

st.set_page_config(layout="wide")

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

        credentials = {
          "type": "service_account",
          "project_id": "model-sheets-connect",
          "private_key_id": st.secrets['model_sheets_connect_pk'],
          "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDiu1v/e6KBKOcK\ncx0KQ23nZK3ZVvADYy8u/RUn/EDI82QKxTd/DizRLIV81JiNQxDJXSzgkbwKYEDm\n48E8zGvupU8+Nk76xNPakrQKy2Y8+VJlq5psBtGchJTuUSHcXU5Mg2JhQsB376PJ\nsCw552K6Pw8fpeMDJDZuxpKSkaJR6k9G5Dhf5q8HDXnC5Rh/PRFuKJ2GGRpX7n+2\nhT/sCax0J8jfdTy/MDGiDfJqfQrOPrMKELtsGHR9Iv6F4vKiDqXpKfqH+02E9ptz\nBk+MNcbZ3m90M8ShfRu28ebebsASfarNMzc3dk7tb3utHOGXKCf4tF8yYKo7x8BZ\noO9X4gSfAgMBAAECggEAU8ByyMpSKlTCF32TJhXnVJi/kS+IhC/Qn5JUDMuk4LXr\naAEWsWO6kV/ZRVXArjmuSzuUVrXumISapM9Ps5Ytbl95CJmGDiLDwRL815nvv6k3\nUyAS8EGKjz74RpoIoH6E7EWCAzxlnUgTn+5oP9Flije97epYk3H+e2f1f5e1Nn1d\nYNe8U+1HqJgILcxA1TAUsARBfoD7+K3z/8DVPHI8IpzAh6kTHqhqC23Rram4XoQ6\nzj/ZdVBjvnKuazETfsD+Vl3jGLQA8cKQVV70xdz3xwLcNeHsbPbpGBpZUoF73c65\nkAXOrjYl0JD5yAk+hmYhXr6H9c6z5AieuZGDrhmlFQKBgQDzV6LRXmjn4854DP/J\nI82oX2GcI4eioDZPRukhiQLzYerMQBmyqZIRC+/LTCAhYQSjNgMa+ZKyvLqv48M0\n/x398op/+n3xTs+8L49SPI48/iV+mnH7k0WI/ycd4OOKh8rrmhl/0EWb9iitwJYe\nMjTV/QxNEpPBEXfR1/mvrN/lVQKBgQDuhomOxUhWVRVH6x03slmyRBn0Oiw4MW+r\nrt1hlNgtVmTc5Mu+4G0USMZwYuOB7F8xG4Foc7rIlwS7Ic83jMJxemtqAelwOLdV\nXRLrLWJfX8+O1z/UE15l2q3SUEnQ4esPHbQnZowHLm0mdL14qSVMl1mu1XfsoZ3z\nJZTQb48CIwKBgEWbzQRtKD8lKDupJEYqSrseRbK/ax43DDITS77/DWwHl33D3FYC\nMblUm8ygwxQpR4VUfwDpYXBlklWcJovzamXpSnsfcYVkkQH47NuOXPXPkXQsw+w+\nDYcJzeu7F/vZqk9I7oBkWHUrrik9zPNoUzrfPvSRGtkAoTDSwibhoc5dAoGBAMHE\nK0T/ANeZQLNuzQps6S7G4eqjwz5W8qeeYxsdZkvWThOgDd/ewt3ijMnJm5X05hOn\ni4XF1euTuvUl7wbqYx76Wv3/1ZojiNNgy7ie4rYlyB/6vlBS97F4ZxJdxMlabbCW\n6b3EMWa4EVVXKoA1sCY7IVDE+yoQ1JYsZmq45YzPAoGBANWWHuVueFGZRDZlkNlK\nh5OmySmA0NdNug3G1upaTthyaTZ+CxGliwBqMHAwpkIRPwxUJpUwBTSEGztGTAxs\nWsUOVWlD2/1JaKSmHE8JbNg6sxLilcG6WEDzxjC5dLL1OrGOXj9WhC9KX3sq6qb6\nF/j9eUXfXjAlb042MphoF3ZC\n-----END PRIVATE KEY-----\n",
          "client_email": "[email protected]",
          "client_id": "100369174533302798535",
          "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%40model-sheets-connect.iam.gserviceaccount.com"
        }
        
        credentials2 = {
          "type": "service_account",
          "project_id": "sheets-api-connect-378620",
          "private_key_id": st.secrets['sheets_api_connect_pk'],
          "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"
        }
     
        NBA_Data = st.secrets['NBA_Data']

        gc = gspread.service_account_from_dict(credentials)
        gc2 = gspread.service_account_from_dict(credentials2)

        return gc, gc2, NBA_Data
    
gcservice_account, gcservice_account2, NBA_Data = init_conn()

@st.cache_data(ttl=300)
def load_overall_stats():
    try:
        sh = gcservice_account.open_by_url(NBA_Data)
    except:
        sh = gcservice_account2.open_by_url(NBA_Data)  
    
    worksheet = sh.worksheet('DK_Build_Up')
    raw_display = pd.DataFrame(worksheet.get_all_records())
    raw_display.rename(columns={"Name": "Player", "Nickname": "Player", "Fantasy": "Median"}, inplace = True)
    raw_display.replace("", 'Welp', inplace=True)
    raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
    raw_display = raw_display.loc[raw_display['Salary'] > 0]
    raw_display = raw_display.loc[raw_display['Median'] > 0]
    raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
    dk_raw = raw_display.sort_values(by='Median', ascending=False)
    
    worksheet = sh.worksheet('FD_Build_Up')
    raw_display = pd.DataFrame(worksheet.get_all_records())
    raw_display.rename(columns={"Name": "Player", "Nickname": "Player", "Fantasy": "Median"}, inplace = True)
    raw_display.replace("", 'Welp', inplace=True)
    raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
    raw_display = raw_display.loc[raw_display['Median'] > 0]
    raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
    fd_raw = raw_display.sort_values(by='Median', ascending=False)
    
    worksheet = sh.worksheet('Secondary_DK_Build')
    raw_display = pd.DataFrame(worksheet.get_all_records())
    raw_display.rename(columns={"Name": "Player", "Nickname": "Player", "Fantasy": "Median"}, inplace = True)
    raw_display.replace("", 'Welp', inplace=True)
    raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
    raw_display = raw_display.loc[raw_display['Median'] > 0]
    raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
    dk_raw_sec = raw_display.sort_values(by='Median', ascending=False)
    
    worksheet = sh.worksheet('Secondary_FD_Build')
    raw_display = pd.DataFrame(worksheet.get_all_records())
    raw_display.rename(columns={"Name": "Player", "Nickname": "Player", "Fantasy": "Median"}, inplace = True)
    raw_display.replace("", 'Welp', inplace=True)
    raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
    raw_display = raw_display.loc[raw_display['Median'] > 0]
    raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
    fd_raw_sec = raw_display.sort_values(by='Median', ascending=False)

    worksheet = sh.worksheet('Player_Level_ROO')
    raw_display = pd.DataFrame(worksheet.get_all_records())
    raw_display.replace("", 'Welp', inplace=True)
    raw_display = raw_display.loc[raw_display['Player'] != 'Welp']
    raw_display = raw_display.loc[raw_display['Median'] > 0]
    raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
    roo_raw = raw_display.sort_values(by='Median', ascending=False)

    timestamp = raw_display['timestamp'].values[0]

    return dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, timestamp

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

dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, timestamp = load_overall_stats()
t_stamp = f"Last Update: " + str(timestamp) + f" CST"

tab1, tab2 = st.tabs(['Range of Outcomes', 'Uploads and Info'])

with tab1:

    col1, col2 = st.columns([1, 9])

    with col1:
        st.info(t_stamp)
        if st.button("Load/Reset Data", key='reset1'):
              st.cache_data.clear()
              dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, timestamp = load_overall_stats()
              t_stamp = f"Last Update: " + str(timestamp) + f" CST"
              for key in st.session_state.keys():
                  del st.session_state[key]
        site_var2 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var2')
        if site_var2 == 'Draftkings':
            site_baselines = roo_raw[roo_raw['site'] == 'Draftkings']
        elif site_var2 == 'Fanduel':
            site_baselines = roo_raw[roo_raw['site'] == 'Fanduel']
        slate_split = st.radio("Are you viewing the main slate or the secondary slate?", ('Main Slate', 'Secondary'), key='slate_split')
        if slate_split == 'Main Slate':
            raw_baselines = site_baselines[site_baselines['slate'] == 'Main Slate']
        elif slate_split == 'Secondary':
            raw_baselines = site_baselines[site_baselines['slate'] == 'Secondary']
        split_var2 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='split_var2')
        if split_var2 == 'Specific Games':
            team_var2 = st.multiselect('Which teams would you like to include in the ROO?', options = raw_baselines['Team'].unique(), key='team_var2')
        elif split_var2 == 'Full Slate Run':
            team_var2 = raw_baselines.Team.values.tolist()
        pos_var2 = st.selectbox('View specific position?', options = ['All', 'PG', 'SG', 'SF', 'PF', 'C'], key='pos_var2')

    with col2:
        display_container_1 = st.empty()
        display_dl_container_1 = st.empty()
        display_proj = raw_baselines[raw_baselines['Team'].isin(team_var2)]
        display_proj = display_proj.drop(columns=['site', 'version', 'slate', 'timestamp'])
        
        st.session_state.display_proj = display_proj
            
        with display_container_1:
            display_container = st.empty()
            if 'display_proj' in st.session_state:
                if pos_var2 == 'All':
                    st.session_state.display_proj = st.session_state.display_proj
                elif pos_var2 != 'All':
                    st.session_state.display_proj = st.session_state.display_proj[st.session_state.display_proj['Position'].str.contains(pos_var2)]
                st.dataframe(st.session_state.display_proj.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height=1000, use_container_width = True)
        
        with display_dl_container_1:
                display_dl_container = st.empty()
                if 'display_proj' in st.session_state:
                    st.download_button(
                                label="Export Tables",
                                data=convert_df_to_csv(st.session_state.display_proj),
                                file_name='NBA_ROO_export.csv',
                                mime='text/csv',
                    )

with tab2:
    st.info("The Projections file can have any columns in any order, but must contain columns explicitly named: 'Player', 'Salary', 'Position', 'Team', 'Opp', 'Minutes', 'Median', 'Own'.")
    col1, col2 = st.columns([1, 5])

    with col1:
        proj_file = st.file_uploader("Upload Projections File", key = 'proj_uploader')
    
        if proj_file is not None:
                  try:
                            proj_dataframe = pd.read_csv(proj_file)
                  except:
                            proj_dataframe = pd.read_excel(proj_file)
    with col2:
        if proj_file is not None:  
                  st.dataframe(proj_dataframe.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)