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 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 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 db = init_conn() ## import global functions 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 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%}'} tab1, tab2 = st.tabs(["Data Load", "Contest Analysis"]) with tab1: col1, col2 = st.columns(2) with col1: 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_select = st.selectbox("Select Sport", ['MLB', 'MMA', 'GOLF', 'NBA', 'NHL', 'WNBA', 'TENNIS'], key='sport_select') type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'], key='type_var') try: contest_names, curr_info = grab_contest_names(db, sport_select, type_var) 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_select, type_var, date_select, contest_name_var, contest_id_map) st.session_state['Contest_file'] = grab_contest_data(sport_select, contest_name_var, contest_id_map, date_select, date_select2) else: pass with col2: 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_select, type_var, 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') else: pass 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_var, st.session_state['player_info'], sport_select) 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([1, 3, 1, 2]) with success_col: st.success('Contest file loaded successfully!') 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_select, type_var, date_select, contest_id_map[contest_name_var], st.session_state['Contest_file']) with message_col: try: st.info(return_message) except: pass st.dataframe(st.session_state['Contest'].head(100)) 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'] with tab2: excluded_cols = ['BaseName', 'EntryCount'] exclude_stacks = ['BaseName', 'EntryCount', 'SP', 'SP1', 'SP2'] 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']) 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))) } # Create a copy of the dataframe for calculations working_df = st.session_state['Contest'].copy() if type_var == 'Classic': working_df['stack'] = working_df.apply( lambda row: Counter( st.session_state['map_dict']['team_map'].get(player, '') for player in row[st.session_state['stack_columns']] if st.session_state['map_dict']['team_map'].get(player, '') != '' ).most_common(1)[0][0] if any(st.session_state['map_dict']['team_map'].get(player, '') for player in row[st.session_state['stack_columns']]) else '', axis=1 ) working_df['stack_size'] = working_df.apply( lambda row: Counter( st.session_state['map_dict']['team_map'].get(player, '') for player in row[st.session_state['stack_columns']] if st.session_state['map_dict']['team_map'].get(player, '') != '' ).most_common(1)[0][1] if any(st.session_state['map_dict']['team_map'].get(player, '') for player in row[st.session_state['stack_columns']]) else '', axis=1 ) working_df['salary'] = working_df.apply(lambda row: sum(st.session_state['salary_dict'].get(player, 0) for player in row[st.session_state['player_columns']]), axis=1) working_df['actual_fpts'] = working_df.apply(lambda row: sum(st.session_state['actual_dict'].get(player, 0) for player in row[st.session_state['player_columns']]), axis=1) working_df['actual_own'] = working_df.apply(lambda row: sum(st.session_state['ownership_dict'].get(player, 0) for player in row[st.session_state['player_columns']]), axis=1) print("Sample row values:") print(working_df.iloc[0][st.session_state['player_columns']]) print("Sample salary calculation:") sample_row = working_df.iloc[0] sample_salary = sum(st.session_state['salary_dict'].get(player, 0) for player in sample_row[st.session_state['player_columns']]) print(f"Sample salary: {sample_salary}") print("Individual player salaries:") for player in sample_row[st.session_state['player_columns']]: salary = st.session_state['salary_dict'].get(player, 0) print(f" {player}: {salary}") 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('sorted').transform('size') working_df['uniques'] = working_df.groupby('BaseName').apply( lambda x: (x['dupes'] == 1).sum() ).reindex(working_df['BaseName']).values working_df['under_5'] = working_df.groupby('BaseName').apply( lambda x: (x['dupes'] <= 5).sum() ).reindex(working_df['BaseName']).values working_df['under_10'] = working_df.groupby('BaseName').apply( lambda x: (x['dupes'] <= 10).sum() ).reindex(working_df['BaseName']).values 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) elif type_var == 'Showdown': working_df['stack'] = working_df.apply( lambda row: Counter( st.session_state['map_dict']['team_map'].get(player, '') for player in row[2:] if st.session_state['map_dict']['team_map'].get(player, '') != '' ).most_common(1)[0][0] if any(st.session_state['map_dict']['team_map'].get(player, '') for player in row[2:]) else '', axis=1 ) working_df['stack_size'] = working_df.apply( lambda row: Counter( st.session_state['map_dict']['team_map'].get(player, '') for player in row[2:] if st.session_state['map_dict']['team_map'].get(player, '') != '' ).most_common(1)[0][1] if any(st.session_state['map_dict']['team_map'].get(player, '') for player in row[2:]) else '', axis=1 ) if sport_select == 'GOLF': working_df['salary'] = working_df.apply(lambda row: sum(st.session_state['salary_dict'].get(player, 0) for player in row), axis=1) working_df['actual_fpts'] = working_df.apply(lambda row: sum(st.session_state['actual_dict'].get(player, 0) for player in row), axis=1) else: # Modified salary calculation with 1.5x multiplier for first player working_df['salary'] = working_df.apply( lambda row: (st.session_state['map_dict']['salary_map'].get(row[2], 0) * 1.5) + sum(st.session_state['map_dict']['salary_map'].get(player, 0) for player in row[3:]), axis=1 ) # Modified actual_fpts calculation with 1.5x multiplier for first player working_df['actual_fpts'] = working_df.apply( lambda row: (st.session_state['actual_dict'].get(row[2], 0) * 1.5) + sum(st.session_state['actual_dict'].get(player, 0) for player in row[3:]), axis=1 ) working_df['actual_own'] = working_df.apply(lambda row: sum(st.session_state['ownership_dict'].get(player, 0) for player in row), axis=1) 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('sorted').transform('size') working_df['uniques'] = working_df.groupby('BaseName').apply( lambda x: (x['dupes'] == 1).sum() ).reindex(working_df['BaseName']).values working_df['under_5'] = working_df.groupby('BaseName').apply( lambda x: (x['dupes'] <= 5).sum() ).reindex(working_df['BaseName']).values working_df['under_10'] = working_df.groupby('BaseName').apply( lambda x: (x['dupes'] <= 10).sum() ).reindex(working_df['BaseName']).values 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) # working_df['stack_size'] = working_df['stack_size'].fillna(1).astype(int) 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'] # Remove any NaN values 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'] # Remove any NaN values 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 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 .background_gradient(axis=0) .background_gradient(cmap='RdYlGn') .format(precision=2), height=500, use_container_width=True, hide_index=True ) else: st.stop() if 'Contest' in st.session_state: with st.container(): tab1, tab2, tab3, tab4, tab5 = st.tabs(['Player Used Info', 'Stack Used Info', 'Stack Size Info', 'General Info', 'Duplication 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 sport_select == 'MLB': pos_select = st.multiselect("Select your position(s)", ['P', 'C', '1B', '2B', '3B', 'SS', 'OF'], key='pos_select') elif sport_select == 'NBA': pos_select = st.multiselect("Select your position(s)", ['PG', 'SG', 'SF', 'PF', 'C'], key='pos_select') elif sport_select == 'WNBA': pos_select = st.multiselect("Select your position(s)", ['PG', 'SG', 'SF', 'PF'], key='pos_select') elif sport_select == 'NFL': pos_select = st.multiselect("Select your position(s)", ['QB', 'RB', 'WR', 'TE', 'DST'], key='pos_select') elif sport_select == 'NHL': pos_select = st.multiselect("Select your position(s)", ['W', 'C', 'D', 'G'], key='pos_select') elif sport_select == 'MMA': pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select') elif sport_select == 'GOLF': pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select') elif sport_select == 'TENNIS': pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], 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 sport_select == 'GOLF': hold_frame['Pos'] = 'G' 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) 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 sport_select == 'GOLF': hold_frame['Pos'] = 'G' 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) 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 sport_select == 'GOLF': hold_frame['Pos'] = 'G' 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) 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) 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) 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) 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) 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) else: st.session_state['stack_size_frame'] = create_stack_size_exposures(st.session_state['display_contest_info'], st.session_state['entry_names']) 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) 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)) 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) 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) 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)