James McCool commited on
Commit
ed2145b
·
1 Parent(s): 9274561

Add session state management for player selection in Handbuilder tab of app.py by introducing a unique key to reset selection upon player choice. This enhancement improves user experience by ensuring accurate player selection and preventing stale selections during interactions.

Browse files
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -827,6 +827,8 @@ with tab4:
827
  # --- LINEUP STATE ---
828
  if 'handbuilder_lineup' not in st.session_state:
829
  st.session_state['handbuilder_lineup'] = pd.DataFrame(columns=['Player', 'Team', 'Salary', 'Median', 'Own%'])
 
 
830
 
831
  col1, col2 = st.columns([1, 2])
832
  with col2:
@@ -834,10 +836,11 @@ with tab4:
834
  event = st.dataframe(
835
  player_select_df,
836
  on_select="rerun",
837
- selection_mode=["single-row"]
 
838
  )
839
 
840
- # If a row is selected, add that player to the lineup
841
  if event and "rows" in event.selection and len(event.selection["rows"]) > 0:
842
  idx = event.selection["rows"][0]
843
  player_row = player_select_df.iloc[[idx]][['Player', 'Team', 'Salary', 'Median', 'Own%']]
@@ -847,7 +850,8 @@ with tab4:
847
  [st.session_state['handbuilder_lineup'], player_row],
848
  ignore_index=True
849
  )
850
- # Rerun to reset selection
 
851
  st.rerun()
852
 
853
  with col1:
 
827
  # --- LINEUP STATE ---
828
  if 'handbuilder_lineup' not in st.session_state:
829
  st.session_state['handbuilder_lineup'] = pd.DataFrame(columns=['Player', 'Team', 'Salary', 'Median', 'Own%'])
830
+ if 'handbuilder_select_key' not in st.session_state:
831
+ st.session_state['handbuilder_select_key'] = 0
832
 
833
  col1, col2 = st.columns([1, 2])
834
  with col2:
 
836
  event = st.dataframe(
837
  player_select_df,
838
  on_select="rerun",
839
+ selection_mode=["single-row"],
840
+ key=f"handbuilder_select_{st.session_state['handbuilder_select_key']}"
841
  )
842
 
843
+ # If a row is selected, add that player to the lineup and reset selection
844
  if event and "rows" in event.selection and len(event.selection["rows"]) > 0:
845
  idx = event.selection["rows"][0]
846
  player_row = player_select_df.iloc[[idx]][['Player', 'Team', 'Salary', 'Median', 'Own%']]
 
850
  [st.session_state['handbuilder_lineup'], player_row],
851
  ignore_index=True
852
  )
853
+ # Change the key to reset the selection
854
+ st.session_state['handbuilder_select_key'] += 1
855
  st.rerun()
856
 
857
  with col1: