James McCool commited on
Commit
379368b
·
1 Parent(s): 5628499

Refactor session state management in app.py to enhance portfolio reset functionality and streamline player summary assignment. Introduce logic to toggle 'settings_base' state during form submissions, ensuring accurate handling of player exposures and improving user experience.

Browse files
Files changed (1) hide show
  1. app.py +11 -3
app.py CHANGED
@@ -811,8 +811,8 @@ with tab2:
811
  col1, col2 = st.columns(2)
812
  with col1:
813
  if st.button('Reset Portfolio', key='reset_port'):
 
814
  st.session_state['working_frame'] = st.session_state['base_frame'].copy()
815
- st.session_state['player_summary'] = st.session_state['origin_player_exposures'].copy()
816
 
817
  with col2:
818
  with st.form(key='contest_size_form'):
@@ -994,7 +994,9 @@ with tab2:
994
  stack_remove = st.multiselect("If Specific Stacks, Which to remove?", options=sorted(list(set(stack_dict.values()))), default=[])
995
 
996
  submitted = st.form_submit_button("Submit")
 
997
  if submitted:
 
998
  parsed_frame = st.session_state['working_frame'].copy()
999
  parsed_frame = parsed_frame[parsed_frame['Dupes'] <= max_dupes]
1000
  parsed_frame = parsed_frame[parsed_frame['salary'] >= min_salary]
@@ -1030,6 +1032,7 @@ with tab2:
1030
 
1031
  submitted = st.form_submit_button("Submit")
1032
  if submitted:
 
1033
  parsed_frame = st.session_state['working_frame'].copy()
1034
  if player_remove:
1035
  # Create mask for lineups that contain any of the removed players
@@ -1105,6 +1108,7 @@ with tab2:
1105
 
1106
  submitted = st.form_submit_button("Trim")
1107
  if submitted:
 
1108
  st.write('initiated')
1109
  parsed_frame = st.session_state['working_frame'].copy()
1110
 
@@ -1118,6 +1122,7 @@ with tab2:
1118
  lineup_target = st.number_input("Lineups to produce", value=150, min_value=1, step=1)
1119
  submitted = st.form_submit_button("Submit")
1120
  if submitted:
 
1121
  if preset_choice == 'Small Field (Heavy Own)':
1122
  parsed_frame = small_field_preset(st.session_state['working_frame'], lineup_target, excluded_cols, sport_var)
1123
  elif preset_choice == 'Large Field (Manage Diversity)':
@@ -1243,7 +1248,9 @@ with tab2:
1243
  player_stats = []
1244
  player_columns = [col for col in display_frame.columns if col not in excluded_cols]
1245
 
1246
- if 'player_summary' not in st.session_state:
 
 
1247
  if type_var == 'Showdown':
1248
  for player in player_names:
1249
  # Create mask for lineups where this player is Captain (first column)
@@ -1334,7 +1341,8 @@ with tab2:
1334
  player_summary = pd.DataFrame(player_stats)
1335
  player_summary = player_summary.sort_values('Lineup Count', ascending=False)
1336
  st.session_state['player_summary'] = player_summary.copy()
1337
- st.session_state['origin_player_exposures'] = player_summary.copy()
 
1338
 
1339
  st.subheader("Player Summary")
1340
  st.dataframe(
 
811
  col1, col2 = st.columns(2)
812
  with col1:
813
  if st.button('Reset Portfolio', key='reset_port'):
814
+ st.session_state['settings_base'] = True
815
  st.session_state['working_frame'] = st.session_state['base_frame'].copy()
 
816
 
817
  with col2:
818
  with st.form(key='contest_size_form'):
 
994
  stack_remove = st.multiselect("If Specific Stacks, Which to remove?", options=sorted(list(set(stack_dict.values()))), default=[])
995
 
996
  submitted = st.form_submit_button("Submit")
997
+
998
  if submitted:
999
+ st.session_state['settings_base'] = False
1000
  parsed_frame = st.session_state['working_frame'].copy()
1001
  parsed_frame = parsed_frame[parsed_frame['Dupes'] <= max_dupes]
1002
  parsed_frame = parsed_frame[parsed_frame['salary'] >= min_salary]
 
1032
 
1033
  submitted = st.form_submit_button("Submit")
1034
  if submitted:
1035
+ st.session_state['settings_base'] = False
1036
  parsed_frame = st.session_state['working_frame'].copy()
1037
  if player_remove:
1038
  # Create mask for lineups that contain any of the removed players
 
1108
 
1109
  submitted = st.form_submit_button("Trim")
1110
  if submitted:
1111
+ st.session_state['settings_base'] = False
1112
  st.write('initiated')
1113
  parsed_frame = st.session_state['working_frame'].copy()
1114
 
 
1122
  lineup_target = st.number_input("Lineups to produce", value=150, min_value=1, step=1)
1123
  submitted = st.form_submit_button("Submit")
1124
  if submitted:
1125
+ st.session_state['settings_base'] = False
1126
  if preset_choice == 'Small Field (Heavy Own)':
1127
  parsed_frame = small_field_preset(st.session_state['working_frame'], lineup_target, excluded_cols, sport_var)
1128
  elif preset_choice == 'Large Field (Manage Diversity)':
 
1248
  player_stats = []
1249
  player_columns = [col for col in display_frame.columns if col not in excluded_cols]
1250
 
1251
+ if st.session_state['settings_base'] and 'origin_player_exposures' in st.session_state:
1252
+ st.session_state['player_summary'] = st.session_state['origin_player_exposures']
1253
+ else:
1254
  if type_var == 'Showdown':
1255
  for player in player_names:
1256
  # Create mask for lineups where this player is Captain (first column)
 
1341
  player_summary = pd.DataFrame(player_stats)
1342
  player_summary = player_summary.sort_values('Lineup Count', ascending=False)
1343
  st.session_state['player_summary'] = player_summary.copy()
1344
+ if 'origin_player_exposures' not in st.session_state:
1345
+ st.session_state['origin_player_exposures'] = player_summary.copy()
1346
 
1347
  st.subheader("Player Summary")
1348
  st.dataframe(