import streamlit as st st.set_page_config(layout="wide") import numpy as np import pandas as pd from rapidfuzz import process, fuzz from collections import Counter from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi from datetime import datetime # Just setting a note here to say that I should attempt to do some memory allocation savings and swap to numpy soon @st.cache_resource def init_conn(): uri = st.secrets['mongo_uri'] client = MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000) db = client['Contest_Information'] return db def grab_contest_names(db, sport, type): if type == 'Classic': db_type = 'reg' elif type == 'Showdown': db_type = 'sd' collection = db[f'{sport}_{db_type}_contest_info'] cursor = collection.find() curr_info = pd.DataFrame(list(cursor)).drop('_id', axis=1) curr_info['Date'] = pd.to_datetime(curr_info['Contest Date'].sort_values(ascending = False)) curr_info['Date'] = curr_info['Date'].dt.strftime('%Y-%m-%d') contest_names = curr_info['Contest Name'] + ' - ' + curr_info['Date'] return contest_names, curr_info def grab_contest_player_info(db, sport, type, contest_date, contest_name, contest_id_map): if type == 'Classic': db_type = 'reg' elif type == 'Showdown': db_type = 'showdown' collection = db[f'{sport}_{db_type}_player_info'] cursor = collection.find() player_info = pd.DataFrame(list(cursor)).drop('_id', axis=1) player_info = player_info[player_info['Contest Date'] == contest_date] player_info = player_info.rename(columns={'Display Name': 'Player'}) player_info = player_info.sort_values(by='Salary', ascending=True).drop_duplicates(subset='Player', keep='first') info_maps = { 'position_dict': dict(zip(player_info['Player'], player_info['Position'])), 'salary_dict': dict(zip(player_info['Player'], player_info['Salary'])), 'team_dict': dict(zip(player_info['Player'], player_info['Team'])), 'opp_dict': dict(zip(player_info['Player'], player_info['Opp'])), 'fpts_avg_dict': dict(zip(player_info['Player'], player_info['Avg FPTS'])) } return player_info, info_maps def grab_contest_payout_info(db, sport, type, contest_date, contest_name, contest_id_map, contest_id): if type == 'Classic': db_type = 'reg' elif type == 'Showdown': db_type = 'showdown' collection = db[f'{sport}_{db_type}_payout_info'] cursor = collection.find() payout_info = pd.DataFrame(list(cursor)).drop('_id', axis=1) payout_info = payout_info[payout_info['Contest Date'] == contest_date] payout_info = payout_info[payout_info['Contest ID'] == contest_id_map[contest_name]] entry_fee = payout_info['Entry Fee'].iloc[0] return payout_info, entry_fee def export_contest_file(db, sport, type, contest_date, contest_id, contest_data): if type == 'Classic': db_type = 'reg' elif type == 'Showdown': db_type = 'showdown' collection = db[f'{sport}_{db_type}_contest_data'] try: cursor = collection.find() contest_import = pd.DataFrame(list(cursor)).drop('_id', axis=1) if contest_id in contest_import['Contest ID'].values: return_message = "Data for this contest already exists, no need to upload, but we appreciate the effort!" return return_message except: contest_import = pd.DataFrame(columns = ['Rank', 'EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Roster Position', '%Drafted', 'FPTS', 'Contest Date', 'Contest ID']) contest_data['Contest Date'] = contest_date contest_data['Contest ID'] = contest_id contest_import = pd.concat([contest_import, contest_data], ignore_index=True) chunk_size = 10000 collection.drop() for i in range(0, len(contest_import), chunk_size): for _ in range(5): try: df_chunk = contest_import.iloc[i:i + chunk_size] collection.insert_many(df_chunk.to_dict('records'), ordered=False) break except Exception as e: print(f"Retry due to error: {e}") return_message = "Contest data uploaded successfully! We appreciate the data!" return return_message def get_payout_for_position(finish_pos, payout_df, dupes_count): """ Calculate payout for a position, handling ties by splitting the combined payout. Args: finish_pos: The finish position (0-indexed) payout_df: DataFrame with payout structure dupes_count: Number of entries that are tied (from the 'dupes' column) """ if dupes_count == 1: # Single position, no tie matching_row = payout_df[ (payout_df['minPosition'] <= finish_pos + 1) & (payout_df['maxPosition'] >= finish_pos + 1) ] if not matching_row.empty: return matching_row.iloc[0]['value'] else: return 0 else: # Handle tie - sum payouts for the range of positions and divide by number of ties # Convert to 1-indexed positions for payout lookup start_pos = finish_pos + 1 # 1-indexed start position end_pos = finish_pos + dupes_count # 1-indexed end position total_payout = 0 for pos in range(start_pos, end_pos + 1): matching_row = payout_df[ (payout_df['minPosition'] <= pos) & (payout_df['maxPosition'] >= pos) ] if not matching_row.empty: total_payout += matching_row.iloc[0]['value'] # Return the split payout return total_payout / dupes_count def color_roi(val): """Color ROI values: green if > 100%, red if < 100%""" if pd.isna(val): return '' if val > 1.0: # Greater than 100% return 'background-color: lightgreen' elif val < 1.0: # Less than 100% return 'background-color: lightcoral' else: # Exactly 100% return 'background-color: lightyellow' db = init_conn() ## import global functions for usages from global_func.load_contest_file import load_contest_file from global_func.create_player_exposures import create_player_exposures from global_func.create_stack_exposures import create_stack_exposures from global_func.create_stack_size_exposures import create_stack_size_exposures from global_func.create_general_exposures import create_general_exposures from global_func.grab_contest_data import grab_contest_data from global_func.create_player_comparison import create_player_comparison from global_func.create_stack_comparison import create_stack_comparison from global_func.create_size_comparison import create_size_comparison from global_func.create_general_comparison import create_general_comparison def is_valid_input(file): if isinstance(file, pd.DataFrame): return not file.empty else: return file is not None # For Streamlit uploader objects def highlight_row_condition(row): if row['BaseName'] == 'Backtesting_upload': return ['background-color: lightgreen'] * len(row) else: return [''] * len(row) player_exposure_format = {'Exposure Overall': '{:.2%}', 'Exposure Top 1%': '{:.2%}', 'Exposure Top 5%': '{:.2%}', 'Exposure Top 10%': '{:.2%}', 'Exposure Top 20%': '{:.2%}'} dupe_format = {'uniques%': '{:.2%}', 'under_5%': '{:.2%}', 'under_10%': '{:.2%}'} roi_format = {'ROI': '{:.2%}', 'Total Fees': '{:.2f}', 'Total Payout': '{:.2f}'} st.markdown(""" """, unsafe_allow_html=True) try: selected_tab = st.segmented_control( "Select Tab", options=["Data Load", "Contest Analysis"], selection_mode='single', default='Data Load', width='stretch', label_visibility='collapsed', key='tab_selector' ) except: selected_tab = st.segmented_control( "Select Tab", options=["Data Load", "Contest Analysis"], selection_mode='single', default='Data Load', label_visibility='collapsed', key='tab_selector' ) if selected_tab == 'Data Load': data_select, contest_upload, portfolio_upload = st.columns(3) with data_select: if st.button('Clear data', key='reset1'): st.session_state.clear() sport_options, date_options = st.columns(2) parse_type = 'Manual' with sport_options: sport_init = st.selectbox("Select Sport", ['NFL', 'MLB', 'MMA', 'GOLF', 'NBA', 'NHL', 'CFB', 'WNBA', 'NAS'], key='sport_init') type_init = st.selectbox("Select Game Type", ['Classic', 'Showdown'], key='type_init') try: contest_names, curr_info = grab_contest_names(db, sport_init, type_init) except: st.error("No contests found for this sport and/or game type") st.stop() with date_options: date_list = curr_info['Date'].sort_values(ascending=False).unique() # date_list = date_list[date_list != pd.Timestamp.today().strftime('%Y-%m-%d')] date_select = st.selectbox("Select Date", date_list, key='date_select') date_select2 = (pd.to_datetime(date_select) + pd.Timedelta(days=1)).strftime('%Y-%m-%d') name_parse = curr_info[curr_info['Date'] == date_select]['Contest Name'].reset_index(drop=True) contest_id_map = dict(zip(name_parse, curr_info[curr_info['Date'] == date_select]['Contest ID'])) date_select = date_select.replace('-', '') date_select2 = date_select2.replace('-', '') contest_name_var = st.selectbox("Select Contest to load", name_parse) if parse_type == 'DB Search': if 'Contest_file_helper' in st.session_state: del st.session_state['Contest_file_helper'] if 'Contest_file' in st.session_state: del st.session_state['Contest_file'] if 'Contest_file' not in st.session_state: if st.button('Load Contest Data', key='load_contest_data'): st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_init, type_init, date_select, contest_name_var, contest_id_map) st.session_state['Contest_file'] = grab_contest_data(sport_init, contest_name_var, contest_id_map, date_select, date_select2) try: st.session_state['payout_info'], st.session_state['entry_fee'] = grab_contest_payout_info(db, sport_init, type_init, date_select, contest_name_var, contest_id_map, contest_id_map[contest_name_var]) except: st.session_state['payout_info'] = None else: pass with contest_upload: st.info(f"If you are manually loading and do not have the results CSV for the contest you selected, you can find it here: https://www.draftkings.com/contest/gamecenter/{contest_id_map[contest_name_var]}#/, or you can initiate a download with this link: https://www.draftkings.com/contest/exportfullstandingscsv/{contest_id_map[contest_name_var]}") if parse_type == 'Manual': if 'Contest_file_helper' in st.session_state: del st.session_state['Contest_file_helper'] if 'Contest_file' in st.session_state: del st.session_state['Contest_file'] if 'Contest_file' not in st.session_state: st.session_state['Contest_upload'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls']) st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_init, type_init, date_select, contest_name_var, contest_id_map) try: st.session_state['Contest_file'] = pd.read_csv(st.session_state['Contest_upload']) except: st.warning('Please upload a Contest CSV') try: st.session_state['payout_info'], st.session_state['entry_fee'] = grab_contest_payout_info(db, sport_init, type_init, date_select, contest_name_var, contest_id_map, contest_id_map[contest_name_var]) except: st.session_state['payout_info'] = None else: pass with portfolio_upload: st.info("If you have a portfolio of lineups, you can upload them here to see how they would have performed against the field") if st.button('Clear portfolio', key='reset2'): st.session_state['portfolio_df'] = None del st.session_state['display_contest_info'] st.session_state['portfolio_file'] = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls']) try: st.session_state['portfolio_df'] = pd.read_csv(st.session_state['portfolio_file']) original_columns = st.session_state['portfolio_df'].columns.tolist() for col in original_columns: st.session_state['portfolio_df'][col] = st.session_state['portfolio_df'][col].astype(str).str.replace(r'\s*\([^)]*\)', '', regex=True).str.strip() st.session_state['portfolio_df']['BaseName'] = 'Backtesting_upload' st.session_state['portfolio_df']['EntryCount'] = len(st.session_state['portfolio_df']) st.session_state['portfolio_df'] = st.session_state['portfolio_df'][['BaseName', 'EntryCount'] + original_columns] if 'display_contest_info' in st.session_state and st.session_state['display_contest_info'][st.session_state['display_contest_info']['BaseName'] == 'Backtesting_upload'].empty: del st.session_state['display_contest_info'] else: pass except: st.session_state['portfolio_df'] = None if 'Contest_file' in st.session_state: st.session_state['Contest'], st.session_state['ownership_df'], st.session_state['actual_df'], st.session_state['entry_list'], check_lineups = load_contest_file(st.session_state['Contest_file'], type_init, st.session_state['player_info'], sport_init, st.session_state['portfolio_df']) st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all') st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True) if st.session_state['Contest'] is not None: success_col, info_col, upload_col, message_col = st.columns([2, 3, 1, 2]) with success_col: st.success('Contest file loaded, please wait for tables to load below before you switch tabs!') with info_col: st.warning("If you have confirmed that the data is correct, you can send the CSV to the database to enrich Paydirt's sources and help us create actionable tools and algorithms >>") with upload_col: if st.button('Send file to Database?', key='export_contest_file'): return_message = export_contest_file(db, sport_init, type_init, date_select, contest_id_map[contest_name_var], st.session_state['Contest_file']) with message_col: try: st.info(return_message) except: pass if 'Contest_file' in st.session_state: st.session_state['ownership_dict'] = dict(zip(st.session_state['ownership_df']['Player'], st.session_state['ownership_df']['Own'])) st.session_state['actual_dict'] = dict(zip(st.session_state['actual_df']['Player'], st.session_state['actual_df']['FPTS'])) st.session_state['salary_dict'] = st.session_state['info_maps']['salary_dict'] st.session_state['team_dict'] = st.session_state['info_maps']['team_dict'] st.session_state['pos_dict'] = st.session_state['info_maps']['position_dict'] excluded_cols = ['BaseName', 'EntryCount'] exclude_stacks = ['BaseName', 'EntryCount', 'SP', 'SP1', 'SP2', 'P1', 'P2', 'RB1', 'RB2', 'DST', 'G'] if 'Contest' in st.session_state and 'display_contest_info' not in st.session_state: st.session_state['player_columns'] = [col for col in st.session_state['Contest'].columns if col not in excluded_cols] st.session_state['stack_columns'] = [col for col in st.session_state['Contest'].columns if col not in exclude_stacks] print(st.session_state['player_columns']) # Vectorized string operations for col in st.session_state['player_columns']: st.session_state['Contest'][col] = st.session_state['Contest'][col].astype(str).str.strip() # Create mapping dictionaries st.session_state['map_dict'] = { 'pos_map': st.session_state['pos_dict'], 'team_map': st.session_state['team_dict'], 'salary_map': st.session_state['salary_dict'], 'own_map': st.session_state['ownership_dict'], 'own_percent_rank': dict(zip(st.session_state['ownership_df']['Player'], st.session_state['ownership_df']['Own'].rank(pct=True))) } working_df = st.session_state['Contest'].copy() # Pre-compute lookup arrays for vectorized operations team_map = st.session_state['map_dict']['team_map'] salary_map = st.session_state['salary_dict'] actual_map = st.session_state['actual_dict'] ownership_map = st.session_state['ownership_dict'] if st.session_state['type_init'] == 'Classic': # Vectorized stack calculation player_teams = working_df[st.session_state['stack_columns']].apply( lambda x: x.map(team_map).fillna('') ) # Vectorized stack and stack_size calculation def get_most_common_team(teams): if teams.empty or teams.isna().all(): return '', 0 non_empty_teams = teams[teams != ''] if len(non_empty_teams) == 0: return '', 0 team_counts = non_empty_teams.value_counts() return team_counts.index[0], team_counts.iloc[0] stack_results = player_teams.apply(get_most_common_team, axis=1) working_df['stack'] = [result[0] for result in stack_results] working_df['stack_size'] = [result[1] for result in stack_results] # Vectorized salary calculation player_salaries = working_df[st.session_state['player_columns']].apply( lambda x: x.map(salary_map).fillna(0) ) working_df['salary'] = player_salaries.sum(axis=1) # Vectorized actual_fpts calculation player_fpts = working_df[st.session_state['player_columns']].apply( lambda x: x.map(actual_map).fillna(0) ) working_df['actual_fpts'] = player_fpts.sum(axis=1) # Vectorized actual_own calculation player_ownership = working_df[st.session_state['player_columns']].apply( lambda x: x.map(ownership_map).fillna(0) ) working_df['actual_own'] = player_ownership.sum(axis=1) # Vectorized duplication calculation working_df['sorted'] = working_df[st.session_state['player_columns']].apply( lambda row: ','.join(sorted(row.values)), axis=1 ) working_df['dupes'] = working_df.groupby(['actual_fpts', 'actual_own', 'salary']).transform('size') # Vectorized unique calculations working_df['uniques'] = working_df.groupby('BaseName')['dupes'].transform( lambda x: (x == 1).sum() ) working_df['under_5'] = working_df.groupby('BaseName')['dupes'].transform( lambda x: (x <= 5).sum() ) working_df['under_10'] = working_df.groupby('BaseName')['dupes'].transform( lambda x: (x <= 10).sum() ) working_df = working_df.sort_values(by='actual_fpts', ascending=False) working_df = working_df.reset_index(drop=True) working_df = working_df.reset_index() working_df['percentile_finish'] = working_df['index'].rank(pct=True) working_df['finish'] = working_df['index'] working_df = working_df.drop(['sorted', 'index'], axis=1) try: # Calculate payouts efficiently by processing each unique fantasy points group working_df['payout'] = 0 # Initialize payout column # Group by actual_fpts since tied entries will have the same fantasy points for fpts, group in working_df.groupby('actual_fpts'): # Get the first row's finish position and dupes count first_row = group.iloc[0] finish_pos = first_row['finish'] dupes_count = first_row['dupes'] if dupes_count == 1: # Single entry - no tie payout = get_payout_for_position(finish_pos, st.session_state['payout_info'], 1) else: # Multiple entries tied - calculate split payout once split_payout = get_payout_for_position(finish_pos, st.session_state['payout_info'], dupes_count) payout = split_payout # Apply the same payout to all rows in this group working_df.loc[group.index, 'payout'] = payout except: pass elif st.session_state['type_init'] == 'Showdown': # Vectorized stack calculation for Showdown player_teams = working_df.iloc[:, 2:].apply( lambda x: x.map(team_map).fillna('') ) # Vectorized stack and stack_size calculation def get_most_common_team(teams): if teams.empty or teams.isna().all(): return '', 0 non_empty_teams = teams[teams != ''] if len(non_empty_teams) == 0: return '', 0 team_counts = non_empty_teams.value_counts() return team_counts.index[0], team_counts.iloc[0] stack_results = player_teams.apply(get_most_common_team, axis=1) working_df['stack'] = [result[0] for result in stack_results] working_df['stack_size'] = [result[1] for result in stack_results] if st.session_state['sport_init'] == 'GOLF': # Vectorized calculations for GOLF player_salaries = working_df.apply( lambda x: x.map(salary_map).fillna(0) ) working_df['salary'] = player_salaries.sum(axis=1) player_fpts = working_df.apply( lambda x: x.map(actual_map).fillna(0) ) working_df['actual_fpts'] = player_fpts.sum(axis=1) else: # Vectorized calculations with 1.5x multiplier for first player first_player_salary = working_df.iloc[:, 2].map(salary_map).fillna(0) * 1.5 other_players_salary = working_df.iloc[:, 3:].apply( lambda x: x.map(salary_map).fillna(0) ).sum(axis=1) working_df['salary'] = first_player_salary + other_players_salary first_player_fpts = working_df.iloc[:, 2].map(actual_map).fillna(0) * 1.5 other_players_fpts = working_df.iloc[:, 3:].apply( lambda x: x.map(actual_map).fillna(0) ).sum(axis=1) working_df['actual_fpts'] = first_player_fpts + other_players_fpts # Vectorized actual_own calculation player_ownership = working_df.apply( lambda x: x.map(ownership_map).fillna(0) ) working_df['actual_own'] = player_ownership.sum(axis=1) # Vectorized duplication calculation working_df['sorted'] = working_df[st.session_state['player_columns']].apply( lambda row: ','.join(sorted(row.values)), axis=1 ) working_df['dupes'] = working_df.groupby(['actual_fpts', 'actual_own', 'salary']).transform('size') # Vectorized unique calculations working_df['uniques'] = working_df.groupby('BaseName')['dupes'].transform( lambda x: (x == 1).sum() ) working_df['under_5'] = working_df.groupby('BaseName')['dupes'].transform( lambda x: (x <= 5).sum() ) working_df['under_10'] = working_df.groupby('BaseName')['dupes'].transform( lambda x: (x <= 10).sum() ) working_df = working_df.sort_values(by='actual_fpts', ascending=False) working_df = working_df.reset_index(drop=True) working_df = working_df.reset_index() working_df['percentile_finish'] = working_df['index'].rank(pct=True) working_df['finish'] = working_df['index'] working_df = working_df.drop(['sorted', 'index'], axis=1) try: # Calculate payouts efficiently by processing each unique fantasy points group working_df['payout'] = 0 # Initialize payout column # Group by actual_fpts since tied entries will have the same fantasy points for fpts, group in working_df.groupby('actual_fpts'): # Get the first row's finish position and dupes count first_row = group.iloc[0] finish_pos = first_row['finish'] dupes_count = first_row['dupes'] if dupes_count == 1: # Single entry - no tie payout = get_payout_for_position(finish_pos, st.session_state['payout_info'], 1) else: # Multiple entries tied - calculate split payout once split_payout = get_payout_for_position(finish_pos, st.session_state['payout_info'], dupes_count) payout = split_payout # Apply the same payout to all rows in this group working_df.loc[group.index, 'payout'] = payout except: pass # Store results st.session_state['field_player_frame'] = create_player_exposures(working_df, st.session_state['player_columns']) st.session_state['field_stack_frame'] = create_stack_exposures(working_df) st.session_state['display_contest_info'] = working_df.copy() st.session_state['contest_info_reset'] = working_df.copy() st.session_state['unique_players'] = pd.unique(st.session_state['display_contest_info'][st.session_state['player_columns']].values.ravel('K')) st.session_state['unique_players'] = [p for p in st.session_state['unique_players'] if p != 'nan'] st.write('Contest data:') st.dataframe(st.session_state['Contest'].head(25)) if st.session_state['portfolio_df'] is not None: st.write('Portfolio data:') st.dataframe(st.session_state['portfolio_df'].head(25)) else: pass if selected_tab == 'Contest Analysis': if 'sport_select' not in st.session_state: st.session_state['sport_select'] = st.session_state['sport_init'] if 'display_contest_info' in st.session_state: with st.expander("Info and filters"): st.info("Note that any filtering here needs to be reset manually, i.e. if you parse down the specific users and want to reset the table, just backtrack your filtering by setting it back to 'All'") clear_col, reset_col, blank_col = st.columns([1, 1, 7]) with clear_col: if st.button('Clear data', key='reset3'): st.session_state.clear() with reset_col: if st.button('Reset filters', key='reset4'): st.session_state['entry_parse_var'] = 'All' st.session_state['entry_names'] = [] st.session_state['low_entries_var'] = 1 st.session_state['high_entries_var'] = 150 st.session_state['stack_parse_var'] = 'All' st.session_state['stack_names'] = [] st.session_state['stack_size_parse_var'] = 'All' st.session_state['stack_size_names'] = [] st.session_state['player_parse_var'] = 'All' st.session_state['player_names'] = [] st.session_state['remove_var'] = 'No' st.session_state['remove_names'] = [] st.session_state['display_contest_info'] = st.session_state['contest_info_reset'].copy() st.session_state['unique_players'] = pd.unique(st.session_state['display_contest_info'][st.session_state['player_columns']].values.ravel('K')) st.session_state['unique_players'] = [p for p in st.session_state['unique_players'] if p != 'nan'] for keys in ['player_frame', 'stack_frame', 'stack_size_frame', 'general_frame', 'duplication_frame', 'player_exp_comp_download', 'stack_exp_comp_download', 'size_exp_comp_download', 'general_exp_comp_download', 'dupe_exp_comp_download']: if keys in st.session_state: del st.session_state[keys] with st.form(key='filter_form'): users_var, entries_var, stack_var, stack_size_var, player_var, remove_var = st.columns(6) with users_var: st.session_state['entry_parse_var'] = st.selectbox("Do you want to view a specific user(s)?", ['All', 'Specific']) st.session_state['entry_names'] = st.multiselect("Select players", options=st.session_state['entry_list'], default=[]) with entries_var: st.session_state['low_entries_var'] = st.number_input("Low end of entries range", min_value=0, max_value=150, value=1) st.session_state['high_entries_var'] = st.number_input("High end of entries range", min_value=0, max_value=150, value=150) with stack_var: st.session_state['stack_parse_var'] = st.selectbox("Do you want to view lineups with specific team(s)?", ['All', 'Specific']) st.session_state['stack_names'] = st.multiselect("Select teams", options=st.session_state['display_contest_info']['stack'].unique(), default=[]) with stack_size_var: st.session_state['stack_size_parse_var'] = st.selectbox("Do you want to view a specific stack size(s)?", ['All', 'Specific']) st.session_state['stack_size_names'] = st.multiselect("Select stack sizes", options=st.session_state['display_contest_info']['stack_size'].unique(), default=[]) with player_var: st.session_state['player_parse_var'] = st.selectbox("Do you want to view lineups with specific player(s)?", ['All', 'Specific']) st.session_state['player_names'] = st.multiselect("Select players to lock", options=st.session_state['unique_players'], default=[]) with remove_var: st.session_state['remove_var'] = st.selectbox("Do you want to remove a specific player(s)?", ['No', 'Yes']) st.session_state['remove_names'] = st.multiselect("Select players to remove", options=st.session_state['unique_players'], default=[]) submitted = st.form_submit_button("Submit") if submitted: if 'player_frame' in st.session_state: del st.session_state['player_frame'] if 'stack_frame' in st.session_state: del st.session_state['stack_frame'] if 'stack_size_frame' in st.session_state: del st.session_state['stack_size_frame'] if 'general_frame' in st.session_state: del st.session_state['general_frame'] if 'duplication_frame' in st.session_state: del st.session_state['duplication_frame'] if 'ROI_frame' in st.session_state: del st.session_state['ROI_frame'] if st.session_state['entry_parse_var'] == 'Specific' and st.session_state['entry_names']: st.session_state['display_contest_info'] = st.session_state['display_contest_info'][st.session_state['display_contest_info']['BaseName'].isin(st.session_state['entry_names'])] if st.session_state['stack_parse_var'] == 'Specific' and st.session_state['stack_names']: st.session_state['display_contest_info'] = st.session_state['display_contest_info'][st.session_state['display_contest_info']['stack'].isin(st.session_state['stack_names'])] if st.session_state['stack_size_parse_var'] == 'Specific' and st.session_state['stack_size_names']: st.session_state['display_contest_info'] = st.session_state['display_contest_info'][st.session_state['display_contest_info']['stack_size'].isin(st.session_state['stack_size_names'])] if st.session_state['player_parse_var'] == 'Specific' and st.session_state['player_names']: mask = st.session_state['display_contest_info'][st.session_state['player_columns']].apply(lambda row: all(player in row.values for player in st.session_state['player_names']), axis=1) st.session_state['display_contest_info'] = st.session_state['display_contest_info'][mask] if st.session_state['remove_var'] == 'Yes' and st.session_state['remove_names']: mask = st.session_state['display_contest_info'][st.session_state['player_columns']].apply(lambda row: any(player in row.values for player in st.session_state['remove_names']), axis=1) st.session_state['display_contest_info'] = st.session_state['display_contest_info'][~mask] if st.session_state['low_entries_var'] and st.session_state['high_entries_var']: st.session_state['display_contest_info'] = st.session_state['display_contest_info'][st.session_state['display_contest_info']['EntryCount'].between(st.session_state['low_entries_var'], st.session_state['high_entries_var'])] if 'display_contest_info' in st.session_state: # Initialize pagination in session state if not exists if 'current_page' not in st.session_state: st.session_state.current_page = 1 # Calculate total pages rows_per_page = 500 total_rows = len(st.session_state['display_contest_info']) total_pages = (total_rows + rows_per_page - 1) // rows_per_page # Create pagination controls in a single row pagination_cols = st.columns([4, 1, 1, 1, 4]) with pagination_cols[1]: if st.button(f"Previous Page"): if st.session_state['current_page'] > 1: st.session_state.current_page -= 1 else: st.session_state.current_page = 1 if 'player_frame' in st.session_state: del st.session_state['player_frame'] if 'stack_frame' in st.session_state: del st.session_state['stack_frame'] with pagination_cols[3]: if st.button(f"Next Page"): st.session_state.current_page += 1 if 'player_frame' in st.session_state: del st.session_state['player_frame'] if 'stack_frame' in st.session_state: del st.session_state['stack_frame'] # Calculate start and end indices for current page start_idx = (st.session_state.current_page - 1) * rows_per_page end_idx = min((st.session_state.current_page) * rows_per_page, total_rows) st.dataframe( st.session_state['display_contest_info'].iloc[start_idx:end_idx].style .apply(highlight_row_condition, axis=1) .background_gradient(axis=0) .background_gradient(cmap='RdYlGn') .format(precision=2), height=500, use_container_width=True, hide_index=True ) else: st.stop() st.download_button(label="Download Contest Info", data=st.session_state['display_contest_info'].to_csv(index=False), file_name="contest_info.csv", mime="text/csv", key='download_contest') if 'Contest' in st.session_state: with st.container(): tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs(['Player Used Info', 'Stack Used Info', 'Stack Size Info', 'General Info', 'Duplication Info', 'ROI Info']) with tab1: player_pos_form_col, player_comp_form_col = st.columns(2) with player_pos_form_col: with st.form(key='player_info_pos_form'): col1, col2 = st.columns(2) with col1: pos_var = st.selectbox("Which position(s) would you like to view?", ['All', 'Specific'], key='pos_var') with col2: if st.session_state['sport_select'] == 'NFL': pos_select = st.multiselect("Select your position(s)", ['QB', 'RB', 'WR', 'TE', 'DST'], key='pos_select') elif st.session_state['sport_select'] == 'MLB': pos_select = st.multiselect("Select your position(s)", ['P', 'C', '1B', '2B', '3B', 'SS', 'OF'], key='pos_select') elif st.session_state['sport_select'] == 'NBA': pos_select = st.multiselect("Select your position(s)", ['PG', 'SG', 'SF', 'PF', 'C'], key='pos_select') elif st.session_state['sport_select'] == 'WNBA': pos_select = st.multiselect("Select your position(s)", ['PG', 'SG', 'SF', 'PF'], key='pos_select') elif st.session_state['sport_select'] == 'NHL': pos_select = st.multiselect("Select your position(s)", ['W', 'C', 'D', 'G'], key='pos_select') elif st.session_state['sport_select'] == 'MMA': pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select') elif st.session_state['sport_select'] == 'GOLF': pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select') elif st.session_state['sport_select'] == 'NAS': pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select') elif st.session_state['sport_select'] == 'CFB': pos_select = st.multiselect("Select your position(s)", ['QB', 'RB', 'WR'], key='pos_select') submitted = st.form_submit_button("Submit") if submitted: if pos_var == 'Specific': pos_select = pos_select else: pos_select = None with player_comp_form_col: with st.form(key='player_exp_comp_form'): col1, col2 = st.columns(2) with col1: comp_player_var = st.selectbox("Would you like to compare with anyone?", ['No', 'Yes'], key='comp_player_var') with col2: comp_player_select = st.multiselect("Select players to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_player_select') submitted = st.form_submit_button("Submit") if submitted: if comp_player_var == 'No': comp_player_select = None else: comp_player_select = comp_player_select if comp_player_var == 'Yes': player_exp_comp = create_player_comparison(st.session_state['display_contest_info'], st.session_state['player_columns'], comp_player_select) hold_frame = player_exp_comp.copy() if st.session_state['sport_select'] == 'GOLF': hold_frame['Pos'] = 'G' elif st.session_state['sport_select'] == 'NAS': hold_frame['Pos'] = 'C' else: hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map']) player_exp_comp.insert(1, 'Pos', hold_frame['Pos']) player_exp_comp = player_exp_comp.dropna(subset=['Pos']) if pos_select: position_mask = player_exp_comp['Pos'].apply(lambda x: any(pos in x for pos in pos_select)) player_exp_comp = player_exp_comp[position_mask] st.dataframe(player_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=player_exp_comp.select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Player Info", data=player_exp_comp.to_csv(index=False), file_name="player_info.csv", mime="text/csv", key='player_exp_comp_download') else: if st.session_state['entry_parse_var'] == 'All': st.session_state['player_frame'] = create_player_exposures(st.session_state['display_contest_info'], st.session_state['player_columns']) hold_frame = st.session_state['player_frame'].copy() if st.session_state['sport_select'] == 'GOLF': hold_frame['Pos'] = 'G' elif st.session_state['sport_select'] == 'NAS': hold_frame['Pos'] = 'NAS' else: hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map']) st.session_state['player_frame'].insert(1, 'Pos', hold_frame['Pos']) st.session_state['player_frame'] = st.session_state['player_frame'].dropna(subset=['Pos']) if pos_select: position_mask = st.session_state['player_frame']['Pos'].apply(lambda x: any(pos in x for pos in pos_select)) st.session_state['player_frame'] = st.session_state['player_frame'][position_mask] st.dataframe(st.session_state['player_frame']. sort_values(by='Exposure Overall', ascending=False). style.background_gradient(cmap='RdYlGn'). format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Player Info", data=st.session_state['player_frame'].to_csv(index=False), file_name="player_info.csv", mime="text/csv", key='player_exp_comp_download') else: st.session_state['player_frame'] = create_player_exposures(st.session_state['display_contest_info'], st.session_state['player_columns'], st.session_state['entry_names']) hold_frame = st.session_state['player_frame'].copy() if st.session_state['sport_select'] == 'GOLF': hold_frame['Pos'] = 'G' elif st.session_state['sport_select'] == 'NAS': hold_frame['Pos'] = 'NAS' else: hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map']) st.session_state['player_frame'].insert(1, 'Pos', hold_frame['Pos']) st.session_state['player_frame'] = st.session_state['player_frame'].dropna(subset=['Pos']) if pos_select: position_mask = st.session_state['player_frame']['Pos'].apply(lambda x: any(pos in x for pos in pos_select)) st.session_state['player_frame'] = st.session_state['player_frame'][position_mask] st.dataframe(st.session_state['player_frame']. sort_values(by='Exposure Overall', ascending=False). style.background_gradient(cmap='RdYlGn'). format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Player Info", data=st.session_state['player_frame'].to_csv(index=False), file_name="player_info.csv", mime="text/csv", key='player_exp_comp_download') with tab2: with st.form(key='stack_exp_comp_form'): col1, col2 = st.columns(2) with col1: comp_stack_var = st.selectbox("Would you like to compare with anyone?", ['No', 'Yes'], key='comp_stack_var') with col2: comp_stack_select = st.multiselect("Select stacks to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_stack_select') submitted = st.form_submit_button("Submit") if submitted: if comp_stack_var == 'No': comp_stack_select = None else: comp_stack_select = comp_stack_select if comp_stack_var == 'Yes': stack_exp_comp = create_stack_comparison(st.session_state['display_contest_info'], comp_stack_select) st.dataframe(stack_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=stack_exp_comp.select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Stack Info", data=stack_exp_comp.to_csv(index=False), file_name="stack_info.csv", mime="text/csv", key='stack_exp_comp_download') else: if st.session_state['entry_parse_var'] == 'All': st.session_state['stack_frame'] = create_stack_exposures(st.session_state['display_contest_info']) st.dataframe(st.session_state['stack_frame']. sort_values(by='Exposure Overall', ascending=False). style.background_gradient(cmap='RdYlGn'). format(formatter='{:.2%}', subset=st.session_state['stack_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Stack Info", data=st.session_state['stack_frame'].to_csv(index=False), file_name="stack_info.csv", mime="text/csv", key='stack_exp_comp_download') else: st.session_state['stack_frame'] = create_stack_exposures(st.session_state['display_contest_info'], st.session_state['entry_names']) st.dataframe(st.session_state['stack_frame']. sort_values(by='Exposure Overall', ascending=False). style.background_gradient(cmap='RdYlGn'). format(formatter='{:.2%}', subset=st.session_state['stack_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Stack Info", data=st.session_state['stack_frame'].to_csv(index=False), file_name="stack_info.csv", mime="text/csv", key='stack_exp_comp_download') with tab3: with st.form(key='size_exp_comp_form'): col1, col2 = st.columns(2) with col1: comp_size_var = st.selectbox("Would you like to compare with anyone?", ['No', 'Yes'], key='comp_size_var') with col2: comp_size_select = st.multiselect("Select sizes to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_size_select') submitted = st.form_submit_button("Submit") if submitted: if comp_size_var == 'No': comp_size_select = None else: comp_size_select = comp_size_select if comp_size_var == 'Yes': size_exp_comp = create_size_comparison(st.session_state['display_contest_info'], comp_size_select) st.dataframe(size_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=size_exp_comp.select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Stack Size Info", data=size_exp_comp.to_csv(index=False), file_name="stack_size_info.csv", mime="text/csv", key='size_exp_comp_download') else: if st.session_state['entry_parse_var'] == 'All': st.session_state['stack_size_frame'] = create_stack_size_exposures(st.session_state['display_contest_info']) st.dataframe(st.session_state['stack_size_frame']. sort_values(by='Exposure Overall', ascending=False). style.background_gradient(cmap='RdYlGn'). format(formatter='{:.2%}', subset=st.session_state['stack_size_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Stack Size Info", data=st.session_state['stack_size_frame'].to_csv(index=False), file_name="stack_size_info.csv", mime="text/csv", key='size_exp_comp_download') else: st.session_state['stack_size_frame'] = create_stack_size_exposures(st.session_state['display_contest_info'], st.session_state['entry_names']) # st.session_state['stack_size_frame']['Player'] = st.session_state['stack_size_frame']['Player'].astype(str) st.dataframe(st.session_state['stack_size_frame']. sort_values(by='Exposure Overall', ascending=False). style.background_gradient(cmap='RdYlGn'). format(formatter='{:.2%}', subset=st.session_state['stack_size_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns), hide_index=True) st.download_button(label="Download Stack Size Info", data=st.session_state['stack_size_frame'].to_csv(index=False), file_name="stack_size_info.csv", mime="text/csv", key='size_exp_comp_download') with tab4: with st.form(key='general_comp_form'): col1, col2 = st.columns(2) with col1: comp_general_var = st.selectbox("Would you like to compare with anyone?", ['No', 'Yes'], key='comp_general_var') with col2: comp_general_select = st.multiselect("Select generals to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_general_select') submitted = st.form_submit_button("Submit") if submitted: if comp_general_var == 'No': comp_general_select = None else: comp_general_select = comp_general_select if comp_general_var == 'Yes': general_comp = create_general_comparison(st.session_state['display_contest_info'], comp_general_select) st.dataframe(general_comp.style.background_gradient(cmap='RdYlGn', axis=1).format(precision=2)) st.download_button(label="Download General Info", data=general_comp.to_csv(index=False), file_name="general_info.csv", mime="text/csv", key='general_exp_comp_download') else: if st.session_state['entry_parse_var'] == 'All': st.session_state['general_frame'] = create_general_exposures(st.session_state['display_contest_info']) st.dataframe(st.session_state['general_frame'].style.background_gradient(cmap='RdYlGn', axis=1).format(precision=2), hide_index=True) st.download_button(label="Download General Info", data=st.session_state['general_frame'].to_csv(index=False), file_name="general_info.csv", mime="text/csv", key='general_exp_comp_download') else: st.session_state['general_frame'] = create_general_exposures(st.session_state['display_contest_info'], st.session_state['entry_names']) st.dataframe(st.session_state['general_frame'].style.background_gradient(cmap='RdYlGn', axis=1).format(precision=2), hide_index=True) st.download_button(label="Download General Info", data=st.session_state['general_frame'].to_csv(index=False), file_name="general_info.csv", mime="text/csv", key='general_exp_comp_download') with tab5: with st.form(key='dupe_form'): col1, col2 = st.columns(2) with col1: user_dupe_var = st.selectbox("Which usage(s) would you like to view?", ['All', 'Specific'], key='user_dupe_var') with col2: user_dupe_select = st.multiselect("Select your user(s)", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='user_dupe_select') submitted = st.form_submit_button("Submit") if submitted: if user_dupe_var == 'Specific': user_dupe_select = user_dupe_select else: user_dupe_select = None if 'duplication_frame' not in st.session_state: dupe_frame = st.session_state['display_contest_info'][['BaseName', 'EntryCount', 'dupes', 'uniques', 'under_5', 'under_10']] dupe_frame['average_dupes'] = dupe_frame['dupes'].mean() dupe_frame['uniques%'] = dupe_frame['uniques'] / dupe_frame['EntryCount'] dupe_frame['under_5%'] = dupe_frame['under_5'] / dupe_frame['EntryCount'] dupe_frame['under_10%'] = dupe_frame['under_10'] / dupe_frame['EntryCount'] dupe_frame = dupe_frame[['BaseName', 'EntryCount', 'average_dupes', 'uniques', 'uniques%', 'under_5', 'under_5%', 'under_10', 'under_10%']].drop_duplicates(subset='BaseName', keep='first') st.session_state['duplication_frame'] = dupe_frame.sort_values(by='uniques%', ascending=False) if user_dupe_var == 'Specific': st.session_state['duplication_frame'] = st.session_state['duplication_frame'][st.session_state['duplication_frame']['BaseName'].isin(user_dupe_select)] # Initialize pagination in session state if not exists if 'dupe_page' not in st.session_state: st.session_state.dupe_page = 1 # Calculate total pages rows_per_page = 50 total_rows = len(st.session_state['duplication_frame']) total_pages = (total_rows + rows_per_page - 1) // rows_per_page # Create pagination controls in a single row pagination_cols = st.columns([4, 1, 1, 1, 4]) with pagination_cols[1]: if st.button(f"Previous Dupes Page"): if st.session_state['dupe_page'] > 1: st.session_state.dupe_page -= 1 with pagination_cols[3]: if st.button(f"Next Dupes Page"): st.session_state.dupe_page += 1 # Calculate start and end indices for current page start_dupe_idx = (st.session_state.dupe_page - 1) * rows_per_page end_dupe_idx = min((st.session_state.dupe_page) * rows_per_page, total_rows) st.dataframe(st.session_state['duplication_frame'].iloc[start_dupe_idx:end_dupe_idx].style. background_gradient(cmap='RdYlGn', subset=['uniques%', 'under_5%', 'under_10%'], axis=0). background_gradient(cmap='RdYlGn', subset=['uniques', 'under_5', 'under_10'], axis=0). format(dupe_format, precision=2), hide_index=True) st.download_button(label="Download Duplication Info", data=st.session_state['duplication_frame'].to_csv(index=False), file_name="duplication_info.csv", mime="text/csv", key='dupe_exp_comp_download') with tab6: if st.session_state['payout_info'] is not None: with st.form(key='ROI_form'): col1, col2 = st.columns(2) with col1: user_ROI_var = st.selectbox("Which user(s) would you like to view?", ['All', 'Specific'], key='user_ROI_var') with col2: user_ROI_select = st.multiselect("Select your user(s)", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='user_ROI_select') submitted = st.form_submit_button("Submit") if submitted: if user_ROI_var == 'Specific': user_ROI_select = user_ROI_select else: user_ROI_select = None if 'ROI_frame' not in st.session_state: roi_frame = st.session_state['display_contest_info'][['BaseName', 'EntryCount', 'finish', 'payout']] roi_frame['Total Fees'] = roi_frame['EntryCount'] * st.session_state['entry_fee'] roi_frame['Total Payout'] = roi_frame.groupby('BaseName')['payout'].transform('sum') roi_frame['ROI'] = (roi_frame['Total Payout'] / roi_frame['Total Fees']) roi_frame = roi_frame[['BaseName', 'EntryCount', 'Total Fees', 'Total Payout', 'ROI']].drop_duplicates(subset='BaseName', keep='first') st.session_state['ROI_frame'] = roi_frame.sort_values(by='Total Payout', ascending=False) if user_ROI_var == 'Specific': st.session_state['ROI_frame'] = st.session_state['ROI_frame'][st.session_state['ROI_frame']['BaseName'].isin(user_ROI_select)] # Initialize pagination in session state if not exists if 'ROI_page' not in st.session_state: st.session_state.ROI_page = 1 # Calculate total pages rows_per_page = 50 total_rows = len(st.session_state['ROI_frame']) total_pages = (total_rows + rows_per_page - 1) // rows_per_page # Create pagination controls in a single row pagination_cols = st.columns([4, 1, 1, 1, 4]) with pagination_cols[1]: if st.button(f"Previous ROI Page"): if st.session_state['ROI_page'] > 1: st.session_state.ROI_page -= 1 with pagination_cols[3]: if st.button(f"Next ROI Page"): st.session_state.ROI_page += 1 # Calculate start and end indices for current page start_ROI_idx = (st.session_state.ROI_page - 1) * rows_per_page end_ROI_idx = min((st.session_state.ROI_page) * rows_per_page, total_rows) st.dataframe(st.session_state['ROI_frame'].iloc[start_ROI_idx:end_ROI_idx].style. applymap(color_roi, subset=['ROI']). background_gradient(cmap='RdYlGn', subset=['Total Fees', 'Total Payout'], axis=0). background_gradient(cmap='RdYlGn', subset=['EntryCount'], axis=0). format(roi_format, precision=2), hide_index=True) st.download_button(label="Download ROI Info", data=st.session_state['ROI_frame'].to_csv(index=False), file_name="ROI_info.csv", mime="text/csv", key='ROI_exp_comp_download') else: st.write('No ROI info available')