Spaces:
Build error
Build error
James McCool
commited on
Commit
·
771c8ac
1
Parent(s):
256492d
Enhance hitter tab functionality in Streamlit app with position filtering
Browse files- Added a new selection option for player positions in the hitter tab, allowing users to filter data based on specific positions.
- Updated the layout to accommodate an additional column for position selection, improving user interactivity.
- Implemented position-based filtering logic for both DraftKings and FanDuel sites, enhancing data accuracy based on user selections.
- Introduced a loading spinner for league baseline data to improve user experience during data retrieval.
- src/streamlit_app.py +23 -2
src/streamlit_app.py
CHANGED
|
@@ -3,6 +3,7 @@ import numpy as np
|
|
| 3 |
import pandas as pd
|
| 4 |
import pymongo
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
st.set_page_config(layout="wide")
|
| 8 |
|
|
@@ -172,7 +173,7 @@ with pitcher_tab:
|
|
| 172 |
with hitter_tab:
|
| 173 |
with st.container(border = True):
|
| 174 |
st.info('Note: Splits options are available for all baseline tables')
|
| 175 |
-
col1, col2, col3, col4, col5 = st.columns(
|
| 176 |
with col1:
|
| 177 |
site_var_hitter = st.selectbox('Site', ['DraftKings', 'FanDuel'], key = 'site_var_hitter')
|
| 178 |
with col2:
|
|
@@ -180,8 +181,10 @@ with hitter_tab:
|
|
| 180 |
with col3:
|
| 181 |
splits_var_hitter = st.selectbox('Splits', ['Overall', 'RHP', 'LHP'], key = 'splits_var_hitter')
|
| 182 |
with col4:
|
| 183 |
-
|
| 184 |
with col5:
|
|
|
|
|
|
|
| 185 |
if team_type_hitter == 'Specific':
|
| 186 |
team_var_hitter = st.multiselect('Select Teams', slate_hitters['Team'].unique(), key = 'team_var_hitter')
|
| 187 |
else:
|
|
@@ -193,12 +196,27 @@ with hitter_tab:
|
|
| 193 |
|
| 194 |
if team_var_hitter is not None:
|
| 195 |
disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
st.session_state['hitter_disp_frame'] = disp_raw
|
| 197 |
elif table_var_hitter == 'Active Baselines':
|
| 198 |
disp_raw = hitter_info
|
| 199 |
|
| 200 |
if team_var_hitter is not None:
|
| 201 |
disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
st.session_state['hitter_disp_frame'] = disp_raw
|
| 204 |
elif table_var_hitter == 'League Aggregate Baselines':
|
|
@@ -227,6 +245,9 @@ with hitter_tab:
|
|
| 227 |
hitter_disp_container = hitter_disp_container.empty()
|
| 228 |
|
| 229 |
with hitter_disp_container:
|
|
|
|
|
|
|
|
|
|
| 230 |
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)
|
| 231 |
|
| 232 |
with team_tab:
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import pymongo
|
| 5 |
import os
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
st.set_page_config(layout="wide")
|
| 9 |
|
|
|
|
| 173 |
with hitter_tab:
|
| 174 |
with st.container(border = True):
|
| 175 |
st.info('Note: Splits options are available for all baseline tables')
|
| 176 |
+
col1, col2, col3, col4, col5, col6 = st.columns(6)
|
| 177 |
with col1:
|
| 178 |
site_var_hitter = st.selectbox('Site', ['DraftKings', 'FanDuel'], key = 'site_var_hitter')
|
| 179 |
with col2:
|
|
|
|
| 181 |
with col3:
|
| 182 |
splits_var_hitter = st.selectbox('Splits', ['Overall', 'RHP', 'LHP'], key = 'splits_var_hitter')
|
| 183 |
with col4:
|
| 184 |
+
position_var_hitter = st.selectbox('Positions', ['C', '1B', '2B', '3B', 'SS', 'OF'], key = 'position_var_hitter')
|
| 185 |
with col5:
|
| 186 |
+
team_type_hitter = st.selectbox('Do you want to view all teams or Specific ones?', ['All', 'Specific'], key = 'team_type_hitter')
|
| 187 |
+
with col6:
|
| 188 |
if team_type_hitter == 'Specific':
|
| 189 |
team_var_hitter = st.multiselect('Select Teams', slate_hitters['Team'].unique(), key = 'team_var_hitter')
|
| 190 |
else:
|
|
|
|
| 196 |
|
| 197 |
if team_var_hitter is not None:
|
| 198 |
disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]
|
| 199 |
+
if position_var_hitter is not None:
|
| 200 |
+
if site_var_hitter == 'Draftkings':
|
| 201 |
+
position_mask = disp_raw['Position'].apply(lambda x: any(pos in x for pos in position_var_hitter))
|
| 202 |
+
disp_raw = disp_raw[position_mask]
|
| 203 |
+
elif site_var_hitter == 'Fanduel':
|
| 204 |
+
position_mask = disp_raw['FD_Position'].apply(lambda x: any(pos in x for pos in position_var_hitter))
|
| 205 |
+
disp_raw = disp_raw[position_mask]
|
| 206 |
st.session_state['hitter_disp_frame'] = disp_raw
|
| 207 |
elif table_var_hitter == 'Active Baselines':
|
| 208 |
disp_raw = hitter_info
|
| 209 |
|
| 210 |
if team_var_hitter is not None:
|
| 211 |
disp_raw = disp_raw[disp_raw['Team'].isin(team_var_hitter)]
|
| 212 |
+
|
| 213 |
+
if position_var_hitter is not None:
|
| 214 |
+
if site_var_hitter == 'Draftkings':
|
| 215 |
+
position_mask = disp_raw['Position'].apply(lambda x: any(pos in x for pos in position_var_hitter))
|
| 216 |
+
disp_raw = disp_raw[position_mask]
|
| 217 |
+
elif site_var_hitter == 'Fanduel':
|
| 218 |
+
position_mask = disp_raw['FD_Position'].apply(lambda x: any(pos in x for pos in position_var_hitter))
|
| 219 |
+
disp_raw = disp_raw[position_mask]
|
| 220 |
|
| 221 |
st.session_state['hitter_disp_frame'] = disp_raw
|
| 222 |
elif table_var_hitter == 'League Aggregate Baselines':
|
|
|
|
| 245 |
hitter_disp_container = hitter_disp_container.empty()
|
| 246 |
|
| 247 |
with hitter_disp_container:
|
| 248 |
+
if table_var_hitter.isin(['League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines']):
|
| 249 |
+
with st.spinner("Full league baselines can take some time to load", show_time=True):
|
| 250 |
+
time.sleep(5)
|
| 251 |
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)
|
| 252 |
|
| 253 |
with team_tab:
|