Spaces:
Running
Running
James McCool
Enhance data processing in app.py by removing duplicate player entries in final line and power play combinations, ensuring cleaner and more accurate data presentation.
08a2ebe
import streamlit as st | |
st.set_page_config(layout="wide") | |
for name in dir(): | |
if not name.startswith('_'): | |
del globals()[name] | |
import pulp | |
import numpy as np | |
import pandas as pd | |
import streamlit as st | |
import gspread | |
import pymongo | |
from itertools import combinations | |
def init_conn(): | |
uri = st.secrets['mongo_uri'] | |
client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000) | |
db = client["NHL_Database"] | |
return db | |
db = init_conn() | |
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}', | |
'4x%': '{:.2%}'} | |
def player_stat_table(): | |
collection = db["Player_Level_ROO"] | |
cursor = collection.find() | |
player_frame = pd.DataFrame(cursor) | |
player_frame = player_frame[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own', | |
'Small Field Own%', 'Large Field Own%', 'Cash Own%', 'CPT_Own', 'Site', 'Type', 'Slate', 'player_id', 'timestamp']] | |
collection = db["Player_Lines_ROO"] | |
cursor = collection.find() | |
line_frame = pd.DataFrame(cursor) | |
line_frame = line_frame[['Player', 'SK1', 'SK2', 'SK3', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '50+%', '2x%', '3x%', '4x%', 'Own', 'Site', 'Type', 'Slate']] | |
collection = db["Player_Powerplay_ROO"] | |
cursor = collection.find() | |
pp_frame = pd.DataFrame(cursor) | |
pp_frame = pp_frame[['Player', 'SK1', 'SK2', 'SK3', 'SK4', 'SK5', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '75+%', '2x%', '3x%', '4x%', 'Own', 'Site', 'Type', 'Slate']] | |
timestamp = player_frame['timestamp'].values[0] | |
return player_frame, line_frame, pp_frame, timestamp | |
def convert_df_to_csv(df): | |
return df.to_csv().encode('utf-8') | |
player_frame, line_frame, pp_frame, timestamp = player_stat_table() | |
t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
tab1, tab2, tab3 = st.tabs(["Player Range of Outcomes", "Line Combo Range of Outcomes", "Power Play Range of Outcomes"]) | |
with tab1: | |
col1, col2 = st.columns([1, 7]) | |
with col1: | |
st.info(t_stamp) | |
if st.button("Load/Reset Data", key='reset1'): | |
st.cache_data.clear() | |
player_frame, line_frame, pp_frame, timestamp = player_stat_table() | |
t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
site_var1 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var1') | |
main_var1 = st.radio("Main slate or secondary slate?", ('Main Slate', 'Secondary Slate'), key='main_var1') | |
split_var1 = st.radio("Would you like to view the whole slate or just specific games?", ('Full Slate Run', 'Specific Games'), key='split_var1') | |
if split_var1 == 'Specific Games': | |
team_var1 = st.multiselect('Which teams would you like to include in the ROO?', options = player_frame['Team'].unique(), key='team_var1') | |
elif split_var1 == 'Full Slate Run': | |
team_var1 = player_frame.Team.values.tolist() | |
pos_split1 = st.radio("Are you viewing all positions, specific groups, or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split1') | |
if pos_split1 == 'Specific Positions': | |
pos_var1 = st.multiselect('What Positions would you like to view?', options = ['C', 'W', 'D', 'G']) | |
elif pos_split1 == 'All Positions': | |
pos_var1 = 'All' | |
sal_var1 = st.slider("Is there a certain price range you want to view?", 2000, 10000, (2000, 20000), key='sal_var1') | |
with col2: | |
final_Proj = player_frame[player_frame['Site'] == str(site_var1)] | |
final_Proj = final_Proj[final_Proj['Type'] == 'Basic'] | |
final_Proj = final_Proj[final_Proj['Slate'] == main_var1] | |
final_Proj = final_Proj[player_frame['Team'].isin(team_var1)] | |
final_Proj = final_Proj[final_Proj['Salary'] >= sal_var1[0]] | |
final_Proj = final_Proj[final_Proj['Salary'] <= sal_var1[1]] | |
if pos_var1 != 'All': | |
final_Proj = final_Proj[final_Proj['Position'].str.contains('|'.join(pos_var1))] | |
final_Proj = final_Proj.sort_values(by='Median', ascending=False) | |
if pos_var1 == 'All': | |
final_Proj = final_Proj.sort_values(by='Median', ascending=False) | |
st.dataframe(final_Proj.iloc[:, :-3].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), use_container_width = True) | |
st.download_button( | |
label="Export Tables", | |
data=convert_df_to_csv(final_Proj), | |
file_name='NHL_player_export.csv', | |
mime='text/csv', | |
) | |
with tab2: | |
col1, col2 = st.columns([1, 7]) | |
with col1: | |
st.info(t_stamp) | |
if st.button("Load/Reset Data", key='reset2'): | |
st.cache_data.clear() | |
player_frame, line_frame, pp_frame, timestamp = player_stat_table() | |
t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
site_var2 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var2') | |
main_var2 = st.radio("Main slate or secondary slate?", ('Main Slate', 'Secondary Slate'), key='main_var2') | |
with col2: | |
final_line_combos = line_frame[line_frame['Site'] == str(site_var2)] | |
final_line_combos = final_line_combos[final_line_combos['Type'] == 'Basic'] | |
final_line_combos = final_line_combos[final_line_combos['Slate'] == main_var2] | |
final_line_combos = final_line_combos.drop_duplicates(subset=['Player']) | |
final_line_combos = final_line_combos.sort_values(by='Median', ascending=False) | |
st.dataframe(final_line_combos.iloc[:, :-3].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True) | |
st.download_button( | |
label="Export Tables", | |
data=convert_df_to_csv(final_line_combos), | |
file_name='NHL_linecombos_export.csv', | |
mime='text/csv', | |
) | |
with tab3: | |
col1, col2 = st.columns([1, 7]) | |
with col1: | |
st.info(t_stamp) | |
if st.button("Load/Reset Data", key='reset3'): | |
st.cache_data.clear() | |
player_frame, line_frame, pp_frame, timestamp = player_stat_table() | |
t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
site_var3 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var3') | |
main_var3 = st.radio("Main slate or secondary slate?", ('Main Slate', 'Secondary Slate'), key='main_var3') | |
with col2: | |
final_pp_combos = pp_frame[pp_frame['Site'] == str(site_var3)] | |
final_pp_combos = final_pp_combos[final_pp_combos['Type'] == 'Basic'] | |
final_pp_combos = final_pp_combos[final_pp_combos['Slate'] == main_var3] | |
final_pp_combos = final_pp_combos.drop_duplicates(subset=['Player']) | |
final_pp_combos = final_pp_combos.sort_values(by='Median', ascending=False) | |
st.dataframe(final_pp_combos.iloc[:, :-3].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True) | |
st.download_button( | |
label="Export Tables", | |
data=convert_df_to_csv(final_pp_combos), | |
file_name='NHL_powerplay_export.csv', | |
mime='text/csv', | |
) |