Advertisement
Guest User

Untitled

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