James McCool
commited on
Commit
Β·
e24862c
1
Parent(s):
38ab7f8
Refactor contest data processing in app.py for improved clarity and functionality
Browse files- Streamlined the initialization of projections and contest data in session state, ensuring name matching occurs only once during the initial load.
- Enhanced the calculation logic for salary, median, and ownership metrics by utilizing a working copy of the contest dataframe, improving data handling.
- Updated pagination controls to reflect changes in the working dataframe, ensuring accurate navigation and display of contest data.
app.py
CHANGED
@@ -75,9 +75,8 @@ with tab1:
|
|
75 |
if 'projections_df' not in st.session_state:
|
76 |
st.session_state['projections_df'] = projections.copy()
|
77 |
st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int))
|
78 |
-
|
79 |
-
|
80 |
-
st.session_state['contest_df'], st.session_state['projections_df'] = find_name_mismatches(st.session_state['Contest'], st.session_state['projections_df'])
|
81 |
|
82 |
with tab2:
|
83 |
if st.button('Clear data', key='reset3'):
|
@@ -92,82 +91,79 @@ with tab2:
|
|
92 |
entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
|
93 |
submitted = st.form_submit_button("Submit")
|
94 |
|
95 |
-
|
96 |
map_dict = {
|
97 |
-
'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
|
98 |
-
'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
|
99 |
-
'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
|
100 |
-
'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
|
101 |
-
'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
|
102 |
-
'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
|
103 |
-
'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
|
104 |
-
'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
|
105 |
-
'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
|
106 |
}
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
113 |
if type_var == 'Classic':
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
elif type_var == 'Showdown':
|
118 |
-
|
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
|
137 |
)
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
)
|
|
|
75 |
if 'projections_df' not in st.session_state:
|
76 |
st.session_state['projections_df'] = projections.copy()
|
77 |
st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int))
|
78 |
+
# Run name matching only once when first loading the files
|
79 |
+
st.session_state['contest_df'], st.session_state['projections_df'] = find_name_mismatches(st.session_state['Contest'], st.session_state['projections_df'])
|
|
|
80 |
|
81 |
with tab2:
|
82 |
if st.button('Clear data', key='reset3'):
|
|
|
91 |
entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
|
92 |
submitted = st.form_submit_button("Submit")
|
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 |
+
|
107 |
+
# Create a copy of the dataframe for calculations
|
108 |
+
working_df = st.session_state['contest_df'].copy()
|
109 |
+
|
110 |
+
# Apply filters if submitted
|
111 |
+
if submitted and entry_parse_var == 'Specific':
|
112 |
+
working_df = working_df[working_df['BaseName'].isin(entry_names)]
|
113 |
+
|
114 |
+
# Calculate metrics based on game type
|
115 |
if type_var == 'Classic':
|
116 |
+
working_df['salary'] = working_df.apply(lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row), axis=1)
|
117 |
+
working_df['median'] = working_df.apply(lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row), axis=1)
|
118 |
+
working_df['Own'] = working_df.apply(lambda row: sum(map_dict['own_map'].get(player, 0) for player in row), axis=1)
|
119 |
elif type_var == 'Showdown':
|
120 |
+
working_df['salary'] = working_df.apply(
|
|
|
121 |
lambda row: map_dict['cpt_salary_map'].get(row.iloc[0], 0) +
|
122 |
sum(map_dict['salary_map'].get(player, 0) for player in row.iloc[1:]),
|
123 |
axis=1
|
124 |
)
|
125 |
+
working_df['median'] = working_df.apply(
|
|
|
|
|
126 |
lambda row: map_dict['cpt_proj_map'].get(row.iloc[0], 0) +
|
127 |
sum(map_dict['proj_map'].get(player, 0) for player in row.iloc[1:]),
|
128 |
axis=1
|
129 |
)
|
130 |
+
working_df['Own'] = working_df.apply(
|
|
|
|
|
131 |
lambda row: map_dict['cpt_own_map'].get(row.iloc[0], 0) +
|
132 |
sum(map_dict['own_map'].get(player, 0) for player in row.iloc[1:]),
|
133 |
axis=1
|
134 |
)
|
135 |
|
136 |
+
# Initialize pagination in session state if not exists
|
137 |
+
if 'current_page' not in st.session_state:
|
138 |
+
st.session_state.current_page = 0
|
139 |
+
|
140 |
+
# Calculate total pages
|
141 |
+
rows_per_page = 500
|
142 |
+
total_rows = len(working_df)
|
143 |
+
total_pages = (total_rows + rows_per_page - 1) // rows_per_page
|
144 |
+
|
145 |
+
# Create pagination controls in a single row
|
146 |
+
pagination_cols = st.columns([4, 1, 1, 1, 4])
|
147 |
+
with pagination_cols[1]:
|
148 |
+
if st.button("β Previous", disabled=st.session_state.current_page == 0):
|
149 |
+
st.session_state.current_page -= 1
|
150 |
+
with pagination_cols[2]:
|
151 |
+
st.markdown(f"**Page {st.session_state.current_page + 1} of {total_pages}**", unsafe_allow_html=True)
|
152 |
+
with pagination_cols[3]:
|
153 |
+
if st.button("Next β", disabled=st.session_state.current_page == total_pages - 1):
|
154 |
+
st.session_state.current_page += 1
|
155 |
+
|
156 |
+
# Calculate start and end indices for current page
|
157 |
+
start_idx = st.session_state.current_page * rows_per_page
|
158 |
+
end_idx = min((st.session_state.current_page + 1) * rows_per_page, total_rows)
|
159 |
+
|
160 |
+
# Display the paginated dataframe
|
161 |
+
st.dataframe(
|
162 |
+
working_df.iloc[start_idx:end_idx].style
|
163 |
+
.background_gradient(axis=0)
|
164 |
+
.background_gradient(cmap='RdYlGn')
|
165 |
+
.format(precision=2),
|
166 |
+
height=1000,
|
167 |
+
use_container_width=True,
|
168 |
+
hide_index=True
|
169 |
+
)
|
|