James McCool commited on
Commit
cd79eb2
·
1 Parent(s): 87b7422

Refactor player selection handling in Handbuilder tab of app.py to improve session state management and enhance user experience by separating lineup display and player selection editor.

Browse files
Files changed (1) hide show
  1. app.py +32 -19
app.py CHANGED
@@ -802,16 +802,12 @@ with tab3:
802
  with tab4:
803
  st.header("Handbuilder")
804
 
805
- # Prepare the player selection DataFrame
806
- player_select_df = dk_roo[['Player', 'Position', 'Team', 'Salary', 'Median', 'Ceiling', 'Own%']].copy()
807
- player_select_df['Select'] = False # Add a checkbox column
808
-
809
- # Use session state to persist selections
810
  if 'handbuilder_select' not in st.session_state:
811
- st.session_state.handbuilder_select = player_select_df
 
812
 
813
- # Data editor for player selection
814
- st.subheader("Player Select")
815
  edited_df = st.data_editor(
816
  st.session_state.handbuilder_select,
817
  column_config={
@@ -826,16 +822,33 @@ with tab4:
826
  key="handbuilder_editor"
827
  )
828
 
829
- # Update session state with the edited DataFrame
830
- st.session_state.handbuilder_select = edited_df
 
831
 
832
  # Filter selected players for the lineup
833
- selected_players = edited_df[edited_df['Select'] == True]
834
-
835
- # Display the selected lineup
836
- st.subheader("Lineup")
837
- st.dataframe(
838
- selected_players.drop(columns=['Select']),
839
- use_container_width=True,
840
- height=500
841
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
802
  with tab4:
803
  st.header("Handbuilder")
804
 
805
+ # Only initialize once
 
 
 
 
806
  if 'handbuilder_select' not in st.session_state:
807
+ st.session_state.handbuilder_select = dk_roo[['Player', 'Position', 'Team', 'Salary', 'Median', 'Ceiling', 'Own%']].copy()
808
+ st.session_state.handbuilder_select['Select'] = False
809
 
810
+ # Use the session state DataFrame as the source
 
811
  edited_df = st.data_editor(
812
  st.session_state.handbuilder_select,
813
  column_config={
 
822
  key="handbuilder_editor"
823
  )
824
 
825
+ # Update session state only if there are changes
826
+ if not edited_df.equals(st.session_state.handbuilder_select):
827
+ st.session_state.handbuilder_select = edited_df
828
 
829
  # Filter selected players for the lineup
830
+ selected_players = st.session_state.handbuilder_select[st.session_state.handbuilder_select['Select'] == True]
831
+
832
+ col1, col2 = st.columns([1, 2])
833
+ with col1:
834
+ st.subheader("Lineup")
835
+ st.dataframe(
836
+ selected_players.drop(columns=['Select']),
837
+ use_container_width=True,
838
+ height=500
839
+ )
840
+ with col2:
841
+ st.subheader("Player Select")
842
+ st.data_editor(
843
+ st.session_state.handbuilder_select,
844
+ column_config={
845
+ "Select": st.column_config.CheckboxColumn(
846
+ "Select",
847
+ help="Check to add player to lineup",
848
+ default=False
849
+ )
850
+ },
851
+ use_container_width=True,
852
+ hide_index=True,
853
+ key="handbuilder_editor_view"
854
+ )