File size: 15,406 Bytes
ab2c04b
5847715
 
ab2c04b
51c1a0b
771c8ac
d5d4d17
 
 
ab2c04b
 
51c1a0b
ab2c04b
 
d5d4d17
ab2c04b
d5d4d17
ab2c04b
 
8f402fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39f346a
f79125f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be3b754
f79125f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256492d
ab2c04b
 
 
72321a2
ab2c04b
 
 
 
 
 
 
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
adce0d1
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
f738bbd
 
 
 
ab2c04b
 
 
 
 
 
 
 
39f346a
ab2c04b
9ca66f9
ab2c04b
 
 
 
 
c256b63
9ca66f9
 
 
 
 
 
93913de
9ca66f9
d5d4d17
ab2c04b
2f31f7d
6e9c2dc
 
 
 
2f31f7d
fb140dc
ab2c04b
2f31f7d
6e9c2dc
 
 
 
2f31f7d
fb140dc
 
ab2c04b
fb140dc
 
 
 
 
 
 
ab2c04b
2a7c2bb
6e9c2dc
2a7c2bb
 
 
 
6e9c2dc
 
 
 
df23dc3
f962e85
fb140dc
ab2c04b
2a7c2bb
 
 
fb140dc
ab2c04b
2a7c2bb
 
 
fb140dc
ab2c04b
2a7c2bb
 
 
2f31f7d
fb140dc
c47e22d
2f31f7d
 
2ea483c
c47e22d
 
 
f79125f
 
c47e22d
 
 
ab2c04b
 
39f346a
ab2c04b
834a92c
ab2c04b
fb140dc
ab2c04b
6e9c2dc
ab2c04b
 
9ca66f9
7a0cd57
834a92c
 
 
 
 
9ca66f9
7a0cd57
9ca66f9
 
 
93913de
834a92c
ab2c04b
 
6e9c2dc
 
 
 
c14bbec
 
86d9bdc
 
5d83f16
 
c14bbec
ab2c04b
6e9c2dc
 
 
 
771c8ac
c14bbec
86d9bdc
 
5d83f16
 
6e9c2dc
ab2c04b
6e9c2dc
 
 
 
 
ab2c04b
6e9c2dc
 
 
 
 
ab2c04b
6e9c2dc
 
 
 
 
86d9bdc
2f31f7d
 
 
7c7d5e6
5a42d4a
834a92c
 
f79125f
 
 
ab2c04b
 
39f346a
ab2c04b
 
 
 
 
 
 
2ea483c
ab2c04b
2ea483c
 
2f31f7d
 
 
7c7d5e6
93913de
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import streamlit as st
import numpy as np
import pandas as pd
import pymongo
import os
import time

st.set_page_config(layout="wide")

@st.cache_resource
def init_conn():
        uri = os.getenv('mongo_uri')
        client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
        db = client["MLB_Database"]

        return db
    
db = init_conn()

st.markdown("""
<style>
    /* Tab styling */
    .stTabs [data-baseweb="tab-list"] {
        gap: 8px;
        padding: 4px;
    }
    .stTabs [data-baseweb="tab"] {
        height: 50px;
        white-space: pre-wrap;
        background-color: #DAA520;
        color: white;
        border-radius: 10px;
        gap: 1px;
        padding: 10px 20px;
        font-weight: bold;
        transition: all 0.3s ease;
    }
    .stTabs [aria-selected="true"] {
        background-color: #DAA520;
        border: 3px solid #FFD700;
        color: white;
    }
    .stTabs [data-baseweb="tab"]:hover {
        background-color: #FFD700;
        cursor: pointer;
    }
    div[data-baseweb="select"] > div {
        background-color: #DAA520;
        color: white;
    }
    div{
        box-sizing: content-box !important;
    }
</style>""", unsafe_allow_html=True)

def paginate_dataframe(df, page_size):
    total_rows = len(df)
    total_pages = (total_rows + page_size -1) // page_size

    if 'current_page' not in st.session_state:
        st.session_state['current_page'] = 0
    
    start_idx = st.session_state['current_page'] * page_size
    end_idx = start_idx + page_size

    current_page_data = df.iloc[start_idx:end_idx]

    return current_page_data, total_pages, total_rows

