James McCool
commited on
Commit
·
6e8cffc
1
Parent(s):
0425e05
Refactor pagination and filtering logic in app.py
Browse files- Improved the structure of the info and filters section by consolidating pagination controls and filtering options.
- Enhanced user experience with clearer pagination management and streamlined data display for player and stack exposures.
- Maintained functionality while ensuring better readability and maintainability of the code.
app.py
CHANGED
@@ -174,112 +174,112 @@ with tab2:
|
|
174 |
st.session_state['field_player_frame'] = create_player_exposures(working_df, player_columns)
|
175 |
st.session_state['field_stack_frame'] = create_stack_exposures(working_df)
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
if st.button(f"Previous Page"):
|
206 |
-
if st.session_state['current_page'] > 1:
|
207 |
-
st.session_state.current_page -= 1
|
208 |
-
else:
|
209 |
-
st.session_state.current_page = 1
|
210 |
if 'player_frame' in st.session_state:
|
211 |
del st.session_state['player_frame']
|
212 |
if 'stack_frame' in st.session_state:
|
213 |
del st.session_state['stack_frame']
|
214 |
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
st.dataframe(
|
227 |
-
working_df.iloc[start_idx:end_idx].style
|
228 |
-
.background_gradient(axis=0)
|
229 |
-
.background_gradient(cmap='RdYlGn')
|
230 |
-
.format(precision=2),
|
231 |
-
height=500,
|
232 |
-
use_container_width=True,
|
233 |
-
hide_index=True
|
234 |
-
)
|
235 |
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
with tab3:
|
271 |
-
|
272 |
-
if entry_parse_var == 'All':
|
273 |
-
st.session_state['stack_size_frame'] = create_stack_size_exposures(working_df)
|
274 |
-
st.dataframe(st.session_state['stack_size_frame'].
|
275 |
-
sort_values(by='Exposure Overall', ascending=False).
|
276 |
-
style.background_gradient(cmap='RdYlGn').
|
277 |
-
format(formatter='{:.2%}', subset=st.session_state['stack_size_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
278 |
-
hide_index=True)
|
279 |
-
else:
|
280 |
-
st.session_state['stack_size_frame'] = create_stack_size_exposures(working_df, entry_names)
|
281 |
-
st.dataframe(st.session_state['stack_size_frame'].
|
282 |
-
sort_values(by='Exposure Overall', ascending=False).
|
283 |
-
style.background_gradient(cmap='RdYlGn').
|
284 |
-
format(formatter='{:.2%}', subset=st.session_state['stack_size_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
285 |
-
hide_index=True)
|
|
|
174 |
st.session_state['field_player_frame'] = create_player_exposures(working_df, player_columns)
|
175 |
st.session_state['field_stack_frame'] = create_stack_exposures(working_df)
|
176 |
|
177 |
+
with st.expander("Info and filters"):
|
178 |
+
if st.button('Clear data', key='reset3'):
|
179 |
+
st.session_state.clear()
|
180 |
+
with st.form(key='filter_form'):
|
181 |
+
entry_parse_var = st.selectbox("Do you want to view a specific player(s) or a group of players?", ['All', 'Specific'])
|
182 |
+
entry_names = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
|
183 |
+
submitted = st.form_submit_button("Submit")
|
184 |
+
if submitted:
|
185 |
+
if 'player_frame' in st.session_state:
|
186 |
+
del st.session_state['player_frame']
|
187 |
+
if 'stack_frame' in st.session_state:
|
188 |
+
del st.session_state['stack_frame']
|
189 |
+
# Apply entry name filter if specific entries are selected
|
190 |
+
if entry_parse_var == 'Specific' and entry_names:
|
191 |
+
working_df = working_df[working_df['BaseName'].isin(entry_names)]
|
192 |
+
|
193 |
+
# Initialize pagination in session state if not exists
|
194 |
+
if 'current_page' not in st.session_state:
|
195 |
+
st.session_state.current_page = 1
|
196 |
+
|
197 |
+
# Calculate total pages
|
198 |
+
rows_per_page = 500
|
199 |
+
total_rows = len(working_df)
|
200 |
+
total_pages = (total_rows + rows_per_page - 1) // rows_per_page
|
201 |
|
202 |
+
# Create pagination controls in a single row
|
203 |
+
pagination_cols = st.columns([4, 1, 1, 1, 4])
|
204 |
+
with pagination_cols[1]:
|
205 |
+
if st.button(f"Previous Page"):
|
206 |
+
if st.session_state['current_page'] > 1:
|
207 |
+
st.session_state.current_page -= 1
|
208 |
+
else:
|
209 |
+
st.session_state.current_page = 1
|
210 |
+
if 'player_frame' in st.session_state:
|
211 |
+
del st.session_state['player_frame']
|
212 |
+
if 'stack_frame' in st.session_state:
|
213 |
+
del st.session_state['stack_frame']
|
214 |
|
215 |
+
with pagination_cols[3]:
|
216 |
+
if st.button(f"Next Page"):
|
217 |
+
st.session_state.current_page += 1
|
|
|
|
|
|
|
|
|
|
|
218 |
if 'player_frame' in st.session_state:
|
219 |
del st.session_state['player_frame']
|
220 |
if 'stack_frame' in st.session_state:
|
221 |
del st.session_state['stack_frame']
|
222 |
|
223 |
+
# Calculate start and end indices for current page
|
224 |
+
start_idx = (st.session_state.current_page - 1) * rows_per_page
|
225 |
+
end_idx = min((st.session_state.current_page) * rows_per_page, total_rows)
|
226 |
+
st.dataframe(
|
227 |
+
working_df.iloc[start_idx:end_idx].style
|
228 |
+
.background_gradient(axis=0)
|
229 |
+
.background_gradient(cmap='RdYlGn')
|
230 |
+
.format(precision=2),
|
231 |
+
height=500,
|
232 |
+
use_container_width=True,
|
233 |
+
hide_index=True
|
234 |
+
)
|
235 |
|
236 |
+
with st.container():
|
237 |
+
tab1, tab2, tab3 = st.tabs(['Player Used Info', 'Stack Used Info', 'Stack Size Info'])
|
238 |
+
with tab1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
+
if entry_parse_var == 'All':
|
241 |
+
st.session_state['player_frame'] = create_player_exposures(working_df, player_columns)
|
242 |
+
st.dataframe(st.session_state['player_frame'].
|
243 |
+
sort_values(by='Exposure Overall', ascending=False).
|
244 |
+
style.background_gradient(cmap='RdYlGn').
|
245 |
+
format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
246 |
+
hide_index=True)
|
247 |
+
else:
|
248 |
+
st.session_state['player_frame'] = create_player_exposures(working_df, player_columns, entry_names)
|
249 |
+
st.dataframe(st.session_state['player_frame'].
|
250 |
+
sort_values(by='Exposure Overall', ascending=False).
|
251 |
+
style.background_gradient(cmap='RdYlGn').
|
252 |
+
format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
253 |
+
hide_index=True)
|
254 |
+
with tab2:
|
255 |
|
256 |
+
if entry_parse_var == 'All':
|
257 |
+
st.session_state['stack_frame'] = create_stack_exposures(working_df)
|
258 |
+
st.dataframe(st.session_state['stack_frame'].
|
259 |
+
sort_values(by='Exposure Overall', ascending=False).
|
260 |
+
style.background_gradient(cmap='RdYlGn').
|
261 |
+
format(formatter='{:.2%}', subset=st.session_state['stack_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
262 |
+
hide_index=True)
|
263 |
+
else:
|
264 |
+
st.session_state['stack_frame'] = create_stack_exposures(working_df, entry_names)
|
265 |
+
st.dataframe(st.session_state['stack_frame'].
|
266 |
+
sort_values(by='Exposure Overall', ascending=False).
|
267 |
+
style.background_gradient(cmap='RdYlGn').
|
268 |
+
format(formatter='{:.2%}', subset=st.session_state['stack_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
269 |
+
hide_index=True)
|
270 |
+
with tab3:
|
271 |
+
|
272 |
+
if entry_parse_var == 'All':
|
273 |
+
st.session_state['stack_size_frame'] = create_stack_size_exposures(working_df)
|
274 |
+
st.dataframe(st.session_state['stack_size_frame'].
|
275 |
+
sort_values(by='Exposure Overall', ascending=False).
|
276 |
+
style.background_gradient(cmap='RdYlGn').
|
277 |
+
format(formatter='{:.2%}', subset=st.session_state['stack_size_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
278 |
+
hide_index=True)
|
279 |
+
else:
|
280 |
+
st.session_state['stack_size_frame'] = create_stack_size_exposures(working_df, entry_names)
|
281 |
+
st.dataframe(st.session_state['stack_size_frame'].
|
282 |
+
sort_values(by='Exposure Overall', ascending=False).
|
283 |
+
style.background_gradient(cmap='RdYlGn').
|
284 |
+
format(formatter='{:.2%}', subset=st.session_state['stack_size_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
|
285 |
+
hide_index=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|