Skip to content

Instantly share code, notes, and snippets.

@nokados
Last active June 17, 2022 04:23
Show Gist options
  • Save nokados/e8f0a64b55099f2f07a50f2b090c91c7 to your computer and use it in GitHub Desktop.
Save nokados/e8f0a64b55099f2f07a50f2b090c91c7 to your computer and use it in GitHub Desktop.
Paginator for pandas.DataFrame in Jupyter Notebook. UPD: use https://github.com/nvictus/pandas-jupyter-paginate instead
from IPython.core.display import display, HTML, clear_output
from ipywidgets import widgets, Layout
import math
PAGESIZE=10
class Paginator:
def __init__(self, df, pagesize = PAGESIZE, start_page = 0):
self.df = df
self.pagesize = pagesize
self.page = start_page
self.max_pages = math.ceil(df.shape[0] / self.pagesize)
self.btn_next = widgets.Button(description='Next')
self.btn_next.on_click(self.next_page)
self.btn_prev = widgets.Button(description='Prev')
self.btn_prev.on_click(self.prev_page)
def get_page(self):
return self.df.iloc[self.page * self.pagesize: (self.page + 1) * self.pagesize]
def show(self):
clear_output()
self.control = widgets.HBox((self.btn_prev,
widgets.Label('PAGE {}/{}'.format(self.page + 1, self.max_pages)),
self.btn_next))
display(self.control)
display(self.get_page())
display(self.control)
def next_page(self, b):
self.page = min(self.max_pages - 1, self.page + 1)
self.show()
def prev_page(self, b):
self.page = max(0, self.page - 1)
self.show()
@sentient-soellingeraj
Copy link

The buttons don't work for me. Is there some settings I need to change?

@nokados
Copy link
Author

nokados commented Feb 7, 2019

@sentient-soellingeraj

The buttons don't work for me. Is there some settings I need to change?

Try to refresh the page with the notebook or restart the laptop.
If not working, you can switch the page with the commands p.next_page(None) and p.prev_page(None)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment