James McCool commited on
Commit
633710b
·
1 Parent(s): 263e733

Refactor player selection handling in Handbuilder tab of app.py by removing unnecessary session state variables and simplifying the selection logic. Update the display of selected players to directly reflect current selections, enhancing clarity and user experience.

Browse files
Files changed (1) hide show
  1. app.py +4 -24
app.py CHANGED
@@ -802,12 +802,7 @@ with tab3:
802
  with tab4:
803
  st.header("Handbuilder")
804
 
805
- if 'handbuilder_editor_key' not in st.session_state:
806
- st.session_state.handbuilder_editor_key = 0
807
-
808
  if st.button("Clear Data", key='clear_handbuild'):
809
- st.session_state.handbuilder_editor_key += 1
810
- st.session_state.handbuilder_selected_indices = []
811
  st.rerun()
812
 
813
  # --- TEAM FILTER UI ---
@@ -832,33 +827,18 @@ with tab4:
832
  else:
833
  player_select_df = dk_roo[['Player', 'Position', 'Team', 'Salary', 'Median', 'Order', 'Hand', 'Own%']].copy()
834
 
835
- # --- ROW SELECTION STATE ---
836
- # Map previous selection to the new filtered DataFrame
837
- if 'handbuilder_selected_indices' not in st.session_state:
838
- st.session_state.handbuilder_selected_indices = []
839
-
840
- # Remove indices that are out of range (in case the player pool shrinks)
841
- st.session_state.handbuilder_selected_indices = [
842
- i for i in st.session_state.handbuilder_selected_indices if i < len(player_select_df)
843
- ]
844
-
845
  col1, col2 = st.columns([1, 2])
846
  with col2:
847
  st.subheader("Player Select")
848
  event = st.dataframe(
849
  player_select_df,
850
- key=f"handbuilder_editor_{st.session_state.handbuilder_editor_key}",
851
  on_select="rerun",
852
- selection_mode=["multi-row"],
853
- selected_rows=st.session_state.handbuilder_selected_indices
854
  )
855
 
856
- # Update session state with new selection if changed
857
- if event and "rows" in event.selection:
858
- st.session_state.handbuilder_selected_indices = event.selection["rows"]
859
-
860
- # Get selected rows
861
- select_players_disp = player_select_df.iloc[st.session_state.handbuilder_selected_indices][['Player', 'Team', 'Salary', 'Median', 'Own%']]
862
 
863
  with col1:
864
  st.subheader("Lineup")
 
802
  with tab4:
803
  st.header("Handbuilder")
804
 
 
 
 
805
  if st.button("Clear Data", key='clear_handbuild'):
 
 
806
  st.rerun()
807
 
808
  # --- TEAM FILTER UI ---
 
827
  else:
828
  player_select_df = dk_roo[['Player', 'Position', 'Team', 'Salary', 'Median', 'Order', 'Hand', 'Own%']].copy()
829
 
 
 
 
 
 
 
 
 
 
 
830
  col1, col2 = st.columns([1, 2])
831
  with col2:
832
  st.subheader("Player Select")
833
  event = st.dataframe(
834
  player_select_df,
 
835
  on_select="rerun",
836
+ selection_mode=["multi-row"]
 
837
  )
838
 
839
+ # Get selected rows (if any)
840
+ selected_indices = event.selection["rows"] if event and "rows" in event.selection else []
841
+ select_players_disp = player_select_df.iloc[selected_indices][['Player', 'Team', 'Salary', 'Median', 'Own%']]
 
 
 
842
 
843
  with col1:
844
  st.subheader("Lineup")