# Card

> A bordered surface with an optional title and actions. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

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

:::tip[In plain words]
A box with a border that keeps related things together, like a card on a table.
:::

A rounded, bordered surface that groups content. `title` and `actions` add a header row; `padding` and `width` control its box.

`ui.card` · Category: [Display](https://albakore.github.io/pyzed/library/#display) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#card)

**Code:**

```python
ui.card(
    pz.column([
        ui.muted("Pyzed draws the UI with Iced, in Rust."),
        pz.row([pz.hspace(), ui.button("Cancel", on_cancel, variant="ghost"),
                ui.button("OK", on_ok, variant="solid")], spacing=8),
    ], spacing=12),
    title="Card",
    actions=[ui.icon_button("ellipsis", on_menu, size="sm")],
    width=380,
)
```

## When to use

- To group related content on a page: a summary, a form section, a dashboard tile.
- For a dialog over the UI use [Modal](https://albakore.github.io/pyzed/library/modal/).

## Usage

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

ui.card(
    pz.column([
        ui.muted("Pyzed draws the UI with Iced, in Rust."),
        pz.row([pz.hspace(), ui.button("Cancel", on_cancel, variant="ghost"),
                ui.button("OK", on_ok, variant="solid")], spacing=8),
    ], spacing=12),
    title="Card",
    actions=[ui.icon_button("ellipsis", on_menu, size="sm")],
    width=380,
)
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One 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.
:::

### Content

```python
ui.card(ui.muted("A bordered surface."), width=260)
```

**} alt={**

```python
ui.card(ui.muted("Three files changed."), title="Changes", width=260)
```

```python
ui.card(ui.muted("Three files changed."), title="Changes",
        actions=[ui.icon_button("ellipsis", on_menu, size="sm")], width=260)
```

### Padding

```python
ui.card(ui.label("Compact"), padding=8)
```

```python
ui.card(ui.label("Default"))
```

```python
ui.card(ui.label("Roomy"), padding=28)
```

### States and events

```python
ui.card(ui.muted("Python app with a window"), title="Desktop app", on_press=on_press,
        selected=True, width=260)
```

```python
ui.card(ui.muted("3 files changed"), title="Commit", footer=[ui.button("Push", on_press)],
        elevated=True, width=260)
```

## Events

Card 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` | `()` | Makes the whole card clickable. |

**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 Plans(pz.App):
    def __init__(self):
        self.plan = "Pro"

    def view(self):
        return pz.row([
            ui.card(ui.muted(f"Plan {name}"), title=name, width=160, selected=name == self.plan,
                    on_press=lambda name=name: setattr(self, "plan", name))   # the whole card
            for name in ("Free", "Pro", "Team")
        ], spacing=8)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Choose:
    plan: str

class Plans(pz.App):
    plan = "Pro"

    def update(self, message):
        match message:
            case Choose(plan):
                self.plan = plan

    def view(self):
        return pz.row([ui.card(ui.muted(name), title=name, width=160, selected=name == self.plan,
                               on_press=Choose(name)) for name in ("Free", "Pro", "Team")], spacing=8)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    plan: str = "Pro"

def plan_card(name, selected, choose):        # a small function component
    return ui.card(ui.muted(name), title=name, width=160, selected=selected, on_press=choose)

def view(state, set_state):
    return pz.row([plan_card(n, n == state.plan, lambda n=n: set_state(plan=n))
                   for n in ("Free", "Pro", "Team")], spacing=8)
```

## Properties and API

### `ui.card`

```python
card(
    content,
    *,
    title=None,
    actions=(),
    padding=16,
    width=None,
    footer=(),
    on_press=None,
    selected=False,
    elevated=False,
    theme=None,
)
```

A bordered surface to group content, with an optional title and actions.

| Property | Default | Description |
| --- | --- | --- |
| `content` | *required* | Content: a `Widget`, a string or a number (converted to text). |
| `title` | `None` | Title. |
| `actions` | `()` | Extra widgets (usually buttons) shown as actions. |
| `padding` | `16` | Inner padding. |
| `width` | `None` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `footer` | `()` | Widgets at the bottom right (buttons). |
| `selected` | `False` | Outlines it in the accent color. |
| `elevated` | `False` | Adds a shadow. |
| `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/#card)

## Related

- [Modal](https://albakore.github.io/pyzed/library/modal/): A dialog surface with title, content and footer buttons.
- [Settings group](https://albakore.github.io/pyzed/library/settings-group/): A titled block of field rows, for settings screens.
- [Empty state](https://albakore.github.io/pyzed/library/empty-state/): A centered placeholder for empty lists and searches.
