# Rating

> Stars to give or show a score. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

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

:::tip[In plain words]
Little stars you click to say how much you like something.
:::

A row of stars (or any icon) where the first `value` are lit. Clicking a star sends its number to `on_change`; with `clearable` a click on the lit one sends 0. Without `on_change` it only shows the score, rounded to the nearest star.

`ui.rating` · Category: [Forms](https://albakore.github.io/pyzed/library/#forms) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#rating)

**Code:**

```python
pz.column([
    ui.rating(4, on_rate),
    ui.rating(3, on_rate, icon="heart", tint="error"),
    ui.rating(4.4),
], spacing=12)
```

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

- To rate an item, a song or an answer.
- To show an average score next to a product or a review.

## Usage

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

class Review(pz.App):
    def __init__(self):
        self.stars = 3

    def view(self):
        return ui.rating(self.stars, lambda stars: setattr(self, "stars", stars))
```

## 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.rating(3, on_rate)
```

```python
ui.rating(7, on_rate, max=10, size=14)
```

```python
ui.rating(4, on_rate, icon="heart", tint="error")
```

```python
ui.rating(2, on_rate, icon="thumbs-up", tint="accent", max=3)
```

### States and events

```python
ui.rating(4.4)
```

```python
ui.rating(3, on_rate, disabled=True)
```

## Events

Rating 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` | `(stars)` | Callable that receives the number of stars picked (0 to `max`). |

**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 Review(pz.App):
    def __init__(self):
        self.stars = 3

    def rate(self, stars):               # on_change: the number of stars (0 when cleared)
        self.stars = stars

    def view(self):
        return pz.column([ui.rating(self.stars, self.rate), ui.label(f"{self.stars} of 5")], spacing=8)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Rate:
    stars: int

class Review(pz.App):
    stars = 3

    def update(self, message):
        match message:
            case Rate(stars):
                self.stars = stars

    def view(self):
        return ui.rating(self.stars, Rate)   # the class builds the message from the stars
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    stars: int = 3

def view(state, set_state):
    return ui.rating(state.stars, lambda stars: set_state(stars=stars))
```

## Tips

:::tip
Focused (click it or press Tab), the arrows change the score by one and Home/End jump to 0 and `max`.
:::

## Properties and API

### `ui.rating`

```python
rating(
    value,
    on_change=None,
    *,
    max=5,
    icon='star',
    size=18,
    tint='warning',
    clearable=True,
    disabled=False,
    theme=None,
)
```

A row of stars (or any icon) to give or show a score. Clicking a star sends its number; with `clearable` a click on the lit one sends 0. Without `on_change` it only shows the value, rounded to the nearest star. Focused, the arrows change it by one.

| Property | Default | Description |
| --- | --- | --- |
| `value` | *required* | How many stars are lit (rounded). |
| `max` | `5` | How many stars there are. |
| `icon` | `"star"` | Lucide icon drawn for each star. |
| `size` | `18` | Icon size in pixels. |
| `tint` | `"warning"` | Color of the lit stars (a theme token or hex color). |
| `clearable` | `True` | A click on the current value sends 0. |
| `disabled` | `False` | Disables it (ignores clicks). |
| `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/#rating)

## Related

- [Slider](https://albakore.github.io/pyzed/library/slider/): Pick a number in a range by dragging.
- [Segmented](https://albakore.github.io/pyzed/library/segmented/): A group of mutually exclusive text buttons.
- [Toggle group](https://albakore.github.io/pyzed/library/toggle-group/): An icon-only segmented control.
