File size: 63,386 Bytes
58cea02
 
 
 
91e473e
d765ee8
0841c51
 
38d3f0b
0841c51
a4ba398
 
a707b59
0841c51
 
 
 
 
 
 
a707b59
910ce9f
 
 
 
 
 
0841c51
 
 
910ce9f
69590e2
e04a121
38d3f0b
e04a121
0841c51
dc9501e
869c271
 
 
48da594
869c271
 
 
 
7365d98
629cb6e
ceb4948
869c271
 
629cb6e
 
 
 
 
869c271
 
 
 
84af75a
e533f8c
 
 
77d71e0
e533f8c
 
 
 
 
84af75a
1b1d1f9
 
e533f8c
1b1d1f9
e533f8c
39841d9
 
 
 
 
 
 
 
 
de7aa9f
591ec4f
35472d1
39841d9
de7aa9f
39841d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
591ec4f
39841d9
35472d1
39841d9
9e7f389
209a946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e533f8c
209a946
ccdf83a
 
 
 
209a946
ccdf83a
209a946
ccdf83a
 
209a946
 
 
 
 
 
e533f8c
7e6e154
 
 
 
 
 
 
 
 
 
 
0841c51
58cea02
f76b524
9c7e08b
45a70a9
1748ccd
d18e5a9
8f424e5
0841c51
45351e3
c744629
c6bf99d
31a2d34
58cea02
4ad4038
 
 
 
 
 
a707b59
 
 
 
 
 
8e43993
99e552f
81ad445
8e43993
8261cfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71c647d
7de18e9
71c647d
7829724
 
4127d38
 
d69a1b2
2ab6242
95ba261
7f07fe7
0c721eb
7f07fe7
c4642dd
7f07fe7
7829724
d69a1b2
7829724
237985f
7829724
c1c18f8
7829724
 
e04a121
7829724
8b08b1a
a692d2e
1666aa7
0841c51
41768e4
 
 
 
a34bb65
 
0c721eb
 
cac37db
1b1d1f9
cac37db
 
a34bb65
 
71c647d
6a6220a
a692d2e
41768e4
 
 
 
b5afac7
6a6220a
0c721eb
6a6220a
 
 
 
cac37db
1b1d1f9
cac37db
 
b5afac7
 
71c647d
 
22b5408
8ada274
 
81fc9e2
22b5408
ffe9696
 
22b5408
ffe9696
 
 
 
 
 
 
 
 
e22fb4e
bca2538
 
e22fb4e
 
ffe9696
2e01205
22b5408
928ec6e
a692d2e
0c721eb
a692d2e
 
 
f051d8d
39841d9
f051d8d
a11ceac
 
39841d9
 
0c721eb
35472d1
cff79f7
a11ceac
cff79f7
 
68d3916
e7e2a49
 
 
5b9c82c
 
 
0c721eb
1854e4d
c196cbc
3e52c68
2cd1df4
 
3597da4
448fa4e
 
a4e3e2e
2cd1df4
1854e4d
28939d0
a4e3e2e
9f87d22
 
 
 
 
28939d0
448fa4e
2cd1df4
39e3915
448fa4e
 
 
 
 
 
f65755c
448fa4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d765ee8
448fa4e
 
 
 
 
d765ee8
448fa4e
 
 
a4e3e2e
448fa4e
76d511e
7e42d3a
448fa4e
 
 
 
 
 
 
 
 
 
 
6d1503d
3eae767
6d1503d
16fbcab
6db62f0
f49d54b
f56fa41
9887c91
31e306d
7ee8722
 
31e306d
 
 
 
 
 
 
 
7ee8722
31e306d
7ee8722
31e306d
 
 
 
 
 
9887c91
e533f8c
f56fa41
f65755c
448fa4e
 
 
f56fa41
d92d150
 
 
 
 
 
 
 
 
 
448fa4e
 
 
 
 
f65755c
448fa4e
 
 
5370c50
448fa4e
 
 
 
5370c50
448fa4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4e3e2e
448fa4e
f56fa41
7e42d3a
448fa4e
 
 
 
 
 
 
 
 
 
 
3eae767
 
6d1503d
f56fa41
 
 
16fbcab
9887c91
31e306d
7ee8722
 
31e306d
 
 
 
 
 
 
 
7ee8722
31e306d
7ee8722
31e306d
 
 
 
 
 
