# Timeline

> Events in order down a line. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

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

:::tip[In plain words]
A list of things that happened, in order, joined by a line.
:::

A vertical list of what happened, joined by a line: each `ui.TimelineItem` has a title, an optional `time`, `description` and `icon` in its dot. `on_press` makes the entries clickable.

`ui.timeline` · Category: [Data](https://albakore.github.io/pyzed/library/#data) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#timeline)

**Code:**

```python
pz.container(ui.timeline([
    ui.TimelineItem("Order placed", "We got your order.", time="09:12", icon="shopping-cart"),
    ui.TimelineItem("Shipped", "On its way with the courier.", time="14:30", icon="truck"),
    ui.TimelineItem("Delivered", time="Tomorrow", icon="package-check", tint="success"),
]), width=360)
```

:::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 the history of an order, a ticket or a file, or a changelog.
- For the steps still to come, use a [Stepper](https://albakore.github.io/pyzed/library/stepper/).

## Usage

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

pz.container(ui.timeline([
    ui.TimelineItem("Order placed", "We got your order.", time="09:12", icon="shopping-cart"),
    ui.TimelineItem("Shipped", "On its way with the courier.", time="14:30", icon="truck"),
    ui.TimelineItem("Delivered", time="Tomorrow", icon="package-check", tint="success"),
]), width=360)
```

## 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.timeline(["Created", "Reviewed", "Merged"]), width=300)
```

```python
pz.container(ui.timeline([ui.TimelineItem("Commit pushed", time="2 min ago", icon="git-commit-horizontal"), ui.TimelineItem("Checks passed", time="1 min ago", icon="check", tint="success")]), width=340)
```

```python
pz.container(ui.timeline([ui.TimelineItem("Started", time="10:00"), ui.TimelineItem("Paused", time="10:20", tint="warning"), ui.TimelineItem("Finished", time="11:05", tint="success")], dense=True), width=300)
```

```python
pz.container(ui.timeline([ui.TimelineItem("Ada commented", "Looks good to me!", time="Today", icon="message-circle", content=ui.facepile(["Ada", "Grace"]))]), width=340)
```

## Events

Timeline 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_press` | `(index)` | Called with the index of a clicked entry. |

**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 Orders(pz.App):
    def __init__(self):
        self.opened = None

    def open(self, index):               # on_press: the index of the clicked entry
        self.opened = index

    def view(self):
        return ui.timeline([
            ui.TimelineItem("Order placed", time="09:12", icon="shopping-cart"),
            ui.TimelineItem("Shipped", "On its way.", time="14:30", icon="truck",
                            tint="success" if self.opened == 1 else "accent"),
        ], on_press=self.open)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Open:
    index: int

class Orders(pz.App):
    opened = None

    def update(self, message):
        match message:
            case Open(index):
                self.opened = index

    def view(self):
        return ui.timeline(["Order placed", "Shipped", "Delivered"], on_press=Open)
```

**Functional:**

```python
from dataclasses import dataclass

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

def view(state, set_state):
    return ui.timeline(["Order placed", "Shipped", "Delivered"], on_press=lambda index: set_state(opened=index))
```

## Properties and API

### `ui.timeline`

```python
timeline(items, *, on_press=None, dense=False, theme=None)
```

Events in order down a line: a history, a changelog, the steps of a delivery. Each entry gets a dot joined to the next one.

| Property | Default | Description |
| --- | --- | --- |
| `items` | *required* | `ui.TimelineItem` objects, dicts with the same keys or plain titles. |
| `dense` | `False` | Small dots and less space. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

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

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

### `ui.TimelineItem`

```python
@dataclass
class TimelineItem(title, description=None, time=None, icon=None, tint='accent', content=None)
```

One entry of `ui.timeline`.

| Property | Default | Description |
| --- | --- | --- |
| `title` | *required* | Title. |
| `description` | `None` | Muted text under the title. |
| `time` | `None` | Text at the right of the title (a date, an hour). |
| `icon` | `None` | Icon inside the dot. |
| `tint` | `"accent"` | Color of the dot. |
| `content` | `None` | Any widget under the text. |

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

## Related

- [Stepper](https://albakore.github.io/pyzed/library/stepper/): The steps of a wizard, with checks on the done ones.
- [List item](https://albakore.github.io/pyzed/library/list-item/): A list row with icon, text and an end slot.
- [Console](https://albakore.github.io/pyzed/library/console/): A dev-tools style log with levels, filter, copy and clear.
