Spaces:
Running
Running
File size: 2,694 Bytes
e23665a b92949e 12ff34e e23665a b92949e 12ff34e b92949e e4b151d b92949e 6c12e73 7de31fb b92949e 6c12e73 b92949e 9ac987b d0d2f37 9ac987b 7fe9caa 7a1b6ea 63ad7ef 7fe9caa 1a7e9e4 63ad7ef 6c12e73 7fe9caa 63ad7ef 350005a 7d2ca0f 350005a 7d2ca0f |
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 |
import numpy as np
import pandas as pd
import streamlit as st
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.cache_resource(ttl = 60)
def init_baselines():
collection = db["HR_Table"]
cursor = collection.find()
raw_display = pd.DataFrame(cursor)
raw_display.rename(columns={"Names": "Player"}, inplace = True)
raw_frame = raw_display.drop(columns=['_id']).drop_duplicates(subset='Player')
raw_frame = raw_frame[raw_frame['Binom_xHR'] != "#N/A"]
return raw_frame
hr_frame = init_baselines()
st.title("HR Finder Table")
with st.container():
col1, col2, col3 = st.columns(3)
with col1:
disp_options = st.radio("Display options:", options = ['Basics', 'Exclude DFS Info', 'Include DFS Info'], key='display_options')
with col2:
team_options = st.multiselect("Parse Teams:", options = hr_frame.Team.unique(), key = 'team_options')
with col3:
pos_options = st.multiselect("Parse Positions:", options = ['C', '1B', '2B', '3B', 'SS', 'OF'], key = 'pos_options')
if len(team_options) > 0:
hr_frame = hr_frame[hr_frame['Team'].isin(team_options)]
if len(pos_options) > 0:
position_mask = hr_frame['Position'].apply(lambda x: any(pos in x for pos in pos_options))
hr_frame = hr_frame[position_mask]
if disp_options == 'Basics':
st.session_state['disp_frame'] = hr_frame[['Player', 'xHR/PA', 'Opp_xHR/PA', 'BP_Binom_xHR', 'Binom_xHR']]
st.session_state['disp_frame'] = st.session_state['disp_frame'].rename(columns={'xHR/PA': 'Hitter', 'Opp_xHR/PA': 'SP', 'BP_Binom_xHR': 'BP', 'Binom_xHR': 'xHRs'})
st.session_state['disp_frame'] = st.session_state['disp_frame'].sort_values(by='xHRs', ascending=False)
elif disp_options == 'Exclude DFS Info':
st.session_state['disp_frame'] = hr_frame.drop(columns=['Salary', 'Position', 'FD_Position', 'Order', 'NQ_Factor'])
st.session_state['disp_frame'] = st.session_state['disp_frame'].sort_values(by='Binom_xHR', ascending=False)
else:
st.session_state['disp_frame'] = hr_frame.copy()
st.session_state['disp_frame'] = st.session_state['disp_frame'].sort_values(by='Binom_xHR', ascending=False)
st.session_state['disp_frame'] = st.session_state['disp_frame'].set_index('Player', drop = True)
if 'disp_frame' in st.session_state:
st.dataframe(st.session_state['disp_frame'].style.background_gradient(axis=0).background_gradient(cmap = 'RdYlGn').format(precision=2), height=1200, use_container_width = True) |