James McCool commited on
Commit
d48b6df
·
1 Parent(s): c141e28

reverting handbuilder adjustments

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +40 -49
src/streamlit_app.py CHANGED
@@ -616,43 +616,43 @@ if selected_tab == 'Handbuilder':
616
  handbuilder_lineup_build_column, handbuilder_player_select_column = st.columns([1, 2])
617
  with handbuilder_player_select_column:
618
  st.subheader("Player Select")
619
-
620
- # Display player selection dataframe
621
- player_select_event = st.dataframe(
622
  st.session_state['player_select_df'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Salary', 'Own']).format(precision=2),
623
  on_select="rerun",
624
- selection_mode="single-row", # Single row selection
625
  key="player_select_dataframe",
626
  height=500,
627
  hide_index=True
628
  )
629
-
630
- # Handle player selection - add to lineup
631
- if player_select_event and "rows" in player_select_event.selection:
632
- selected_idx = player_select_event.selection["rows"][0] # Single selection
633
- selected_player_row = st.session_state['player_select_df'].iloc[selected_idx]
634
-
635
- # Check if player is already in lineup
636
- if selected_player_row['Player'] not in st.session_state['handbuilder_lineup']['Player'].values:
637
- eligible_positions = re.split(r'[/, ]+', selected_player_row['Position'])
638
- slot_to_fill = None
639
-
640
- for slot in ['QB', 'RB', 'WR', 'TE', 'UTIL', 'DST']:
641
- if slot_counts.get(slot, 0) < position_limits.get(slot, 0):
642
- if slot == 'UTIL':
643
- if 'DST' not in eligible_positions and 'QB' not in eligible_positions:
644
- slot_to_fill = slot
645
- break
646
- elif slot in eligible_positions:
647
  slot_to_fill = slot
648
  break
649
-
650
- if slot_to_fill is not None:
651
- player_row = selected_player_row.to_frame().T.assign(Slot=slot_to_fill)
 
 
 
 
 
 
652
  st.session_state['handbuilder_lineup'] = pd.concat(
653
  [st.session_state['handbuilder_lineup'], player_row[['Player', 'Position', 'Team', 'Salary', 'Median', '2x%', 'Own', 'Slot']]],
654
  ignore_index=True
655
  )
 
 
656
 
657
  with handbuilder_lineup_build_column:
658
  st.subheader("Lineup Build")
@@ -697,36 +697,27 @@ if selected_tab == 'Handbuilder':
697
 
698
  st.session_state['lineup_display_df'] = pd.DataFrame(display_rows, columns=display_columns)
699
 
700
- # Display the lineup table as an interactive dataframe
701
- lineup_select_event = st.dataframe(
702
  st.session_state['lineup_display_df'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn', subset=['Median']).background_gradient(cmap='RdYlGn_r', subset=['Salary', 'Own']).format(precision=2),
703
  on_select="rerun",
704
- selection_mode="single-row", # Single row selection
705
- key="lineup_display_dataframe",
706
- height=445,
707
- hide_index=True
708
- )
709
-
710
- # Handle lineup selection - remove from lineup
711
- if lineup_select_event and "rows" in lineup_select_event.selection:
712
- selected_idx = lineup_select_event.selection["rows"][0] # Single selection
713
- selected_slot_row = st.session_state['lineup_display_df'].iloc[selected_idx]
714
-
715
- # Only remove if there's actually a player in that slot
716
- if selected_slot_row['Player'] and selected_slot_row['Player'] != '':
717
- player_to_remove = selected_slot_row['Player']
718
- if player_to_remove in st.session_state['handbuilder_lineup']['Player'].values:
719
- st.session_state['handbuilder_lineup'] = st.session_state['handbuilder_lineup'][
720
- st.session_state['handbuilder_lineup']['Player'] != player_to_remove
721
- ]
722
-
723
- # Show the lineup table as a static display (no selection needed)
724
- st.dataframe(
725
- st.session_state['lineup_display_df'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn', subset=['Median']).background_gradient(cmap='RdYlGn_r', subset=['Salary', 'Own']).format(precision=2),
726
  height=445,
727
  hide_index=True
728
  )
729
 
 
 
 
 
 
 
 
 
 
 
 
730
  # --- SUMMARY ROW ---
731
  if not filled_lineup.empty:
732
  total_salary = filled_lineup['Salary'].sum()
 
616
  handbuilder_lineup_build_column, handbuilder_player_select_column = st.columns([1, 2])
617
  with handbuilder_player_select_column:
618
  st.subheader("Player Select")
619
+ event = st.dataframe(
 
 
620
  st.session_state['player_select_df'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Salary', 'Own']).format(precision=2),
621
  on_select="rerun",
622
+ selection_mode=["single-row"],
623
  key="player_select_dataframe",
624
  height=500,
625
  hide_index=True
626
  )
627
+ # If a row is selected, add that player to the lineup and reset selection
628
+ if event and "rows" in event.selection and len(event.selection["rows"]) > 0:
629
+ idx = event.selection["rows"][0]
630
+ player_row = st.session_state['player_select_df'].iloc[[idx]]
631
+ eligible_positions = re.split(r'[/, ]+', player_row['Position'].iloc[0])
632
+ # Find the first eligible slot that is not full
633
+ slot_to_fill = None
634
+
635
+ for slot in ['QB', 'RB', 'WR', 'TE', 'UTIL', 'DST']:
636
+ if slot_counts.get(slot, 0) < position_limits.get(slot, 0):
637
+ if slot == 'UTIL':
638
+ if 'DST' not in eligible_positions and 'QB' not in eligible_positions:
 
 
 
 
 
 
639
  slot_to_fill = slot
640
  break
641
+ elif slot in eligible_positions:
642
+ slot_to_fill = slot
643
+ break
644
+
645
+ if slot_to_fill is not None:
646
+ # Avoid duplicates
647
+ if not player_row['Player'].iloc[0] in st.session_state['handbuilder_lineup']['Player'].values:
648
+ # Add the slot info
649
+ player_row = player_row.assign(Slot=slot_to_fill)
650
  st.session_state['handbuilder_lineup'] = pd.concat(
651
  [st.session_state['handbuilder_lineup'], player_row[['Player', 'Position', 'Team', 'Salary', 'Median', '2x%', 'Own', 'Slot']]],
652
  ignore_index=True
653
  )
654
+ st.rerun()
655
+
656
 
657
  with handbuilder_lineup_build_column:
658
  st.subheader("Lineup Build")
 
697
 
698
  st.session_state['lineup_display_df'] = pd.DataFrame(display_rows, columns=display_columns)
699
 
700
+ # Show the lineup table with single-row selection for removal
701
+ event_remove = st.dataframe(
702
  st.session_state['lineup_display_df'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn', subset=['Median']).background_gradient(cmap='RdYlGn_r', subset=['Salary', 'Own']).format(precision=2),
703
  on_select="rerun",
704
+ selection_mode=["single-row"],
705
+ key="lineup_remove_dataframe",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
706
  height=445,
707
  hide_index=True
708
  )
709
 
710
+ # If a row is selected and not blank, remove that player from the lineup
711
+ if event_remove and "rows" in event_remove.selection and len(event_remove.selection["rows"]) > 0:
712
+ idx = event_remove.selection["rows"][0]
713
+ player_to_remove = st.session_state['lineup_display_df'].iloc[idx]['Player']
714
+ slot_to_remove = st.session_state['lineup_display_df'].iloc[idx]['Slot']
715
+ if player_to_remove: # Only remove if not blank
716
+ st.session_state['handbuilder_lineup'] = filled_lineup[
717
+ ~((filled_lineup['Player'] == player_to_remove) & (filled_lineup['Slot'] == slot_to_remove))
718
+ ]
719
+ st.rerun()
720
+
721
  # --- SUMMARY ROW ---
722
  if not filled_lineup.empty:
723
  total_salary = filled_lineup['Salary'].sum()