9887c91
e533f8c
448fa4e
 
a4e3e2e
1817a5f
a4e3e2e
1cbe27c
1599cba
448fa4e
0d01fa6
f051d8d
 
 
 
 
 
 
066ac05
0c721eb
149c996
 
0c721eb
f344ebf
6e8cffc
886a898
e025a7c
b27b8d0
 
 
 
 
c7440a1
 
 
 
 
 
 
 
 
 
 
 
1cbe27c
c7440a1
448fa4e
294b6d5
 
 
886a898
6e8cffc
1599cba
4ac617e
b9319bd
 
4ac617e
b9319bd
 
4ac617e
b9319bd
 
4ac617e
b9319bd
 
4ac617e
b9319bd
544cf42
1599cba
 
544cf42
16aefb8
6e8cffc
 
 
 
 
cf18cbb
 
 
 
 
 
 
 
8aaea01
a4e3e2e
 
 
 
 
 
 
 
 
1599cba
3eb2035
 
a4e3e2e
 
2e01205
3e52c68
6e8cffc
 
 
 
 
 
a4e3e2e
6e8cffc
a19edd8
6e8cffc
 
 
 
 
 
 
 
 
 
 
 
a19edd8
6e8cffc
 
 
a19edd8
 
9da8f46
a19edd8
857c2eb
6e8cffc
 
 
 
a4e3e2e
a707b59
6e8cffc
 
 
 
 
 
 
3e52c68
 
8c3816f
e8a3351
6e8cffc
1b1d1f9
6e8cffc
c9fd1e3
 
 
 
 
 
 
2ab6242
 
 
c9fd1e3
8261cfd
c9fd1e3
8261cfd
c9fd1e3
8261cfd
c9fd1e3
8261cfd
c9fd1e3
8261cfd
c9fd1e3
8261cfd
dc0d88b
27c2660
 
c9fd1e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45351e3
00f9a6c
c744629
8261cfd
c744629
8261cfd
181beb3
c744629
 
 
 
 
 
 
c9fd1e3
4526102
6e8cffc
45351e3
df8ffd8
45351e3
 
8261cfd
45351e3
8261cfd
181beb3
45351e3
 
 
 
 
 
 
 
 
 
 
 
4526102
8e622a8
45351e3
 
8261cfd
45351e3
8261cfd
181beb3
45351e3
 
 
 
 
 
 
 
 
 
 
 
4526102
6e8cffc
c744629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4526102
6e8cffc
c744629
 
 
 
 
 
 
4526102
c744629
 
 
 
 
 
 
4526102
6e8cffc
c6bf99d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4526102
6e8cffc
c6bf99d
 
 
 
 
 
 
4526102
c6bf99d
 
3c22444
c6bf99d
 
 
dc6fa4a
c6bf99d
4526102
8f424e5
31a2d34
 
 
 
 
 
 
 
 
 
 
 
 
 
06c79a5
4526102
8f424e5
31a2d34
 
 
4526102
31a2d34
 
 
4526102
8f424e5
b36408f
d415f18
 
 
 
 
a4e3e2e
d415f18
 
 
 
 
 
fd1c071
 
 
 
 
 
 
31a2d34
d415f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eabaa7d
d415f18
 
 
 
eabaa7d
d415f18
 
 
 
 
 
 
2ceda65
d415f18
219079c
1b1d1f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
749d0aa
 
 
 
 
420ea34
1b1d1f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e6e154
 
 
81ad445
1b1d1f9
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
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("""
<style>
    /* Tab styling */
    .stElementContainer [data-baseweb="button-group"] {
        gap: 2.000rem;
        padding: 4px;
    }
    .stElementContainer [kind="segmented_control"] {
        height: 2.000rem;
        white-space: pre-wrap;
        background-color: #DAA520;
        color: white;
        border-radius: 20px;
        gap: 1px;
        padding: 10px 20px;
        font-weight: bold;
        transition: all 0.3s ease;
    }
    .stElementContainer [kind="segmented_controlActive"] {
        height: 3.000rem;
        background-color: #DAA520;
        border: 3px solid #FFD700;
        border-radius: 10px;
        color: black;
    }
    .stElementContainer [kind="segmented_control"]:hover {
        background-color: #FFD700;
        cursor: pointer;
    }

    div[data-baseweb="select"] > div {
        background-color: #DAA520;
        color: white;
    }

</style>""", 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')