# Reference browser

> Built-in, searchable docs like Godot's: a class list next to the open page. A Pyzed component for Python desktop GUIs, with examples and its API.

Source: https://albakore.github.io/pyzed/library/reference-browser/

:::tip[In plain words]
A help window with the documentation inside the app, searchable and offline.
:::

The built-in reference: a searchable class list (the library, your components and any section you `reference.register`) next to the page that is open, with back and forward, internal links and an Open online button. Keep a `pyzed.reference.Browser` in your state; the browser changes it. Links that leave the reference go to `on_open_url`. See [Built-in reference](https://albakore.github.io/pyzed/editor/reference/).

`ui.reference_browser` · Category: [Code and tools](https://albakore.github.io/pyzed/library/#code-and-tools) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#reference_browser)

**Code:**

```python
from pyzed import reference

help = reference.Browser("pyzed.ui.button")
ui.reference_browser(help, on_open_url=open_in_browser, sidebar_width=230)
```

## When to use

- In a Help tab or window of an app built on Pyzed, so its users can read the API offline.
- With your own `reference.Provider` sections, as your app's help.

## Usage

```python
from pyzed import ui

from pyzed import reference

help = reference.Browser("pyzed.ui.button")
ui.reference_browser(help, on_open_url=open_in_browser, sidebar_width=230)
```

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

### Pages

```python
from pyzed import reference

ui.reference_browser(reference.Browser("pyzed.ui.badge"), sidebar_width=200)
```

```python
from pyzed import reference

ui.reference_browser(reference.Browser("pyzed.row"), sidebar_width=200)
```

## Events

Reference browser 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_open_url` | `(url)` | Called with the URL of a link that leaves the reference (open it in the browser). |

**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
import webbrowser

from pyzed import reference

class Help(pz.App):
    def __init__(self):
        self.browser = reference.Browser("pyzed.ui.button")

    def view(self):
        return ui.reference_browser(self.browser, on_open_url=webbrowser.open)   # links to the web
```

**Declarative:**

```python
from dataclasses import dataclass

from pyzed import reference

@dataclass
class OpenUrl:
    url: str

class Help(pz.App):
    browser = reference.Browser("pyzed.ui.button")

    def update(self, message):
        match message:
            case OpenUrl(url):
                print("open", url)

    def view(self):
        return ui.reference_browser(self.browser, on_open_url=OpenUrl)
```

**Functional:**

```python
from dataclasses import dataclass

from pyzed import reference

BROWSER = reference.Browser("pyzed.ui.button")

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

def view(state, set_state):
    return ui.reference_browser(BROWSER, on_open_url=lambda url: set_state(last_url=url))
```

## Properties and API

### `ui.reference_browser`

```python
reference_browser(
    browser,
    *,
    on_open_url=None,
    sidebar_width=270,
    width='fill',
    height='fill',
    theme=None,
)
```

The built-in reference, like Godot's: a searchable class list next to the page that is open.

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

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

## Related

- [Shortcuts](https://albakore.github.io/pyzed/library/shortcuts/): A searchable list of key bindings, grouped.
- [About](https://albakore.github.io/pyzed/library/about/): An About box with icon, version, details and links.
- [Markdown](https://albakore.github.io/pyzed/library/markdown/): Renders Markdown with the theme's colors.