def display_paginated_table(df, page_size):
    current_page_data, total_pages, total_rows = paginate_dataframe(df, page_size)

    st.dataframe(current_page_data.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').format(precision=2), height = 500, use_container_width = True, hide_index = True)

    col1, col2, col3, col4 = st.columns([1, 2, 2, 1])
    with col1:
        if st.button('⏮️ First', disabled=st.session_state.current_page == 0):
            st.session_state.current_page = 0
            st.rerun()
    with col2:
        if st.button('⬅️ Previous', disabled=st.session_state.current_page == 0):
            st.session_state.current_page -= 1
            st.rerun()
    with col3:
        if st.button('Next ➡️', disabled=st.session_state.current_page >= total_pages - 1):
            st.session_state.current_page += 1
            st.rerun()
    with col4:
        if st.button('Last ⏭️', disabled=st.session_state.current_page >= total_pages - 1):
            st.session_state.current_page = total_pages - 1
            st.rerun()

@st.cache_resource(ttl = 61)
def init_baselines():

    db_pulls = ['Bullpen_Data', 'Hitter_Agg_Merge', 'Hitter_Long_Merge', 'Hitter_Short_Merge', 'Pitcher_Agg_Merge', 'Pitcher_Long_Merge', 'Pitcher_Short_Merge',
                'Slate_Hitters_Merge', 'Slate_Teams_Merge', 'Starting_Pitchers', 'True_AVG_Split', 'Pitcher_Info', 'Hitter_Info']

    for table in db_pulls:
        collection = db[table] 
        cursor = collection.find()
        df = pd.DataFrame(cursor)

        if table == 'Bullpen_Data':
            try:
                bp_data = df.drop(columns = ['_id'])
            except:
                bp_data = df
        elif table == 'Hitter_Agg_Merge':
            try:
                hitter_agg = df.drop(columns = ['_id'])
            except:
                hitter_agg = df
        elif table == 'Hitter_Long_Merge':
            try:
                hitter_long = df.drop(columns = ['_id'])
            except:
                hitter_long = df
        elif table == 'Hitter_Short_Merge':
            try:
                hitter_short = df.drop(columns = ['_id'])
            except:
                hitter_short = df
        elif table == 'Pitcher_Agg_Merge':
            try:
                pitcher_agg = df.drop(columns = ['_id'])
            except:
                pitcher_agg = df
        elif table == 'Pitcher_Long_Merge':
            try:
                pitcher_long = df.drop(columns = ['_id'])
            except:
                pitcher_long = df
        elif table == 'Pitcher_Short_Merge':
            try:
                pitcher_short = df.drop(columns = ['_id'])
            except:
                pitcher_short = df
        elif table == 'Slate_Hitters_Merge':
            try:
                slate_hitters = df.drop(columns = ['_id'])
            except:
                slate_hitters = df
        elif table == 'Slate_Teams_Merge':
            try:
                slate_team = df.drop(columns = ['_id'])
            except:
                slate_team = df
        elif table == 'Starting_Pitchers':  
            try:
                starting_pitchers = df.drop(columns = ['_id'])
            except:
                starting_pitchers = df
        elif table == 'True_AVG_Split':
            try:
                true_avg_split = df.drop(columns = ['_id'])
            except:
                true_avg_split = df
        elif table == 'Pitcher_Info':
            try:
                pitcher_info = df.drop(columns = ['_id'])
            except:
                pitcher_info = df
        elif table == 'Hitter_Info':    
            try:
                hitter_info = df.drop(columns = ['_id'])
            except:
                hitter_info = df

    return bp_data, hitter_agg, hitter_long, hitter_short, pitcher_agg, pitcher_long, pitcher_short, slate_hitters, slate_team, starting_pitchers, true_avg_split, pitcher_info, hitter_info

bp_data, hitter_agg, hitter_long, hitter_short, pitcher_agg, pitcher_long, pitcher_short, slate_hitters, slate_team, starting_pitchers, true_avg_split, pitcher_info, hitter_info = init_baselines()

pitcher_tab, hitter_tab, team_tab = st.tabs(['Pitchers', 'Hitters', 'Team'])

with pitcher_tab:
    with st.container(border = True):
        st.info('Note: Splits options are available for all baseline tables, they do not apply to True AVG, HWSr, or the Overview tables')
        col1, col2, col3, col4, col5 = st.columns(5)
        with col1:
            site_var_sp = st.selectbox('Site', ['DraftKings', 'FanDuel'], key = 'site_var_sp')
        with col2:
            table_var_sp = st.selectbox('Table', ['True AVG Splits', 'HWSr Splits', 'Current Slate Overview', 'Active Baselines', 'League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines'], key = 'table_var_sp')
        with col3:
            splits_var_sp = st.selectbox('Splits', ['RHH', 'LHH', 'Overall'], key = 'splits_var_sp')
        with col4:
            team_type_sp = st.selectbox('Do you want to view all teams or Specific ones?', ['All', 'Specific'], key = 'team_type_sp')
        with col5:
            if team_type_sp == 'Specific':
                team_var_sp = st.multiselect('Select Teams', starting_pitchers['Team'].unique(), key = 'team_var_sp')
            else:
                team_var_sp = None
                st.write('All teams selected')
    
    if table_var_sp == 'True AVG Splits':
        disp_raw = true_avg_split

        if team_var_sp is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_sp)]

        disp_raw = disp_raw[['Player', 'Handedness', 'Team', 'Opp', 'Opp LHH', 'Opp RHH', 'True AVG (LHH)', 'True AVG (RHH)', 'True AVG (Overall)', 'Weighted True AVG']]

    elif table_var_sp == 'HWSr Splits':
        disp_raw = true_avg_split

        if team_var_sp is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_sp)]

        disp_raw = disp_raw[['Player', 'Handedness', 'Team', 'Opp', 'Opp LHH', 'Opp RHH', 'HWSr (LHH)', 'HWSr (RHH)', 'HWSr (Overall)', 'Weighted HWSr']]
        

    elif table_var_sp == 'Current Slate Overview':
        disp_raw = starting_pitchers

        disp_raw = disp_raw.drop(columns = ['Own Adj', 'Perf_Adj'])

        if team_var_sp is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_sp)]

    elif table_var_sp == 'Active Baselines':
        disp_raw = pitcher_info

        if splits_var_sp != 'Overall':
            disp_raw = disp_raw[disp_raw['Set'] == splits_var_sp]
        else:
            disp_raw = disp_raw[disp_raw['Set'] == 'RHH']
        
        if team_var_sp is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_sp)]

        disp_raw = disp_raw[['Names', 'DK_Salary', 'FD_Salary', 'Team', 'Opp', 'Opp_TT', 'Hand', 'K%', 'BB%', 'True AVG', 'xSLG', 'xBA', 'Hits', 'xHRs', 'xHR/PA']]
        positive_set = ['K%']

    elif table_var_sp == 'League Aggregate Baselines':
        disp_raw = pitcher_agg
        disp_raw = disp_raw[disp_raw['Set'] == splits_var_sp]
        disp_raw = disp_raw[['Player', 'PA', 'Hits', 'Singles', 'Doubles', 'Homeruns', 'Strikeoutper', 'Strikeouts', 'Walkper', 'Walks', 'xBA', 'xSLG', 'xwOBA', 'BABIP', 'AVG', 'FB%', 'True_AVG', 'xHits', 'xHRs', 'xHR/PA', 'HWSr']]

    elif table_var_sp == 'League Short Term Baselines':
        disp_raw = pitcher_short
        disp_raw = disp_raw[disp_raw['Set'] == splits_var_sp]
        disp_raw = disp_raw[['Player', 'PA', 'Hits', 'Singles', 'Doubles', 'Homeruns', 'Strikeoutper', 'Strikeouts', 'Walkper', 'Walks', 'xBA', 'xSLG', 'xwOBA', 'BABIP', 'AVG', 'FB%', 'True_AVG', 'xHits', 'xHRs', 'xHR/PA', 'HWSr']]

    elif table_var_sp == 'League Long Term Baselines':
        disp_raw = pitcher_long
        disp_raw = disp_raw[disp_raw['Set'] == splits_var_sp]
        disp_raw = disp_raw[['Player', 'PA', 'Hits', 'Singles', 'Doubles', 'Homeruns', 'Strikeoutper', 'Strikeouts', 'Walkper', 'Walks', 'xBA', 'xSLG', 'xwOBA', 'BABIP', 'AVG', 'FB%', 'True_AVG', 'xHits', 'xHRs', 'xHR/PA', 'HWSr']]

    st.session_state['sp_disp_frame'] = disp_raw
    page_var = len(st.session_state['sp_disp_frame']) / 2
    sp_disp_container = st.container(border = True)
    sp_disp_container = sp_disp_container.empty()
    
    if table_var_sp in (['League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines']):

        with st.spinner("Full league baselines can take some time to load"):
            time.sleep(5)
        display_paginated_table(st.session_state['sp_disp_frame'], 50)
    else:
        with sp_disp_container:
            st.dataframe(st.session_state['sp_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').format(precision=2), height = 500, use_container_width = True, hide_index = True)

