James McCool commited on
Commit
a19edd8
Β·
1 Parent(s): 0b14eea

Implement pagination for dataframe display in app.py

Browse files

- Added pagination controls to navigate through the displayed dataframe, enhancing user experience by allowing users to view data in manageable chunks.
- Included logic to update session state for current page tracking and to clear relevant session data upon navigation, ensuring a fresh state for user interactions.
- Improved overall data presentation with dynamic start and end index calculations for the displayed subset of the dataframe.

Files changed (1) hide show
  1. app.py +40 -42
app.py CHANGED
@@ -183,6 +183,46 @@ with tab2:
183
  working_df['dupes'] = working_df.groupby('sorted').transform('size')
184
  working_df = working_df.drop('sorted', axis=1)
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  for col in player_columns:
187
  contest_players = working_df.copy()
188
  players_1per = working_df.nlargest(n=int(len(working_df) * 0.01), columns='actual_fpts')
@@ -237,45 +277,3 @@ with tab2:
237
  style.background_gradient(cmap='RdYlGn').
238
  format(formatter='{:.2%}', subset=st.session_state['stack_frame'].select_dtypes(include=['number']).columns),
239
  hide_index=True)
240
-
241
- start_idx = 0
242
- end_idx = 500
243
- st.dataframe(
244
- working_df.iloc[start_idx:end_idx].style
245
- .background_gradient(axis=0)
246
- .background_gradient(cmap='RdYlGn')
247
- .format(precision=2),
248
- height=1000,
249
- use_container_width=True,
250
- hide_index=True
251
- )
252
-
253
- # Initialize pagination in session state if not exists
254
- if 'current_page' not in st.session_state:
255
- st.session_state.current_page = 0
256
-
257
- # Calculate total pages
258
- rows_per_page = 500
259
- total_rows = len(working_df)
260
- total_pages = (total_rows + rows_per_page - 1) // rows_per_page
261
-
262
- # Create pagination controls in a single row
263
- pagination_cols = st.columns([4, 1, 1, 1, 4])
264
- with pagination_cols[1]:
265
- if st.button("← Previous", disabled=st.session_state.current_page == 0):
266
- st.session_state.current_page -= 1
267
- if 'player_frame' in st.session_state:
268
- del st.session_state['player_frame']
269
- del st.session_state['stack_frame']
270
-
271
- with pagination_cols[2]:
272
- st.markdown(f"**Page {st.session_state.current_page + 1} of {total_pages}**", unsafe_allow_html=True)
273
- with pagination_cols[3]:
274
- if st.button("Next β†’", disabled=st.session_state.current_page == total_pages - 1):
275
- st.session_state.current_page += 1
276
- if 'player_frame' in st.session_state:
277
- del st.session_state['player_frame']
278
- del st.session_state['stack_frame']
279
- # Calculate start and end indices for current page
280
- start_idx = st.session_state.current_page * rows_per_page
281
- end_idx = min((st.session_state.current_page + 1) * rows_per_page, total_rows)
 
183
  working_df['dupes'] = working_df.groupby('sorted').transform('size')
184
  working_df = working_df.drop('sorted', axis=1)
185
 
186
+
187
+ # Initialize pagination in session state if not exists
188
+ if 'current_page' not in st.session_state:
189
+ st.session_state.current_page = 0
190
+
191
+ # Calculate total pages
192
+ rows_per_page = 500
193
+ total_rows = len(working_df)
194
+ total_pages = (total_rows + rows_per_page - 1) // rows_per_page
195
+
196
+ # Create pagination controls in a single row
197
+ pagination_cols = st.columns([4, 1, 1, 1, 4])
198
+ with pagination_cols[1]:
199
+ if st.button("← Previous", disabled=st.session_state.current_page == 0):
200
+ st.session_state.current_page -= 1
201
+ if 'player_frame' in st.session_state:
202
+ del st.session_state['player_frame']
203
+ del st.session_state['stack_frame']
204
+
205
+ with pagination_cols[2]:
206
+ st.markdown(f"**Page {st.session_state.current_page + 1} of {total_pages}**", unsafe_allow_html=True)
207
+ with pagination_cols[3]:
208
+ if st.button("Next β†’", disabled=st.session_state.current_page == total_pages - 1):
209
+ st.session_state.current_page += 1
210
+ if 'player_frame' in st.session_state:
211
+ del st.session_state['player_frame']
212
+ del st.session_state['stack_frame']
213
+ # Calculate start and end indices for current page
214
+ start_idx = st.session_state.current_page * rows_per_page
215
+ end_idx = min((st.session_state.current_page + 1) * rows_per_page, total_rows)
216
+ st.dataframe(
217
+ working_df.iloc[start_idx:end_idx].style
218
+ .background_gradient(axis=0)
219
+ .background_gradient(cmap='RdYlGn')
220
+ .format(precision=2),
221
+ height=1000,
222
+ use_container_width=True,
223
+ hide_index=True
224
+ )
225
+
226
  for col in player_columns:
227
  contest_players = working_df.copy()
228
  players_1per = working_df.nlargest(n=int(len(working_df) * 0.01), columns='actual_fpts')
 
277
  style.background_gradient(cmap='RdYlGn').
278
  format(formatter='{:.2%}', subset=st.session_state['stack_frame'].select_dtypes(include=['number']).columns),
279
  hide_index=True)