James McCool commited on
Commit
f79125f
·
1 Parent(s): c47e22d

Add pagination functionality to data tables in Streamlit app

Browse files

- Implemented a new pagination system for displaying data frames in both pitcher and hitter tabs, enhancing user navigation through large datasets.
- Introduced buttons for navigating between pages, improving the overall user experience.
- Adjusted loading spinner duration for league baseline data to optimize perceived performance during data retrieval.

Files changed (1) hide show
  1. src/streamlit_app.py +42 -11
src/streamlit_app.py CHANGED
@@ -53,6 +53,43 @@ st.markdown("""
53
  }
54
  </style>""", unsafe_allow_html=True)
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  @st.cache_resource(ttl = 61)
57
  def init_baselines():
58
 
@@ -178,16 +215,8 @@ with pitcher_tab:
178
  if table_var_sp in (['League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines']):
179
 
180
  with st.spinner("Full league baselines can take some time to load"):
181
- time.sleep(7)
182
- with sp_disp_container:
183
- st.dataframe(st.session_state['sp_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').format(precision=2), height = 500, use_container_width = True, hide_index = True)
184
- col1, col2, col3, col4 = st.columns(4)
185
- with col1:
186
- if st.button('Previous'):
187
- st.session_state['sp_disp_frame'] = st.session_state['sp_disp_frame'].iloc[:page_var]
188
- with col4:
189
- if st.button('Next'):
190
- st.session_state['sp_disp_frame'] = st.session_state['sp_disp_frame'].iloc[page_var:]
191
  else:
192
  with sp_disp_container:
193
  st.dataframe(st.session_state['sp_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').format(precision=2), height = 500, use_container_width = True, hide_index = True)
@@ -267,7 +296,9 @@ with hitter_tab:
267
  if table_var_hitter in (['League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines']):
268
  with st.spinner("Full league baselines can take some time to load"):
269
  time.sleep(7)
270
- st.dataframe(st.session_state['hitter_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height = 750, use_container_width = True, hide_index = True)
 
 
271
 
272
  with team_tab:
273
  with st.container(border = True):
 
53
  }
54
  </style>""", unsafe_allow_html=True)
55
 
56
+ def paginate_dataframe(df, page_size):
57
+ total_rows = len(df)
58
+ total_pages = (total_rows + page_size -1) // page_size
59
+
60
+ if 'current_page' not in st.session_state:
61
+ st.session_state['current_page'] = 0
62
+
63
+ start_idx = st.session_state['current_page'] * page_size
64
+ end_idx = start_idx + page_size
65
+
66
+ current_page_data = df.iloc[start_idx:end_idx]
67
+
68
+ return current_page_data, total_pages, total_rows
69
+
70
+ def display_paginated_table(df, page_size):
71
+ current_page_data, total_pages, total_rows = paginate_dataframe(df, page_size)
72
+
73
+ st.dataframe(current_page_data.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').format(precision=2), height = 500, use_container_width = True, hide_index = True)
74
+
75
+ col1, col2, col3, col4 = st.columns(1, 2, 2, 1)
76
+ with col1:
77
+ if st.button('⏮️ First', disabled=st.session_state.current_page == 0):
78
+ st.session_state.current_page = 0
79
+ st.rerun()
80
+ with col2:
81
+ if st.button('⬅️ Previous', disabled=st.session_state.current_page == 0):
82
+ st.session_state.current_page -= 1
83
+ st.rerun()
84
+ with col3:
85
+ if st.button('Next ➡️', disabled=st.session_state.current_page >= total_pages - 1):
86
+ st.session_state.current_page += 1
87
+ st.rerun()
88
+ with col4:
89
+ if st.button('Last ⏭️', disabled=st.session_state.current_page >= total_pages - 1):
90
+ st.session_state.current_page = total_pages - 1
91
+ st.rerun()
92
+
93
  @st.cache_resource(ttl = 61)
94
  def init_baselines():
95
 
 
215
  if table_var_sp in (['League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines']):
216
 
217
  with st.spinner("Full league baselines can take some time to load"):
218
+ time.sleep(5)
219
+ display_paginated_table(st.session_state['sp_disp_frame'], 50)
 
 
 
 
 
 
 
 
220
  else:
221
  with sp_disp_container:
222
  st.dataframe(st.session_state['sp_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn_r').format(precision=2), height = 500, use_container_width = True, hide_index = True)
 
296
  if table_var_hitter in (['League Aggregate Baselines', 'League Short Term Baselines', 'League Long Term Baselines']):
297
  with st.spinner("Full league baselines can take some time to load"):
298
  time.sleep(7)
299
+ display_paginated_table(st.session_state['hitter_disp_frame'], 50)
300
+ else:
301
+ st.dataframe(st.session_state['hitter_disp_frame'].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), height = 750, use_container_width = True, hide_index = True)
302
 
303
  with team_tab:
304
  with st.container(border = True):