James McCool commited on
Commit
38ab7f8
·
1 Parent(s): 53f8288

Refactor contest data handling in app.py for consistency and clarity

Browse files

- Updated references from 'Contest' to 'contest_df' in session state to align with naming conventions.
- Adjusted calculations for salary, median, and ownership metrics to use the new 'contest_df' variable, ensuring accurate data processing.
- Enhanced pagination and display logic to reflect changes in data structure, improving user experience and data visualization.

Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -82,7 +82,7 @@ with tab1:
82
  with tab2:
83
  if st.button('Clear data', key='reset3'):
84
  st.session_state.clear()
85
- if 'Contest' in st.session_state and 'projections_df' in st.session_state:
86
  col1, col2 = st.columns([1, 8])
87
  excluded_cols = ['BaseName', 'EntryCount']
88
  with col1:
@@ -106,31 +106,31 @@ with tab2:
106
  }
107
 
108
  if entry_parse_var == 'Specific':
109
- st.session_state['Contest'] = st.session_state['Contest'][st.session_state['Contest']['BaseName'].isin(entry_names)]
110
  else:
111
- st.session_state['Contest'] = st.session_state['Contest']
112
 
113
  if type_var == 'Classic':
114
- st.session_state['Contest']['salary'] = st.session_state['Contest'].apply(lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row), axis=1)
115
- st.session_state['Contest']['median'] = st.session_state['Contest'].apply(lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row), axis=1)
116
- st.session_state['Contest']['Own'] = st.session_state['Contest'].apply(lambda row: sum(map_dict['own_map'].get(player, 0) for player in row), axis=1)
117
  elif type_var == 'Showdown':
118
  # Calculate salary (CPT uses cpt_salary_map, others use salary_map)
119
- st.session_state['Contest']['salary'] = st.session_state['Contest'].apply(
120
  lambda row: map_dict['cpt_salary_map'].get(row.iloc[0], 0) +
121
  sum(map_dict['salary_map'].get(player, 0) for player in row.iloc[1:]),
122
  axis=1
123
  )
124
 
125
  # Calculate median (CPT uses cpt_proj_map, others use proj_map)
126
- st.session_state['Contest']['median'] = st.session_state['Contest'].apply(
127
  lambda row: map_dict['cpt_proj_map'].get(row.iloc[0], 0) +
128
  sum(map_dict['proj_map'].get(player, 0) for player in row.iloc[1:]),
129
  axis=1
130
  )
131
 
132
  # Calculate ownership (CPT uses cpt_own_map, others use own_map)
133
- st.session_state['Contest']['Own'] = st.session_state['Contest'].apply(
134
  lambda row: map_dict['cpt_own_map'].get(row.iloc[0], 0) +
135
  sum(map_dict['own_map'].get(player, 0) for player in row.iloc[1:]),
136
  axis=1
@@ -144,7 +144,7 @@ with tab2:
144
 
145
  # Calculate total pages
146
  rows_per_page = 500
147
- total_rows = len(st.session_state['Contest'])
148
  total_pages = (total_rows + rows_per_page - 1) // rows_per_page
149
 
150
  # Create pagination controls in a single row
@@ -164,7 +164,7 @@ with tab2:
164
 
165
  # Display the paginated dataframe
166
  st.dataframe(
167
- st.session_state['Contest'].iloc[start_idx:end_idx].style
168
  .background_gradient(axis=0)
169
  .background_gradient(cmap='RdYlGn')
170
  .format(precision=2),
 
82
  with tab2:
83
  if st.button('Clear data', key='reset3'):
84
  st.session_state.clear()
85
+ if 'contest_df' in st.session_state and 'projections_df' in st.session_state:
86
  col1, col2 = st.columns([1, 8])
87
  excluded_cols = ['BaseName', 'EntryCount']
88
  with col1:
 
106
  }
107
 
108
  if entry_parse_var == 'Specific':
109
+ st.session_state['contest_df'] = st.session_state['contest_df'][st.session_state['contest_df']['BaseName'].isin(entry_names)]
110
  else:
111
+ st.session_state['contest_df'] = st.session_state['contest_df']
112
 
113
  if type_var == 'Classic':
114
+ st.session_state['contest_df']['salary'] = st.session_state['contest_df'].apply(lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row), axis=1)
115
+ st.session_state['contest_df']['median'] = st.session_state['contest_df'].apply(lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row), axis=1)
116
+ st.session_state['contest_df']['Own'] = st.session_state['contest_df'].apply(lambda row: sum(map_dict['own_map'].get(player, 0) for player in row), axis=1)
117
  elif type_var == 'Showdown':
118
  # Calculate salary (CPT uses cpt_salary_map, others use salary_map)
119
+ st.session_state['contest_df']['salary'] = st.session_state['contest_df'].apply(
120
  lambda row: map_dict['cpt_salary_map'].get(row.iloc[0], 0) +
121
  sum(map_dict['salary_map'].get(player, 0) for player in row.iloc[1:]),
122
  axis=1
123
  )
124
 
125
  # Calculate median (CPT uses cpt_proj_map, others use proj_map)
126
+ st.session_state['contest_df']['median'] = st.session_state['contest_df'].apply(
127
  lambda row: map_dict['cpt_proj_map'].get(row.iloc[0], 0) +
128
  sum(map_dict['proj_map'].get(player, 0) for player in row.iloc[1:]),
129
  axis=1
130
  )
131
 
132
  # Calculate ownership (CPT uses cpt_own_map, others use own_map)
133
+ st.session_state['contest_df']['Own'] = st.session_state['contest_df'].apply(
134
  lambda row: map_dict['cpt_own_map'].get(row.iloc[0], 0) +
135
  sum(map_dict['own_map'].get(player, 0) for player in row.iloc[1:]),
136
  axis=1
 
144
 
145
  # Calculate total pages
146
  rows_per_page = 500
147
+ total_rows = len(st.session_state['contest_df'])
148
  total_pages = (total_rows + rows_per_page - 1) // rows_per_page
149
 
150
  # Create pagination controls in a single row
 
164
 
165
  # Display the paginated dataframe
166
  st.dataframe(
167
+ st.session_state['contest_df'].iloc[start_idx:end_idx].style
168
  .background_gradient(axis=0)
169
  .background_gradient(cmap='RdYlGn')
170
  .format(precision=2),