import streamlit as st import numpy as np import pandas as pd import pymongo import os 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(""" """, unsafe_allow_html=True) @st.cache_resource() 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': bp_data = df.drop(columns = ['_id']) elif table == 'Hitter_Agg_Merge': hitter_agg = df.drop(columns = ['_id']) elif table == 'Hitter_Long_Merge': hitter_long = df.drop(columns = ['_id']) elif table == 'Hitter_Short_Merge': hitter_short = df.drop(columns = ['_id']) elif table == 'Pitcher_Agg_Merge': pitcher_agg = df.drop(columns = ['_id']) elif table == 'Pitcher_Long_Merge': pitcher_long = df.drop(columns = ['_id']) elif table == 'Pitcher_Short_Merge': pitcher_short = df.drop(columns = ['_id']) elif table == 'Slate_Hitters_Merge': slate_hitters = df.drop(columns = ['_id']) elif table == 'Slate_Teams_Merge': slate_team = df.drop(columns = ['_id']) elif table == 'Starting_Pitchers': starting_pitchers = df.drop(columns = ['_id']) elif table == 'True_AVG_Split': true_avg_split = df.drop(columns = ['_id']) elif table == 'Pitcher_Info': pitcher_info = df.drop(columns = ['_id']) elif table == 'Hitter_Info': hitter_info = df.drop(columns = ['_id']) 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: st.write('All teams selected') if table_var_sp == 'True AVG Splits': disp_raw = true_avg_split disp_raw = disp_raw[['Player', 'Handedness', 'Team', 'Opp', 'Opp LHH', 'Opp RHH', 'True AVG (LHH)', 'True AVG (RHH)', 'True AVG (Overall)', 'Weighted True AVG']] st.session_state['sp_disp_frame'] = disp_raw elif table_var_sp == 'HWSr Splits': disp_raw = true_avg_split disp_raw = disp_raw[['Player', 'Handedness', 'Team', 'Opp', 'Opp LHH', 'Opp RHH', 'HWSr (LHH)', 'HWSr (RHH)', 'HWSr (Overall)', 'Weighted HWSr']] st.session_state['sp_disp_frame'] = disp_raw elif table_var_sp == 'Current Slate Overview': st.session_state['sp_disp_frame'] = starting_pitchers 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'] 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%'] st.session_state['sp_disp_frame'] = disp_raw 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']] st.session_state['sp_disp_frame'] = disp_raw 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']] st.session_state['sp_disp_frame'] = pitcher_short 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'] = pitcher_long sp_disp_container = st.container(border = True) sp_disp_container = sp_disp_container.empty() 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), 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', ['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: team_type_hitter = st.selectbox('Do you want to view all teams or Specific ones?', ['All', 'Specific'], key = 'team_type_hitter') with col5: if team_type_hitter == 'Specific': team_var_hitter = st.multiselect('Select Teams', slate_hitters['Team'].unique(), key = 'team_var_hitter') else: st.write('All teams selected') if table_var_hitter == 'Current Slate Overview': st.session_state['hitter_disp_frame'] = slate_hitters elif table_var_hitter == 'Active Baselines': st.session_state['hitter_disp_frame'] = hitter_info elif table_var_hitter == 'League Aggregate Baselines': st.session_state['hitter_disp_frame'] = hitter_agg elif table_var_hitter == 'League Short Term Baselines': st.session_state['hitter_disp_frame'] = hitter_short elif table_var_hitter == 'League Long Term Baselines': st.session_state['hitter_disp_frame'] = hitter_long hitter_disp_container = st.container(border = True) hitter_disp_container = hitter_disp_container.empty() with hitter_disp_container: st.dataframe(st.session_state['hitter_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), 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), use_container_width = True, hide_index = True)