# Diff view

> An edit as colored lines: added in green, removed in red. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/diff-view/

:::tip[In plain words]
Shows what changed in a file: new lines in green, deleted lines in red.
:::

`ui.diff_view(diff)` shows an edit: the file's name, then the lines that changed (added in green, removed in red) with two lines of context around each change; changes far apart are split by `⋯`. `diff` is a dict with `path`, `old` and `new`, the whole texts before and after, and `max_lines` limits how many lines show before `… N more lines`. It is what a [Tool call](https://albakore.github.io/pyzed/library/tool-call/) shows for the edits it made.

`ui.diff_view` · Category: [AI agents](https://albakore.github.io/pyzed/library/#ai-agents) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#diff_view)

**Code:**

```python
old = "def save(path):\n    data = load()\n    write(path, data)\n    return True\n"
new = "def save(path, data):\n    write(path, data)\n    log(path)\n    return True\n"
pz.container(ui.diff_view({"path": "app/storage.py", "old": old, "new": new}), width=420)
```

:::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 show what an agent or a tool changed in a file.
- To review a change before saving it, next to Keep and Reject buttons.
- For the counts alone (`+12 −3`), use [Diff stat](https://albakore.github.io/pyzed/library/diff-stat/).

## Usage

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

old = "def save(path):\n    data = load()\n    write(path, data)\n    return True\n"
new = "def save(path, data):\n    write(path, data)\n    log(path)\n    return True\n"
pz.container(ui.diff_view({"path": "app/storage.py", "old": old, "new": new}), width=420)
```

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

Two lines of context surround each change; changes further apart are split by `⋯`.

```python
pz.container(ui.diff_view({"path": "app.py", "old": "x = 1\ny = 2\n", "new": "x = 10\ny = 2\n"}),
             width=300)
```

```python
pz.container(ui.diff_view({"path": "main.py", "old": "", "new": "import pyzed as pz\n\npz.run(App)\n"}),
             width=300)
```

```python
pz.container(ui.diff_view({"path": "todo.txt", "old": "milk\neggs\nbread\n", "new": "milk\n"}),
             width=300)
```

```python
old = "".join(f"line {i}\n" for i in range(1, 13))
new = old.replace("line 2\n", "line two\n").replace("line 11\n", "")
pz.container(ui.diff_view({"path": "notes.txt", "old": old, "new": new}), width=300)
```

```python
new = "".join(f"item {i}\n" for i in range(1, 11))
pz.container(ui.diff_view({"path": "list.txt", "old": "", "new": new}, max_lines=4), width=300)
```

## Events

Diff view has no events of its own: it shows what you give it. 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/).

## Properties and API

### `ui.diff_view`

```python
diff_view(diff, *, theme=None, max_lines=120)
```

An edit as Zed shows it in a thread: the file's name, then the changed lines (green added, red removed) with a few lines around them.

| Property | Default | Description |
| --- | --- | --- |
| `diff` | *required* | A dict with `path`, `old` and `new` (the texts before and after). |
| `theme` | `None` | Theme. |
| `max_lines` | `120` | Lines shown before `… N more lines`. |

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

## Related

- [Tool call](https://albakore.github.io/pyzed/library/tool-call/): One step of an AI agent: what it did, its result, and Allow or Reject.
- [Diff stat](https://albakore.github.io/pyzed/library/diff-stat/): `+12 −3` with GitHub-style blocks.
- [Edits bar](https://albakore.github.io/pyzed/library/edits-bar/): The files an agent changed, with Keep all and Reject all.
