# Markdown

> Renders Markdown with the theme's colors. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

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

:::tip[In plain words]
Shows formatted text (titles, lists, links) written in the simple Markdown format.
:::

Renders Markdown (headings, lists, code blocks, quotes, tables, links) with the theme's text, link and code colors. `on_link(url)` receives clicked links. `pz.markdown` is the base widget with explicit colors.

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

**Code:**

```python
ui.markdown('''## Release notes

Pyzed **0.4** adds the *component designer*, `pyzed build` and [styles](https://iced.rs).

- Track grids and flex
- Ready-made dialogs
''', width=460, on_link=on_link)
```

## When to use

- For READMEs, release notes, help pages and chat messages.

## Usage

```python
from pyzed import ui

ui.markdown('''## Release notes

Pyzed **0.4** adds the *component designer*, `pyzed build` and [styles](https://iced.rs).

- Track grids and flex
- Ready-made dialogs
''', width=460, on_link=on_link)
```

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

### Elements

```python
ui.markdown("## Title\nSome **bold** and *italic* text.", width=320)
```

```python
ui.markdown("- Fast\n- Native\n- Pythonic\n\n1. Install\n2. Run", width=320)
```

```python
ui.markdown("Run `pyzed new`:\n\n```python\nimport pyzed as pz\n```", width=320)
```

```python
ui.markdown("> Desktop UIs in Python.\n\nSee [the docs](https://albakore.github.io/pyzed/).", width=320, on_link=on_link)
```

```python
ui.markdown("| Key | Action |\n| --- | --- |\n| ctrl-s | Save |\n| ctrl-p | Open |", width=320)
```

## Events

Markdown 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 | Of | Handler gets | When |
| --- | --- | --- | --- |
| `on_link` | `ui.markdown`, `pz.markdown` | `(url)` | Callable that receives the URL when a link is clicked. |

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

class Notes(pz.App):
    def __init__(self):
        self.last = None

    def follow(self, url):               # on_link: the URL of the clicked link
        self.last = url
        webbrowser.open(url)

    def view(self):
        return ui.markdown("Read the [docs](https://albakore.github.io/pyzed/) or the "
                           "[changelog](https://github.com/albakore/pyzed).", on_link=self.follow)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Link:
    url: str

class Notes(pz.App):
    def update(self, message):
        match message:
            case Link(url) if url.startswith("app:"):
                print("internal page", url[4:])   # links can drive the app, too
            case Link(url):
                print("open", url)

    def view(self):
        return ui.markdown("Go to [settings](app:settings) or the [docs](https://albakore.github.io/pyzed/).",
                           on_link=Link)
```

**Functional:**

```python
from dataclasses import dataclass

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

PAGES = {"home": "Welcome. See [about](about).", "about": "Made with Pyzed. Back [home](home)."}

def view(state, set_state):
    text = PAGES.get(state.page, "Not found. Back [home](home).")
    return ui.markdown(text, on_link=lambda url: set_state(page=url))
```

## Properties and API

### `ui.markdown`

```python
markdown(source, *, size=None, on_link=None, width=None, selectable=None, theme=None)
```

`pz.markdown` with the theme's text, link and code colors.

| Property | Default | Description |
| --- | --- | --- |
| `source` | *required* | The Markdown text. |
| `size` | `None` | Size. |
| `width` | `None` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `selectable` | `None` | Same as in `pz.markdown`; the selection uses the theme's `selection` color. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

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

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

### `pz.markdown`

```python
markdown(
    source,
    *,
    size=14.0,
    color=None,
    link_color=None,
    code_color=None,
    code_bg=None,
    on_link=None,
    width=None,
    selectable=None,
    selection_color=None,
)
```

Renders Markdown (headings, lists, code blocks, quotes, tables, links…). For the theme colors use `ui.markdown`.

| Property | Default | Description |
| --- | --- | --- |
| `source` | *required* | The Markdown text. |
| `size` | `14.0` | Size. |
| `color` | `None` | Text or icon color. |
| `link_color` | `None` | Link color. |
| `code_color` | `None` | Code color. |
| `code_bg` | `None` | Code background. |
| `width` | `None` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `selectable` | `None` | `True` lets the mouse select the text (drag, double-click a word, triple-click a block) and Ctrl+C copy it. A string names a selection group: the texts and Markdown of one group form a single selection, in the order they are drawn (the lines of a log, the messages of a chat). |
| `selection_color` | `None` | Color behind the selected text. |

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

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

## Related

- [Rich text](https://albakore.github.io/pyzed/library/rich-text/): One block of text mixing colors, weights and links.
- [Text](https://albakore.github.io/pyzed/library/text/): A piece of text with size, color, font and weight.
