Skip to content

Add stable public Python API (mkdocs.build / mkdocs.serve) - #76

Merged
shenxianpeng merged 2 commits into
mainfrom
feature/python-api
Aug 4, 2026
Merged

shenxianpeng merged 2 commits into
mainfrom
feature/python-api

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Implements stable public Python API** (upstream mkdocs#1240 "API for running mkdocs"): a stable, documented, programmatic API for building and serving docs — no more from mkdocs.__main__ import build_command hacks or subprocess calls.

What changed

  • mkdocs.build() — programmatic equivalent of mkdocs build:
    • Loads the config from a file path, file-like object, or stdin (-), with Path accepted
    • Any config option can be overridden via keyword arguments (site_name, nav, strict, ...)
    • Fires plugin on_startup/on_shutdown events exactly like the CLI
    • Returns the loaded MkDocsConfig (e.g. config.site_dir)
  • mkdocs.serve() — programmatic equivalent of mkdocs serve (blocks until shutdown), with the same override semantics
  • CLI parity: mkdocs build / mkdocs serve commands now call the same public functions, so CLI and API can never drift apart
  • Widened commands.serve.serve()'s config_file annotation to str | IO | None (the CLI already passes file objects at runtime)
  • Docs: new Python API user-guide page (build/serve examples, programmatic config overrides, no-config-file pattern) + API reference entries in dev-guide/api.md (mkdocstrings)
  • Tests: mkdocs/tests/api_tests.py (build end-to-end incl. Path, file objects, kwargs overrides, dirty mode; serve argument forwarding); updated CLI serve tests for the normalized watch list

Verification

  • uvx prek run -a passes (incl. mypy)
  • Full test suite: 753 tests OK
  • Docs site builds cleanly; mkdocstrings renders both signatures with annotations
  • py.typed already ships with the package, so the new API is covered by PEP 561 type declarations

Examples

import mkdocs

config = mkdocs.build(config_file="mkdocs.yml", site_name="My project")
print(f"Built site to {config.site_dir}")

Add mkdocs.build() and mkdocs.serve() as thin, documented wrappers over
the internal commands, giving scripts and build tools a stable way to
build and serve documentation programmatically instead of calling CLI
internals or spawning subprocesses.

- mkdocs.build() loads the config (file or file object), fires plugin
  startup/shutdown events like the CLI does, and returns the loaded
  MkDocsConfig so callers can inspect site_dir etc. All config options
  can be overridden via keyword arguments; Path values are accepted.
- mkdocs.serve() mirrors the serve command and blocks until shutdown.
- The build/serve CLI commands now go through the same public functions,
  so the CLI and the API can never drift apart.
- New user guide page with examples plus API reference entries.
@github-actions github-actions Bot added the enhancement New feature or request label Aug 3, 2026
@shenxianpeng shenxianpeng added this to the v1.8.0 milestone Aug 4, 2026
@shenxianpeng
shenxianpeng merged commit dd530ad into main Aug 4, 2026
23 checks passed
@shenxianpeng
shenxianpeng deleted the feature/python-api branch August 4, 2026 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant