# Pagination

> Page numbers with previous and next arrows. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/pagination/

:::tip[In plain words]
Page numbers with arrows, to move through long lists page by page.
:::

Moves through long results one page at a time: `‹ 1 … 4 5 6 … 20 ›`. `page` counts from 1; `siblings` pages show on each side of it and `boundaries` at each end. `compact` shows `4 / 20`.

`ui.pagination` · Category: [Navigation](https://albakore.github.io/pyzed/library/#navigation) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#pagination)

**Code:**

```python
pz.column([
    ui.pagination(5, 20, on_page),
    ui.pagination(1, 3, on_page),
    ui.pagination(4, 20, on_page, compact=True),
], spacing=12, align="center")
```

:::note[New]
This component was added recently. Tell us how it works for you in the [issue tracker](https://github.com/albakore/pyzed/issues).
:::

## When to use

- Under a table or a list of search results split into pages.
- When everything fits in one scroll, a [Virtual list](https://albakore.github.io/pyzed/library/virtual-list/) is simpler.

## Usage

```python
import pyzed as pz
from pyzed import ui

pz.column([
    ui.pagination(5, 20, on_page),
    ui.pagination(1, 3, on_page),
    ui.pagination(4, 20, on_page, compact=True),
], spacing=12, align="center")
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in Pyzed Dark or Pyzed Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Options

```python
ui.pagination(2, 5, on_page)
```

```python
ui.pagination(10, 40, on_page)
```

```python
ui.pagination(10, 40, on_page, siblings=2)
```

```python
ui.pagination(3, 12, on_page, compact=True)
```

### Size

```python
ui.pagination(4, 9, on_page, size="sm")
```

```python
ui.pagination(4, 9, on_page, size="lg")
```

## Events

Pagination has **1 event of its own**, listed below. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

| Event | Handler gets | When |
| --- | --- | --- |
| `on_change` | `(page)` | Called with the page that was clicked. |

**Handler gets** is what Pyzed passes to your callable: `()` means nothing. Any handler can also be a message (a value that is not callable), which goes to `update()`.

### Handling the events

The same events written three ways. **Imperative**: methods of the app change `self`. **Declarative**: the handlers produce messages and one `update()` decides. **Functional**: a pure function draws an immutable state and asks for changes with `set_state(...)`. See [Imperative, declarative and functional](https://albakore.github.io/pyzed/guides/code-styles/).

**Imperative:**

```python
class Results(pz.App):
    def __init__(self):
        self.page = 1

    def go(self, page):                  # on_change: the page that was clicked, from 1
        self.page = page

    def view(self):
        return pz.column([ui.label(f"Results of page {self.page}"),
                          ui.pagination(self.page, 20, self.go)], spacing=8)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Page:
    number: int

class Results(pz.App):
    page = 1

    def update(self, message):
        match message:
            case Page(number):
                self.page = number

    def view(self):
        return ui.pagination(self.page, 20, Page)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    page: int = 1

def view(state, set_state):
    return ui.pagination(state.page, 20, lambda page: set_state(page=page))
```

## Properties and API

### `ui.pagination`

```python
pagination(
    page,
    pages,
    on_change=None,
    *,
    siblings=1,
    boundaries=1,
    size='md',
    compact=False,
    theme=None,
)
```

Page numbers with previous and next arrows (`‹ 1 … 4 5 6 … 20 ›`). Pages count from 1.

| Property | Default | Description |
| --- | --- | --- |
| `page` | *required* | The current page, from 1. |
| `pages` | *required* | How many pages there are. |
| `siblings` | `1` | Pages shown on each side of the current one. |
| `boundaries` | `1` | Pages always shown at each end. |
| `size` | `"md"` | `"sm"`, `"md"` or `"lg"`. |
| `compact` | `False` | Shows `4 / 20` between the arrows instead of the numbers. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

Events: `on_change` ([events](#events)).

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#pagination)

## Related

- [Table](https://albakore.github.io/pyzed/library/table/): A data table with a fixed header, sorting and selection.
- [Virtual list](https://albakore.github.io/pyzed/library/virtual-list/): A list that only builds the rows in view, for millions of items.
- [Breadcrumbs](https://albakore.github.io/pyzed/library/breadcrumbs/): `src › main.rs › fn main`, optionally clickable.
