---
title: Python module docs
description: Render Python modules, `{{< doc ... >}}` shortcodes, and `pyproject.toml`
  site defaults for Octavo.
order: 6
---
Use [`render_mod`](pydocs.md#render_mod) or [`mod_page`](pydocs.md#mod_page) to turn a Python module's docstring, `# - key: val` frontmatter comments, top-level Markdown strings, and public classes/callables into Markdown in AST definition order. Use [`expand_docs`](pydocs.md#expand_docs) to expand `{{< doc sym [level=N] >}}` shortcodes in hand-written `.md` or `.qmd` pages, and [`pkg_pages`](pydocs.md#pkg_pages) to discover uncovered public modules in a package for automatic API documentation.

```python
py_frontmatter('"Mod."\n# - order: 6\n# - title: Python docs\nx = 1\n')
```

## Frontmatter and `{python}` fences

Extract `# - key: val` module metadata and prepare ```` ```{python} ```` blocks for static documentation rendering.

### py\_frontmatter

```python
def py_frontmatter(
    src:str, # Python module source text
)->dict: # Parsed YAML dict from leading `# - key: val` comments after the module docstring
```

*Parse `# - key: val` frontmatter comments immediately after a module's docstring.*

````
```{python}
assert py_frontmatter('"Doc."
````

# - order: 2

# - title: Core

') \=\= {'order': 2, 'title': 'Core'} assert py\_frontmatter('x \= 1 ') \=\= {} \`\`\`

### py\_fences

```python
def py_fences(
    txt:str, # Markdown or docstring text to scan
)->list[tuple[str, int]]: # `(code, line_offset)` pairs for each top-level `{python}` fence (`line_offset` is 0-based body start in `txt`)
```

*Extract executable `{python}` fenced code blocks and their 0-based body line offsets from Markdown `txt`.*

````
```{python}
assert py_fences('Hi
````

```python
x = 1
```

') \=\= \[('x \= 1', 3)\] \`\`\`

### clean\_doc\_md

```python
def clean_doc_md(
    txt:str, # Markdown or rendered docstring text
)->str: # Text with hidden `{python}` blocks dropped, `#| ` directives stripped, and `{python}` rendered as `python`
```

*Prepare Markdown `txt` with `{python}` fences for static docs, dropping `#| hide` blocks and stripping ` #|  ` directives.*

````
```{python}
src = 'Intro
````

```python
y = 2
```

' assert clean\_doc\_md(src) \=\= 'Intro

````python
y = 2
```'
    ```

## Rendering modules and shortcodes

Turn Python modules and `{{< doc ... >}}` shortcodes into Markdown pages with YAML frontmatter.

### DocRenderer

```python
def DocRenderer(
    sym, name:str | None=None, title_level:int=3, maxline:int=110
):
````

*Markdown renderer for [`expand_docs`](pydocs.md#expand_docs) and [`render_mod`](pydocs.md#render_mod) with a heading.*

### render\_mod

```python
def render_mod(
    mod:module, # Imported Python module to document
    level:int=2, # Base heading level for top-level symbols (class methods use `sym_level + 1`)
    renderer:type=DocRenderer, # Renderer class or callable taking `(sym, title_level=level)`
    skip_first:bool=False, # Omit paragraph 1 of the module docstring (when already in YAML `description:`)
    src:str=None, # Optional module source text (defaults to reading `mod.__file__`)
)->str:
```

*Render a module's docstring, top-level Markdown strings, and public classes/callables in definition order.*

### mod\_page

```python
def mod_page(
    mod_nm:str, # Dotted module name to import and render
    path:str | pathlib._local.Path=None, # Optional `.py` path (defaults to `mod.__file__`)
    level:int=2, # Heading level for top-level module symbols (class methods use `level + 1`)
    renderer:type=DocRenderer, # Renderer class or callable taking `(sym, title_level=level)`
)->str:
```

*Render a standalone Markdown page (YAML frontmatter \+ module docs) for `mod_nm`.*

### expand\_docs

```python
def expand_docs(
    text:str, # Markdown or Quarto source text
    renderer:type=DocRenderer, # Renderer class or callable taking `(sym, title_level=level)`
)->str:
```

*Expand `{{< doc sym [level=N] >}}` shortcodes in `text`, skipping code blocks and inline code.*

## Package discovery and `pyproject.toml` defaults

Discover uncovered hand-written `.py` modules, determine their sidebar placement (`api/` vs root for flat `nbdev` projects), and derive site defaults from `pyproject.toml`.

### docs\_base\_url

```python
def docs_base_url(
    root:str | pathlib._local.Path, # Project root directory
    data:dict=None, # Parsed `pyproject.toml` mapping (read from `root` when omitted)
)->str: # Published docs base URL ending with `/`, or `""` when unknown
```

*Return the published docs base URL from `[project.urls].Documentation` or the GitHub Pages URL inferred from the repo slug.*

### pkg\_pages

```python
def pkg_pages(
    root:str | pathlib._local.Path, # Project root directory
    pkg_path:str | pathlib._local.Path=None, # Package directory (inferred from `pyproject.toml` when omitted)
    docs_dir:str | pathlib._local.Path=None, # Docs or notebooks directory to check for existing/covered pages
    excl:set | list=(), # Relative paths or directory names to exclude
    renderer:type=DocRenderer, # Renderer passed to `mod_page`
    render:bool=True, # Import and render discovered modules? False returns AST-only `mod_pages` and `mod_urls`
)->tuple[list[tuple[str, pathlib._local.Path, pathlib._local.Path, str]], dict[str, tuple[str, str]], dict[str, str]]: # `(auto_pages, mod_pages, mod_urls)`
```

*Discover uncovered hand-written `.py` modules and render them as standalone Markdown pages.*

Skips `_*.py`, `skill.py`, `guidance.py`, `# AUTOGENERATED!` files, modules without both a module docstring and `# - ...` frontmatter (or with `# - publish: false`), modules without public API, modules covered by a hand-written page, and excluded paths. Places generated pages under `api/` unless `docs_dir` has visible root `#| default_exp` notebooks and no `api/` subfolder. Returns `(auto_pages, mod_pages, mod_urls)` where `auto_pages` holds `(mod_nm, py_path, rel_md, md_text)`, `mod_pages` maps `mod_nm -> (html_rel, py_rel)`, and `mod_urls` maps `mod_nm -> md_rel`.

### pkg\_mod\_urls

```python
def pkg_mod_urls(
    root:str | pathlib._local.Path, # Project root directory
    pkg_path:str | pathlib._local.Path=None, # Package directory (inferred from `pyproject.toml` when omitted)
    docs_dir:str | pathlib._local.Path=None, # Docs or notebooks directory to check for existing/covered pages
    excl:set | list=(), # Relative paths or directory names to exclude
)->dict[str, str]: # Mapping of public module name to relative Markdown doc URL without importing modules
```

*Discover uncovered and covered `.py` module doc URLs via AST without importing any package code.*

### project\_defaults

```python
def project_defaults(
    root:str | pathlib._local.Path, # Project root containing `pyproject.toml`
    data:dict=None, # Optional parsed `pyproject.toml` mapping
)->dict: # Default Octavo site settings (snake-case) derived from `pyproject.toml`
```

*Derive default Octavo site settings (`title`, `description`, `site_url`, `tools`, `exclude`) from `pyproject.toml`.*
