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