# doc-builder **Repository Path**: mirrors_huggingface/doc-builder ## Basic Information - **Project Name**: doc-builder - **Description**: The package used to build the documentation of our Hugging Face repos - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-12-03 - **Last Updated**: 2026-07-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # doc-builder This is the package we use to build the documentation of our Hugging Face repos. ## Table of Contents - [doc-builder](#doc-builder) * [Installation](#installation) * [Previewing](#previewing) * [Doc building](#doc-building) * [Writing in notebooks](#writing-in-notebooks) * [Templates for GitHub Actions](#templates-for-github-actions) + [Enabling multilingual documentation](#enabling-multilingual-documentation) + [Redirects](#redirects) * [Fixing and testing doc-builder](#fixing-and-testing-doc-builder) * [Development Commands](#development-commands) + [Code Quality & Formatting](#code-quality--formatting) + [Testing](#testing) + [Development Workflow](#development-workflow) * [Writing documentation for Hugging Face libraries](#writing-documentation-for-hugging-face-libraries) + [Internal link to object](#internal-link-to-object) + [External link to object](#external-link-to-object) + [Tip](#tip) + [Framework Content](#framework-content) + [Options](#options) + [Anchor link](#anchor-link) + [LaTeX](#latex) + [Code Blocks](#code-blocks) + [Stretch Tables](#stretch-tables) + [Inference Snippet](#inference-snippet) * [Writing API documentation (Python)](#writing-api-documentation-python) + [Autodoc](#autodoc) + [Code Blocks from file references](#code-blocks-from-file-references) + [Writing source documentation](#writing-source-documentation) + [Description](#description) + [Arguments](#arguments) + [Attributes](#attributes) + [Parameter typing and default value](#parameter-typing-and-default-value) + [Returns](#returns) + [Yields](#yields) + [Raises](#raises) + [Directives for Added, Changed, Deprecated](#directives-for-added-changed-deprecated) * [Developing svelte locally](#developing-svelte-locally) ## Installation You can install from PyPi with ```bash pip install hf-doc-builder ``` To install from source, clone this repository then ```bash cd doc-builder pip install -e . ``` ## Previewing To preview the docs, use the following command: ```bash doc-builder preview {package_name} {path_to_docs} ``` For example: ```bash doc-builder preview datasets ~/Desktop/datasets/docs/source/ ``` **`preview` command only works with existing doc files. When you add a completely new file, you need to update `_toctree.yml` & restart `preview` command (`ctrl-c` to stop it & call `doc-builder preview ...` again). ## Doc building To build the documentation of a given package, use the following command: ```bash #Add --not_python_module if not building doc for a python lib doc-builder build {package_name} {path_to_docs} --build_dir {build_dir} ``` For instance, here is how you can build the Datasets documentation (requires `pip install datasets[dev]`) if you have cloned the repo in `~/git/datasets`: ```bash doc-builder build datasets ~/git/datasets/docs/source --build_dir ~/tmp/test-build ``` This will generate MDX files that you can preview like any Markdown file in your favorite editor. To have a look at the documentation in HTML, you need to install node version 14 or higher. Then you can run (still with the example on Datasets) ```bash doc-builder build datasets ~/git/datasets/docs/source --build_dir ~/tmp/test-build --html ``` which will build HTML files in `~/tmp/test-build`. You can then inspect those files in your browser. ### Specifying a version By default, `doc-builder` automatically determines the version: - For **Python modules**: uses the package's `__version__` attribute (or `main` for dev versions) - For **non-Python modules** (with `--not_python_module`): uses the default branch name (typically `main`) You can override this behavior with the `--version` flag: ```bash # Build documentation for a specific semantic version (must start with "v") doc-builder build transformers ~/git/transformers/docs/source --build_dir ~/tmp/test-build --version v4.30.0 # Build documentation for the default branch (non-Python modules) doc-builder build hub ~/git/hub-docs/docs/source --build_dir ~/tmp/test-build --not_python_module --version main ``` **Important**: When specifying a semantic version with `--version`, it **must start with the letter "v"** (e.g., `v1.0.0`, `v2.3.1`). For branch names like `main`, `master`, or other default branches, the "v" prefix is not required. ### Page-level HTML build cache Building the HTML docs of a large library (e.g. `transformers`) prerenders hundreds of pages, even when a commit only changed one of them. `--html_page_cache` enables a page-level cache so that only pages whose generated content changed are prerendered again: ```bash # cache in a Hugging Face storage bucket (https://huggingface.co/docs/hub/storage-buckets) ... doc-builder build datasets ~/git/datasets/docs/source --build_dir ~/tmp/test-build --html \ --html_page_cache hf://buckets/hf-doc-build/doc-build-cache # ... or in a local directory doc-builder build datasets ~/git/datasets/docs/source --build_dir ~/tmp/test-build --html \ --html_page_cache ~/tmp/doc-page-cache --html_page_cache_write ``` A page is reused when its generated MDX (post autodoc and internal-link resolution — so docstring changes invalidate exactly the affected pages), the `kit` folder, and the doc-builder version are unchanged. Keys are version-normalized: pages that only differ by the built version (e.g. `main` vs `pr_123` in resolved internal links) share a cache entry, and on a cross-version reuse the page URLs are rewritten to the target version while asset URLs stay pinned to the source version, whose content-hashed assets remain hosted. `--html_page_cache_write` also stores freshly built pages. Only enable it on trusted builds (the shared GitHub workflows enable it on main-branch builds and keep PR builds read-only), so that untrusted code cannot poison the cache. Cache failures are never fatal: any error degrades to building the affected pages. ### Building without installing heavy dependencies doc-builder imports the documented library to read docstrings with `inspect` — necessary because libraries like `transformers` construct docstrings dynamically. Historically that meant installing the library's full dependency tree (torch, GPU wheels, custom containers) just to build documentation. doc-builder mocks heavy dependencies instead, so only the documented library itself (and its light dependencies) needs to be installed. Each library's registry file `src/doc_builder/mock_deps/.txt` records both sides of that split: - bare lines name the heavy dependencies to **mock** at build time (applied automatically by `doc-builder build`/`preview`); - `real:` lines name the light dependencies a doc build must **install for real** (things the library imports unguarded, version-pinned requirements, packages used through `isinstance` checks, ...); - `nodeps:` lines are also installed for real but with `--no-deps`, because their own dependency trees pull in heavy packages (e.g. `accelerate`, whose install requires torch). `doc-builder light-install ` installs the `real:`/`nodeps:` dependencies (via `uv`), so a full light setup is: ```bash uv pip install ./accelerate --no-deps doc-builder light-install accelerate doc-builder build accelerate ~/git/accelerate/docs/source --build_dir ~/tmp/test-build # torch and deepspeed are mocked automatically (src/doc_builder/mock_deps/accelerate.txt) ``` Install the library before running `light-install`: bare registry names are pinned to the library's own declared version ranges (e.g. transformers' `tokenizers>=0.22,<=0.23`), so registry files never have to chase upstream pins. `light-install` exits with code 3 when the library has no registry entry — `light-install --check` tests this without installing anything, which is how the shared GitHub workflows decide between the light path and a full `[dev]` install. For a library without a registry entry (or to experiment), `--mock_deps torch,...` adds extra mocks on top of the registry. The registry key is the doc-builder *library name* as passed to `doc-builder build` — for namespace subpackages that is the dotted module name (e.g. `mock_deps/optimum.intel.txt`), and libraries that build under a shared name share a file (optimum-onnx builds as `optimum`). A registered name may itself be a dotted submodule (e.g. `optimum.onnxruntime`), which mocks a missing sibling inside an installed namespace package. The mocked packages are importable, pass the `importlib.util.find_spec` + `importlib.metadata` availability checks HF libraries use, are subclassable without affecting the real subclass's signature or docstring, behave as pass-through decorators, and render in signatures like the real objects (`repr(torch.float32) == "torch.float32"`). Packages that are actually installed are never mocked, and `doc-builder preview` supports the flag too. Fidelity, verified on `accelerate` built without torch installed: 46/48 pages byte-identical to a real-torch build; the two remaining pages differ only in typing paths rendered from the public import path instead of the internal one (e.g. `torch.nn.Module` instead of `torch.nn.modules.module.Module`). ### Notebook conversion `doc-builder` can also automatically convert some of the documentation guides or tutorials into notebooks. This requires two steps: - add `[[open-in-colab]]` in the tutorial for which you want to build a notebook - add `--notebook_dir {path_to_notebook_folder}` to the build command. ### Runnable code blocks `doc-builder` recognizes runnable Python fences tagged with `runnable` or `runnable: