---
title: Python module docs
description: Render Python modules, `{{< doc ... >}}` shortcodes, and live API signatures
  for `fastship`.
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 [`py_page_specs`](pydocs.md#py_page_specs) 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')
```

## docs\_base\_url

```python
def docs_base_url(
    root:str | pathlib._local.Path, # Project root directory
    data:dict=None, # Optional parsed `pyproject.toml` mapping
    ship:dict=None, # Optional normalized fastship settings dict
)->str: # Published docs base URL ending with `/`, or `""` when unknown
```

*Return the published docs base URL from `site-url`, `[project.urls].Documentation`, or inferred GitHub Pages URL.*

## project\_defaults

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

*Derive default site settings (`title`, `description`, `site_url`, `tools`) from native manifests and git remote.*

## 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, source_link:bool=True, lookup=None
):
````

*Markdown renderer for Python module pages, `{{< doc ... >}}` shortcodes, and [`show_doc`](showdoc.md#show_doc).*

### 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`)
    lookup=None, # Optional `DocLookup` instance reused for source links
    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`)
    lookup=None, # Optional `DocLookup` instance reused for source links
)->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
    lookup=None, # Optional `DocLookup` instance reused for source links
)->str:
```

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

## Package discovery

Discover uncovered hand-written `.py` modules for generated `api/` pages.

### py\_page\_specs

```python
def py_page_specs(
    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
    data:dict=None, # Optional parsed `pyproject.toml` mapping
)->tuple[list[tuple[str, pathlib._local.Path, pathlib._local.Path, dict]], dict[str, tuple[str, str]], dict[str, str]]: # `(specs, mod_pages, mod_urls)`
```

*Discover uncovered and covered `.py` modules and their page frontmatter via AST without importing package code.*
