# Stepper

> The steps of a wizard, with checks on the done ones. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

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

:::tip[In plain words]
Numbered circles that show which step of a process you are on.
:::

Shows where the user is in a process of several steps. Done steps get a check, the `current` one is ringed and the line between them fills as you advance. `on_select` lets the user go back to a step.

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

**Code:**

```python
pz.container(ui.stepper(["Cart", "Address", "Payment", "Done"], 2, on_step), width=520)
```

:::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

- For sign-ups, checkouts, installers and other wizards.
- For sections the user can open in any order, use a [Tab bar](https://albakore.github.io/pyzed/library/tab-bar/).

## Usage

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

class Checkout(pz.App):
    def __init__(self):
        self.step = 0

    def view(self):
        return pz.column([
            ui.stepper(["Cart", "Address", "Payment"], self.step, lambda i: setattr(self, "step", i)),
            ui.button("Next", lambda: setattr(self, "step", min(self.step + 1, 3)), variant="solid"),
        ], spacing=16)
```

## 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
pz.container(ui.stepper(["Cart", "Address", "Payment"], 1, on_step), width=420)
```

```python
ui.stepper([("Account", "Name and email"), ("Address", "Where it goes"), ("Payment", "Card or transfer")], 1, on_step, vertical=True)
```

```python
pz.container(ui.stepper(["Upload", "Check", "Publish"], 3, on_step, tint="success"), width=420)
```

### States and events

```python
pz.container(ui.stepper(["Cart", "Address", "Payment"], 1, on_step, errors=[1]), width=420)
```

```python
pz.container(ui.stepper(["One", "Two", "Three"], 0), width=420)
```

## Events

Stepper 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_select` | `(index)` | Called with the index of a clicked step. |

**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 Checkout(pz.App):
    def __init__(self):
        self.step = 1

    def go(self, index):                 # on_select: the index of the clicked step
        self.step = index

    def view(self):
        return pz.column([
            ui.stepper(["Cart", "Address", "Payment", "Done"], self.step, self.go),
            ui.button("Next", lambda: self.go(min(self.step + 1, 4)), variant="solid"),
        ], spacing=16)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class GoTo:
    index: int

class Checkout(pz.App):
    step = 1

    def update(self, message):
        match message:
            case GoTo(index):
                self.step = index

    def view(self):
        return ui.stepper(["Cart", "Address", "Payment"], self.step, GoTo)
```

**Functional:**

```python
from dataclasses import dataclass

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

def view(state, set_state):
    return ui.stepper(["Cart", "Address", "Payment"], state.step, lambda index: set_state(step=index))
```

## Properties and API

### `ui.stepper`

```python
stepper(
    steps,
    current,
    on_select=None,
    *,
    vertical=False,
    linear=True,
    errors=(),
    tint='accent',
    theme=None,
)
```

The steps of a wizard or a checkout: done ones get a check, the current one is ringed and the line between them fills as you advance.

| Property | Default | Description |
| --- | --- | --- |
| `steps` | *required* | Titles or `(title, description)` tuples. |
| `current` | *required* | Index of the step in progress (`len(steps)` once all are done). |
| `vertical` | `False` | Stacks the steps, with each description under its title. |
| `linear` | `True` | Only done steps and the current one can be clicked. |
| `errors` | `()` | Indexes of steps drawn in the error color. |
| `tint` | `"accent"` | Color of the done steps and the line. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

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

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

## Related

- [Progress](https://albakore.github.io/pyzed/library/progress/): A thin progress bar in a semantic color.
- [Tab bar](https://albakore.github.io/pyzed/library/tab-bar/): Editor tabs with icons, dirty dots and close buttons.
- [Breadcrumbs](https://albakore.github.io/pyzed/library/breadcrumbs/): `src › main.rs › fn main`, optionally clickable.