with hitter_tab:
    with st.container(border = True):
        st.info('Note: Splits options are available for all baseline tables')
        col1, col2, col3, col4, col5 = st.columns(5)
        with col1:
            site_var_hitter = st.selectbox('Site', ['Draftkings', 'Fanduel'], key = 'site_var_hitter')
        with col2:
            table_var_hitter = st.selectbox('Table', ['Current Slate Overview', 'Active Baselines', 'League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines'], key = 'table_var_hitter')
        with col3:
            splits_var_hitter = st.selectbox('Splits', ['Overall', 'RHP', 'LHP'], key = 'splits_var_hitter')
        with col4:
            position_type_hitter = st.selectbox('Do you want to view all Positions or Specific ones?', ['All', 'Specific'], key = 'position_type_hitter')
            if position_type_hitter == 'Specific':
                position_var_hitter = st.multiselect('Positions', ['C', '1B', '2B', '3B', 'SS', 'OF'], key = 'position_var_hitter')
            else:
                position_var_hitter = None
                st.write('All Positions selected')
        with col5:
            team_type_hitter = st.selectbox('Do you want to view all Teams or Specific ones?', ['All', 'Specific'], key = 'team_type_hitter')
            if team_type_hitter == 'Specific':
                team_var_hitter = st.multiselect('Select Teams', slate_hitters['Team'].unique(), key = 'team_var_hitter')
            else:
                team_var_hitter = None
                st.write('All Teams selected')

    if table_var_hitter == 'Current Slate Overview':
        disp_raw = slate_hitters
        
        if team_var_hitter is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]

        if position_var_hitter:
            position_mask = disp_raw['Position'].apply(lambda x: any(pos in x for pos in position_var_hitter))
            disp_raw = disp_raw[position_mask]
        
        disp_raw = disp_raw[disp_raw['Set'] == site_var_hitter]

    elif table_var_hitter == 'Active Baselines':
        disp_raw = hitter_info
        
        if team_var_hitter is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]
        
        if position_var_hitter:
            position_mask = disp_raw['Position'].apply(lambda x: any(pos in x for pos in position_var_hitter))
            disp_raw = disp_raw[position_mask]
        
        disp_raw = disp_raw.drop(columns = ['DK_SD_Salary', 'FD_SD_Salary', 'DK_Own', 'FD_Own', 'DK_player_ID', 'FD_player_ID', 'Opp_TT'])

    elif table_var_hitter == 'League Aggregate Baselines':
        disp_raw = hitter_agg
        
        if team_var_hitter is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]

    elif table_var_hitter == 'League Short Term Baselines':
        disp_raw = hitter_short
        
        if team_var_hitter is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]

    elif table_var_hitter == 'League Long Term Baselines':
        disp_raw = hitter_long
        
        if team_var_hitter is not None:
            disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]

    st.session_state['hitter_disp_frame'] = disp_raw
    hitter_disp_container = st.container(border = True)
    hitter_disp_container = hitter_disp_container.empty()

    with hitter_disp_container:
        if table_var_hitter in (['League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines']):
            with st.spinner("Full league baselines can take some time to load"):
                time.sleep(7)
            display_paginated_table(st.session_state['hitter_disp_frame'], 50)
        else:
            st.dataframe(st.session_state['hitter_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height = 750, use_container_width = True, hide_index = True)

with team_tab:
    with st.container(border = True):
        col1, col2, col3 = st.columns(3)
        with col1:
            site_var_team= st.selectbox('Site', ['DraftKings', 'FanDuel'], key = 'site_var_team')
        with col2:
            table_var_team = st.selectbox('Table', ['Team Baselines', 'Bullpen Baselines'], key = 'table_var_team')
    
    if table_var_team == 'Team Baselines':
        st.session_state['team_disp_frame'] = slate_team
    elif table_var_team == 'Bullpen Baselines':
        st.session_state['team_disp_frame'] = bp_data

    team_disp_container = st.container(border = True)
    team_disp_container = team_disp_container.empty()

    with team_disp_container:
        st.dataframe(st.session_state['team_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height = 750, use_container_width = True, hide_index = True)