James McCool commited on
Commit
1854e4d
·
1 Parent(s): 7adbc9e

Refactor app.py to improve session state handling and user interaction

Browse files

- Streamlined the management of session state variables in app.py, specifically focusing on the handling of 'Contest' and 'projections_df'.
- Enhanced the layout for user filters and data clearing options, ensuring a more intuitive user experience.
- Maintained the functionality of 'calc_toggle' to track calculation states effectively during user interactions.

Files changed (1) hide show
  1. app.py +37 -38
app.py CHANGED
@@ -86,46 +86,45 @@ with tab2:
86
  st.write(st.session_state['calc_toggle'])
87
  st.write(st.session_state['projections_df'])
88
  st.write(st.session_state['Contest'])
89
- if 'Contest' in st.session_state and 'projections_df' in st.session_state and st.session_state['calc_toggle']:
90
- col1, col2 = st.columns([1, 8])
91
- excluded_cols = ['BaseName', 'EntryCount']
92
- player_columns = [col for col in st.session_state['Contest'].columns if col not in excluded_cols]
93
- for col in player_columns:
94
- st.session_state['Contest'][col] = st.session_state['Contest'][col].astype(str)
95
-
96
- # Create mapping dictionaries
97
- map_dict = {
98
- 'pos_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
99
- 'team_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
100
- 'salary_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
101
- 'proj_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
102
- 'own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
103
- 'own_percent_rank': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
104
- 'cpt_salary_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
105
- 'cpt_proj_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
106
- 'cpt_own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
107
- }
108
- # Create a copy of the dataframe for calculations
109
- working_df = st.session_state['Contest'].copy()
110
 
111
- with col1:
112
- with st.expander("Info and filters"):
113
- if st.button('Clear data', key='reset3'):
114
- st.session_state.clear()
115
- with st.form(key='filter_form'):
116
- type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'])
117
- entry_parse_var = st.selectbox("Do you want to view a specific player(s) or a group of players?", ['All', 'Specific'])
118
- entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
119
- submitted = st.form_submit_button("Submit")
120
- if submitted:
121
- if 'player_frame' in st.session_state:
122
- del st.session_state['player_frame']
123
- if 'stack_frame' in st.session_state:
124
- del st.session_state['stack_frame']
125
- # Apply entry name filter if specific entries are selected
126
- if entry_parse_var == 'Specific' and entry_names:
127
- working_df = working_df[working_df['BaseName'].isin(entry_names)]
128
 
 
129
  if type_var == 'Classic':
130
  working_df['stack'] = working_df.apply(
131
  lambda row: Counter(
 
86
  st.write(st.session_state['calc_toggle'])
87
  st.write(st.session_state['projections_df'])
88
  st.write(st.session_state['Contest'])
89
+ excluded_cols = ['BaseName', 'EntryCount']
90
+ player_columns = [col for col in st.session_state['Contest'].columns if col not in excluded_cols]
91
+ for col in player_columns:
92
+ st.session_state['Contest'][col] = st.session_state['Contest'][col].astype(str)
93
+
94
+ # Create mapping dictionaries
95
+ map_dict = {
96
+ 'pos_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
97
+ 'team_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
98
+ 'salary_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
99
+ 'proj_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
100
+ 'own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
101
+ 'own_percent_rank': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
102
+ 'cpt_salary_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
103
+ 'cpt_proj_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
104
+ 'cpt_own_map': dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
105
+ }
106
+ # Create a copy of the dataframe for calculations
107
+ working_df = st.session_state['Contest'].copy()
 
 
108
 
109
+ with st.expander("Info and filters"):
110
+ if st.button('Clear data', key='reset3'):
111
+ st.session_state.clear()
112
+ with st.form(key='filter_form'):
113
+ type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'])
114
+ entry_parse_var = st.selectbox("Do you want to view a specific player(s) or a group of players?", ['All', 'Specific'])
115
+ entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
116
+ submitted = st.form_submit_button("Submit")
117
+ if submitted:
118
+ if 'player_frame' in st.session_state:
119
+ del st.session_state['player_frame']
120
+ if 'stack_frame' in st.session_state:
121
+ del st.session_state['stack_frame']
122
+ # Apply entry name filter if specific entries are selected
123
+ if entry_parse_var == 'Specific' and entry_names:
124
+ working_df = working_df[working_df['BaseName'].isin(entry_names)]
125
+ st.session_state['calc_toggle'] = True
126
 
127
+ if 'Contest' in st.session_state and 'projections_df' in st.session_state and st.session_state['calc_toggle']:
128
  if type_var == 'Classic':
129
  working_df['stack'] = working_df.apply(
130
  lambda row: Counter(