---
description: Integrate external tools using pre- and post-build hooks for things like generating files.
---

# Hooks

Doctopus can run commands before and after a build.

Hooks are configured in `doctopus.yaml` as argv arrays:

```yaml
hooks:
  pre_build: ["uv", "run", "--script", "hooks/pre_build.py"]
  post_build: ["uv", "run", "--script", "hooks/post_build.py"]
```

Each array is executed directly. The first item is the executable. All following items are passed as arguments.

Hooks run from the directory that contains `doctopus.yaml`. That makes relative paths like `hooks/pre_build.py` resolve from your project root.

## When Hooks Run

- `pre_build` runs before Doctopus starts generating output.
- `post_build` runs after the site has been written successfully.

If a hook exits with a non-zero status, the build fails.

## Recommendation: Use `uv`

For Python hooks, prefer [`uv`](https://docs.astral.sh/uv/). It keeps hook scripts easy to run and solves dependency management without requiring a separate virtual environment setup.

One nice pattern is a self-contained script with dependency metadata in comments:

```python
# /// script
# dependencies = [
#   "pillow>=11",
# ]
# ///

from pathlib import Path

Path("docs/generated.md").write_text("# Generated\n", encoding="utf-8")
```

Then run it with:

```yaml
hooks:
  pre_build: ["uv", "run", "--script", "hooks/pre_build.py"]
```

This keeps hook dependencies close to the script that needs them.

<!--
Sitemap

URL: https://doctopus.frankmayer.dev/index.md
Title: doctopus
Description: The simplest way to write beautiful documentation

URL: https://doctopus.frankmayer.dev/v0.3.0/config.md
Title: Config
Description: Doctopus build configuration.

URL: https://doctopus.frankmayer.dev/v0.3.0/diagrams.md
Title: Diagrams
Description: Different diagram tools.

URL: https://doctopus.frankmayer.dev/v0.3.0/favicon.md
Title: Favicon
Description: How to set a custom favicon for your docs.

URL: https://doctopus.frankmayer.dev/v0.3.0/homepage.md
Title: Homepage
Description: The special homepage (`/index.html`).

URL: https://doctopus.frankmayer.dev/v0.3.0/hooks.md
Title: Hooks
Description: Integrate external tools using pre- and post-build hooks for things like generating files.

URL: https://doctopus.frankmayer.dev/v0.3.0/index.md
Title: Index

URL: https://doctopus.frankmayer.dev/v0.3.0/math.md
Title: Math
Description: LaTeX math support.

URL: https://doctopus.frankmayer.dev/v0.3.0/syntax_highlighting.md
Title: Syntax Highlighting
Description: Code blocks with syntax highlighting.

URL: https://doctopus.frankmayer.dev/v0.3.1/config.md
Title: Config
Description: Doctopus build configuration.

URL: https://doctopus.frankmayer.dev/v0.3.1/diagrams.md
Title: Diagrams
Description: Different diagram tools.

URL: https://doctopus.frankmayer.dev/v0.3.1/favicon.md
Title: Favicon
Description: How to set a custom favicon for your docs.

URL: https://doctopus.frankmayer.dev/v0.3.1/homepage.md
Title: Homepage
Description: The special homepage (`/index.html`).

URL: https://doctopus.frankmayer.dev/v0.3.1/hooks.md
Title: Hooks
Description: Integrate external tools using pre- and post-build hooks for things like generating files.

URL: https://doctopus.frankmayer.dev/v0.3.1/index.md
Title: Index

URL: https://doctopus.frankmayer.dev/v0.3.1/math.md
Title: Math
Description: LaTeX math support.

URL: https://doctopus.frankmayer.dev/v0.3.1/syntax_highlighting.md
Title: Syntax Highlighting
Description: Code blocks with syntax highlighting.

URL: https://doctopus.frankmayer.dev/next/config.md
Title: Config
Description: Doctopus build configuration.

URL: https://doctopus.frankmayer.dev/next/diagrams.md
Title: Diagrams
Description: Different diagram tools.

URL: https://doctopus.frankmayer.dev/next/favicon.md
Title: Favicon
Description: How to set a custom favicon for your docs.

URL: https://doctopus.frankmayer.dev/next/homepage.md
Title: Homepage
Description: The special homepage (`/index.html`).

URL: https://doctopus.frankmayer.dev/next/hooks.md
Title: Hooks
Description: Integrate external tools using pre- and post-build hooks for things like generating files.

URL: https://doctopus.frankmayer.dev/next/math.md
Title: Math
Description: LaTeX math support.

URL: https://doctopus.frankmayer.dev/next/media.md
Title: Media
Description: More than just images.

URL: https://doctopus.frankmayer.dev/next/search.md
Title: Search
Description: Search pages by content.

URL: https://doctopus.frankmayer.dev/next/syntax_highlighting.md
Title: Syntax Highlighting
Description: Code blocks with syntax highlighting.
-->
