Spaces:
Sleeping
Sleeping
Upload pagination.py
Browse files- pagination.py +70 -0
pagination.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def paginator(label, items, items_per_page=10, on_sidebar=True, ukey=None):
|
| 5 |
+
"""Lets the user paginate a set of items.
|
| 6 |
+
Parameters
|
| 7 |
+
----------
|
| 8 |
+
label : str
|
| 9 |
+
The label to display over the pagination widget.
|
| 10 |
+
items : Iterator[Any]
|
| 11 |
+
The items to display in the paginator.
|
| 12 |
+
items_per_page: int
|
| 13 |
+
The number of items to display per page.
|
| 14 |
+
on_sidebar: bool
|
| 15 |
+
Whether to display the paginator widget on the sidebar.
|
| 16 |
+
|
| 17 |
+
Returns
|
| 18 |
+
-------
|
| 19 |
+
Iterator[Tuple[int, Any]]
|
| 20 |
+
An iterator over *only the items on that page*, including
|
| 21 |
+
the item's index.
|
| 22 |
+
Example
|
| 23 |
+
-------
|
| 24 |
+
This shows how to display a few pages of fruit.
|
| 25 |
+
>>> fruit_list = [
|
| 26 |
+
... 'Kiwifruit', 'Honeydew', 'Cherry', 'Honeyberry', 'Pear',
|
| 27 |
+
... 'Apple', 'Nectarine', 'Soursop', 'Pineapple', 'Satsuma',
|
| 28 |
+
... 'Fig', 'Huckleberry', 'Coconut', 'Plantain', 'Jujube',
|
| 29 |
+
... 'Guava', 'Clementine', 'Grape', 'Tayberry', 'Salak',
|
| 30 |
+
... 'Raspberry', 'Loquat', 'Nance', 'Peach', 'Akee'
|
| 31 |
+
... ]
|
| 32 |
+
...
|
| 33 |
+
... for i, fruit in paginator("Select a fruit page", fruit_list):
|
| 34 |
+
... st.write('%s. **%s**' % (i, fruit))
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
# Figure out where to display the paginator
|
| 38 |
+
if on_sidebar:
|
| 39 |
+
location = st.sidebar.empty()
|
| 40 |
+
else:
|
| 41 |
+
location = st.empty()
|
| 42 |
+
|
| 43 |
+
# Display a pagination selectbox in the specified location.
|
| 44 |
+
items = list(items)
|
| 45 |
+
n_pages = len(items)
|
| 46 |
+
n_pages = (len(items) - 1) // items_per_page + 1
|
| 47 |
+
page_format_func = lambda i: "Page %s" % (i+1)
|
| 48 |
+
page_number = location.select_slider(label, range(n_pages), format_func=page_format_func, key=ukey)
|
| 49 |
+
|
| 50 |
+
# Iterate over the items in the page to let the user display them.
|
| 51 |
+
min_index = page_number * items_per_page
|
| 52 |
+
max_index = min_index + items_per_page
|
| 53 |
+
import itertools
|
| 54 |
+
return itertools.islice(enumerate(items), min_index, max_index)
|
| 55 |
+
|
| 56 |
+
def demonstrate_paginator():
|
| 57 |
+
fruit_list = [
|
| 58 |
+
'Kiwifruit', 'Honeydew', 'Cherry', 'Honeyberry', 'Pear',
|
| 59 |
+
'Apple', 'Nectarine', 'Soursop', 'Pineapple', 'Satsuma',
|
| 60 |
+
'Fig', 'Huckleberry', 'Coconut', 'Plantain', 'Jujube',
|
| 61 |
+
'Guava', 'Clementine', 'Grape', 'Tayberry', 'Salak',
|
| 62 |
+
'Raspberry', 'Loquat', 'Nance', 'Peach', 'Akee'
|
| 63 |
+
]
|
| 64 |
+
for i, fruit in paginator("Select a fruit page", fruit_list):
|
| 65 |
+
st.write('%s. **%s**' % (i, fruit))
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
if __name__ == '__main__':
|
| 69 |
+
demonstrate_paginator()
|
| 70 |
+
|