James McCool commited on
Commit
23ee048
·
1 Parent(s): 1da87a4

Refactor session state management in app.py

Browse files

- Removed the 'calc_toggle' variable from session state, simplifying the logic for calculations.
- Updated variable names for ownership and actual dataframes to improve clarity and consistency.
- Enhanced the handling of name mismatches by directly using updated dataframe variables, streamlining data processing.

Files changed (1) hide show
  1. app.py +5 -12
app.py CHANGED
@@ -15,14 +15,11 @@ from global_func.create_stack_exposures import create_stack_exposures
15
  from global_func.create_stack_size_exposures import create_stack_size_exposures
16
 
17
  player_exposure_format = {'Exposure Overall': '{:.2%}', 'Exposure Top 1%': '{:.2%}', 'Exposure Top 5%': '{:.2%}', 'Exposure Top 10%': '{:.2%}', 'Exposure Top 20%': '{:.2%}'}
18
- if 'calc_toggle' not in st.session_state:
19
- st.session_state['calc_toggle'] = False
20
 
21
  tab1, tab2 = st.tabs(["Data Load", "Contest Analysis"])
22
  with tab1:
23
  if st.button('Clear data', key='reset1'):
24
  st.session_state.clear()
25
- st.session_state['calc_toggle'] = False
26
  col1, col2 = st.columns(2)
27
  with col1:
28
  sport_select = st.selectbox("Select Sport", ['MLB', 'NBA', 'NFL'])
@@ -39,7 +36,7 @@ with tab1:
39
  del st.session_state['Contest']
40
 
41
  if Contest_file:
42
- st.session_state['Contest'], st.session_state['ownership_dict'], st.session_state['actual_dict'], st.session_state['entry_list'] = load_contest_file(Contest_file, sport_select)
43
  st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
44
  st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
45
  if st.session_state['Contest'] is not None:
@@ -77,11 +74,10 @@ with tab1:
77
 
78
  if Contest_file and projections_file:
79
  st.subheader("Name Matching functions")
80
- st.session_state['Contest'], st.session_state['projections_df'], st.session_state['ownership_dict'], st.session_state['actual_dict'], st.session_state['calc_toggle'] = find_name_mismatches(st.session_state['Contest'], st.session_state['projections_df'], st.session_state['ownership_dict'], st.session_state['actual_dict'], st.session_state['calc_toggle'])
81
  st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int))
82
- st.session_state['ownership_dict'] = dict(zip(st.session_state['ownership_dict']['Player'], st.session_state['ownership_dict']['Own']))
83
- st.session_state['actual_dict'] = dict(zip(st.session_state['actual_dict']['Player'], st.session_state['actual_dict']['FPTS']))
84
- st.session_state['calc_toggle'] = True
85
 
86
 
87
  with tab2:
@@ -105,7 +101,7 @@ with tab2:
105
  # Create a copy of the dataframe for calculations
106
  working_df = st.session_state['Contest'].copy()
107
 
108
- if 'Contest' in st.session_state and 'projections_df' in st.session_state and st.session_state['calc_toggle']:
109
  if type_var == 'Classic':
110
  working_df['stack'] = working_df.apply(
111
  lambda row: Counter(
@@ -192,9 +188,6 @@ with tab2:
192
  # Apply entry name filter if specific entries are selected
193
  if entry_parse_var == 'Specific' and entry_names:
194
  working_df = working_df[working_df['BaseName'].isin(entry_names)]
195
- st.session_state['calc_toggle'] = True
196
- elif entry_parse_var == 'All':
197
- st.session_state['calc_toggle'] = True
198
 
199
  # Initialize pagination in session state if not exists
200
  if 'current_page' not in st.session_state:
 
15
  from global_func.create_stack_size_exposures import create_stack_size_exposures
16
 
17
  player_exposure_format = {'Exposure Overall': '{:.2%}', 'Exposure Top 1%': '{:.2%}', 'Exposure Top 5%': '{:.2%}', 'Exposure Top 10%': '{:.2%}', 'Exposure Top 20%': '{:.2%}'}
 
 
18
 
19
  tab1, tab2 = st.tabs(["Data Load", "Contest Analysis"])
20
  with tab1:
21
  if st.button('Clear data', key='reset1'):
22
  st.session_state.clear()
 
23
  col1, col2 = st.columns(2)
24
  with col1:
25
  sport_select = st.selectbox("Select Sport", ['MLB', 'NBA', 'NFL'])
 
36
  del st.session_state['Contest']
37
 
38
  if Contest_file:
39
+ st.session_state['Contest'], st.session_state['ownership_df'], st.session_state['actual_df'], st.session_state['entry_list'] = load_contest_file(Contest_file, sport_select)
40
  st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
41
  st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
42
  if st.session_state['Contest'] is not None:
 
74
 
75
  if Contest_file and projections_file:
76
  st.subheader("Name Matching functions")
77
+ st.session_state['Contest'], st.session_state['projections_df'], st.session_state['ownership_df'], st.session_state['actual_df'] = find_name_mismatches(st.session_state['Contest'], st.session_state['projections_df'], st.session_state['ownership_df'], st.session_state['actual_df'])
78
  st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int))
79
+ st.session_state['ownership_dict'] = dict(zip(st.session_state['ownership_df']['Player'], st.session_state['ownership_df']['Own']))
80
+ st.session_state['actual_dict'] = dict(zip(st.session_state['actual_df']['Player'], st.session_state['actual_df']['FPTS']))
 
81
 
82
 
83
  with tab2:
 
101
  # Create a copy of the dataframe for calculations
102
  working_df = st.session_state['Contest'].copy()
103
 
104
+ if 'Contest' in st.session_state and 'projections_df' in st.session_state:
105
  if type_var == 'Classic':
106
  working_df['stack'] = working_df.apply(
107
  lambda row: Counter(
 
188
  # Apply entry name filter if specific entries are selected
189
  if entry_parse_var == 'Specific' and entry_names:
190
  working_df = working_df[working_df['BaseName'].isin(entry_names)]
 
 
 
191
 
192
  # Initialize pagination in session state if not exists
193
  if 'current_page' not in st.session_state: