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

Enhance player selection and lineup management in Handbuilder tab of Streamlit app by introducing dedicated buttons for adding and removing players. Update selection mode to 'single' for clarity, and improve feedback messages for user actions. Streamline the process of managing player slots and ensure better state management during updates.

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +55 -43
src/streamlit_app.py CHANGED
@@ -616,43 +616,52 @@ 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
  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,26 +706,29 @@ if selected_tab == 'Handbuilder':
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:
 
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 with single row selection
621
  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",
625
  key="player_select_dataframe",
626
  height=500,
627
  hide_index=True
628
  )
629
+
630
+ # Add Player button
631
+ if st.button("Add Selected Player to Lineup", key="add_player_button"):
632
+ if event and "rows" in event.selection and len(event.selection["rows"]) > 0:
633
+ idx = event.selection["rows"][0]
634
+ player_row = st.session_state['player_select_df'].iloc[[idx]]
635
+ eligible_positions = re.split(r'[/, ]+', player_row['Position'].iloc[0])
636
+
637
+ # Find the first eligible slot that is not full
638
+ slot_to_fill = None
639
+ for slot in ['QB', 'RB', 'WR', 'TE', 'UTIL', 'DST']:
640
+ if slot_counts.get(slot, 0) < position_limits.get(slot, 0):
641
+ if slot == 'UTIL':
642
+ if 'DST' not in eligible_positions and 'QB' not in eligible_positions:
643
+ slot_to_fill = slot
644
+ break
645
+ elif slot in eligible_positions:
646
  slot_to_fill = slot
647
  break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
648
 
649
+ if slot_to_fill is not None:
650
+ # Avoid duplicates
651
+ if not player_row['Player'].iloc[0] in st.session_state['handbuilder_lineup']['Player'].values:
652
+ # Add the slot info
653
+ player_row = player_row.assign(Slot=slot_to_fill)
654
+ st.session_state['handbuilder_lineup'] = pd.concat(
655
+ [st.session_state['handbuilder_lineup'], player_row[['Player', 'Position', 'Team', 'Salary', 'Median', '2x%', 'Own', 'Slot']]],
656
+ ignore_index=True
657
+ )
658
+ st.success(f"Added {player_row['Player'].iloc[0]} to {slot_to_fill} slot")
659
+ else:
660
+ st.warning(f"{player_row['Player'].iloc[0]} is already in the lineup")
661
+ else:
662
+ st.error("No available slots for this player")
663
+ else:
664
+ st.warning("Please select a player first")
665
 
666
  with handbuilder_lineup_build_column:
667
  st.subheader("Lineup Build")
 
706
 
707
  st.session_state['lineup_display_df'] = pd.DataFrame(display_rows, columns=display_columns)
708
 
709
+ # Show the lineup table as a static display
710
+ st.dataframe(
711
  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),
 
 
 
712
  height=445,
713
  hide_index=True
714
  )
715
+
716
+ # Remove Player button
717
+ if st.button("Remove Selected Player from Lineup", key="remove_player_button"):
718
+ # For removal, we can use a simple dropdown or text input
719
+ if not filled_lineup.empty:
720
+ player_to_remove = st.selectbox(
721
+ "Select player to remove:",
722
+ options=filled_lineup['Player'].tolist(),
723
+ key="remove_player_selectbox"
724
+ )
725
+ if st.button("Confirm Remove", key="confirm_remove_button"):
726
+ st.session_state['handbuilder_lineup'] = st.session_state['handbuilder_lineup'][
727
+ st.session_state['handbuilder_lineup']['Player'] != player_to_remove
728
+ ]
729
+ st.success(f"Removed {player_to_remove} from lineup")
730
+ else:
731
+ st.warning("No players in lineup to remove")
732
 
733
  # --- SUMMARY ROW ---
734
  if not filled_lineup.empty: