Last active
June 17, 2022 04:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sentient-soellingeraj
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)
andp.prev_page(None)