MkDocs is a fast, simple, and beautiful static site generator designed for building project documentation. It uses Markdown files to create a professional documentation website with minimal configuration.
MkDocs provides the following features:
- Simple and intuitive project structure
- Markdown-based content creation
- Live preview development server
- Multiple themes including Material for MkDocs
- Customizable navigation
- Full-text search capability
- Easy deployment to GitHub Pages or other hosting
- Plugin system for extended functionality
- Code highlighting
- Table of contents generation
MkDocs is included as a development dependency:
# Install with other development dependencies
uv sync --devTo install it directly:
uv pip install mkdocsIn this project, MkDocs is used to:
- Generate the project documentation website
- Provide a searchable interface for documentation
- Organize documentation in a logical structure
- Enable easy navigation between documentation sections
- Facilitate documentation updates alongside code changes
MkDocs is configured in the mkdocs.yml file at the root of the project:
site_name: Python Starting Project
site_description: A modern Python project template with best practices
site_author: Your Name
site_url: https://example.com/
theme:
name: material # (1)
features:
- navigation.instant
- navigation.tracking
- navigation.indexes
- navigation.top
- search.highlight
- search.share
- content.code.copy # (2)
plugins:
- search
- mkdocstrings: # (3)
handlers:
python:
options:
show_source: true
markdown_extensions: # (4)
- pymdownx.highlight
- pymdownx.superfences
- pymdownx.inlinehilite
- pymdownx.tabbed
- pymdownx.critic
- admonition
- toc:
permalink: true
nav:
- Home: index.md
- Getting Started: getting-started.md
- Overview: overview.md
- Development:
- Workflow: development/workflow.md
- Pre-commit Hooks: development/pre-commit-hooks.md
- Technologies:
- Package Management:
- UV: technologies/package-management/uv.md
- Code Quality:
- Ruff: technologies/code-quality/ruff.md
- API Reference: api/- Material theme provides a modern, responsive design
- Adds copy buttons to all code blocks
- MkDocstrings plugin generates API documentation from docstrings
- Markdown extensions enhance the basic Markdown syntax
A typical MkDocs project structure:
my-project/
├── docs/
│ ├── index.md
│ └── getting-started.md
└── mkdocs.yml
To preview the documentation locally:
=== "Using poe tasks"
```bash linenums="1" # Start the development server uv run poe docs-serve
# Build the static site
uv run poe docs-build
# Deploy to GitHub Pages
uv run poe docs-deploy
```
=== "Using direct commands"
```bash linenums="1" # Start the development server uv run mkdocs serve
# Build the static site
uv run mkdocs build
# Deploy to GitHub Pages
uv run mkdocs gh-deploy
```
MkDocs uses standard Markdown syntax:
# Page Title
## Section
This is a paragraph with **bold** and *italic* text.
- List item 1
- List item 2
1. Numbered item 1
2. Numbered item 2
[Link text](https://example.com)
=== "Basic Code Block"
markdown linenums="1" ```python def hello_world(): print("Hello, world!") ```
=== "With Line Numbers"
markdown linenums="1" ```python linenums="1" def hello_world(): print("Hello, world!") ```
=== "With Annotations"
````markdown linenums="1"
python def hello_world(): # (1) print("Hello, world!") # (2)
1. Function definition
2. Print statement that outputs "Hello, world!"
````
!!! note "Custom Title"
This is a note admonition with a custom title.
!!! warning
This is a warning admonition.
!!! tip
This is a tip admonition.- Organize documentation logically: Structure your documentation in a way that makes sense for users.
- Keep documentation up-to-date: Update documentation when code changes.
- Use descriptive page titles: Make it easy for users to find what they're looking for.
- Include examples: Provide code examples and use cases.
- Use admonitions for important information: Highlight warnings, notes, and tips.
- Add screenshots when helpful: Visual aids can improve understanding.
- Maintain a consistent style: Use a consistent writing style throughout.
- Link related content: Cross-reference related documentation.
- Test documentation: Ensure code examples work and instructions are accurate.
- Get feedback: Ask users for feedback on documentation clarity.
Create a custom theme:
mkdocs/
├── custom_theme/
│ ├── main.html
│ └── css/
│ └── style.css
Configure in mkdocs.yml:
theme:
name:
custom_dir: mkdocs/custom_theme/Add plugins to extend functionality:
plugins:
- search
- minify: # (1)
minify_html: true
- git-revision-date-localized: # (2)
type: date- Minifies HTML output for faster loading
- Adds last updated dates to pages based on git history
If navigation isn't updating:
- Check the
navsection inmkdocs.yml - Ensure file paths are correct
- Restart the development server
If you encounter build errors:
- Check for syntax errors in Markdown files
- Verify that all linked files exist
- Check for configuration errors in
mkdocs.yml
If search isn't working:
- Ensure the search plugin is enabled
- Rebuild the documentation
- Check for JavaScript errors in the browser console