Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@sentient-soellingeraj sentient-soellingeraj commented Jan 14, 2019

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

@nokados

This comment has been minimized.

Copy link
Owner Author

@nokados 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
You can’t perform that action at this time.