# Python

Reference pages from Python source code.

`@vuesho/python` generates reference pages for a Python package's modules and classes, with signatures, types, docstrings and each function's source. The [example](/api-docs/python/example) documents python-dotenv.

## Setup [#setup]

### Install the Package [#install-the-package-step]

```npm
npm i @vuesho/python
```

### Install the Generator [#install-the-generator-step]

The package includes `fumapy-generate`, a Python script that reads a Python package. The script needs Python 3.10 or later, so install it in a virtual environment with that version, and add `.venv` to `.gitignore`:

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install ./node_modules/@vuesho/python
```

<Callout title="Python Version">
macOS comes with Python 3.9. `python3 --version` prints your version. To create the environment with a newer Python, run `python3.13 -m venv .venv`.
</Callout>

### Read the Package [#read-the-package-step]

`fumapy-generate` reads an installed package or a package in your project, and writes its API to `<package>.json`:

```bash tab="From PyPI"
pip install httpx
fumapy-generate httpx
```

```bash tab="From your project"
fumapy-generate my_package
```

The command takes these options:

- `--dir <folder>`: the folder for the JSON file, the current folder by default
- `--docstring-style <style>`: the docstrings' section style, `google` (default), `numpy` or `sphinx`

Docstring text outside the style's sections is read as Markdown.

### Add the Source and Plugin [#add-the-source-and-plugin-step]

`createPython()` reads the JSON, `staticSource()` returns its pages as a content source under `baseDir`, and `pythonPlugin()` renders the pages:

```ts title="vuesho.config.ts"
import { createPython, pythonPlugin } from "@vuesho/python";
import { createSite, recommendedPreset } from "vuesho/config";
import { defineDocs } from "vuesho/content";

const docs = defineDocs();
const python = createPython({ file: "httpx.json" });

export default createSite({
  docs: docs.toSource(),
  api: python.staticSource({ baseDir: "api" }), // [!code highlight]
}).plugins(recommendedPreset(), pythonPlugin());
```

Run `vuesho dev` and open `/api/httpx`. To give the `api` folder a title, add a [meta file](/navigation#folders), `content/api/meta.json`.

## Pages [#pages]

Each module and class has a page, with a `module` or `class` badge in the sidebar. A module with classes or submodules is a folder, and its page is the folder's index:

```files
api
└── httpx
    ├── index.md
    ├── utils.md
    └── -_client
        ├── index.md
        └── Client.md
```

In a URL, a private module's name starts with `-`, like `-_client` in `/api/httpx/-_client/Client`. `groupBy: "none"` leaves out the package's folder, so the package is the index of `baseDir`:

```ts title="vuesho.config.ts"
const python = createPython({ file: "httpx.json", groupBy: "none" }); // [!code highlight]
```

The generated pages are in search and `llms.txt`, like your other pages. HTML in a docstring shows as text, not as elements.

### Live Updates [#live-updates]

`staticSource()` reads the JSON when the site starts, so restart `vuesho dev` after `fumapy-generate` writes a new one.

With `dynamicSource()`, the pages render on request, and the server reads the file again after `staleTime`, like any [dynamic source](/dynamic-rendering#dynamic-sources):

```ts title="vuesho.config.ts"
createSite({
  docs: docs.toSource(),
  api: python.staticSource({ baseDir: "api" }), // [!code --]
  api: python.dynamicSource({ baseDir: "api", staleTime: 60_000 }), // [!code ++]
});
```

To publish a new version without a build, replace the JSON in the server's output, like `.output/server/httpx.json`.

## Customization [#customization]

To change the look, copy the components with the CLI and edit them:

```bash
npx vuesho add components --from @vuesho/python
```

Or replace the `python` body in your [theme](/theme#views-and-page-bodies), which receives the page's hast tree in `tree` and the highlighted types in `highlighted`. `toVNodes()` from `vuesho/ui/hast` renders the tree, and `@vuesho/python/ui/components` has the components of the built-in body.

These pages use only the `highlight` option of the config's `markdown`, which also highlights the types and default values. `markdownItPlugins` on `createPython()` adds [markdown-it plugins](/markdown#markdown-it-plugins):

```ts title="vuesho.config.ts"
import { katex } from "@mdit/plugin-katex";

const python = createPython({
  file: "httpx.json",
  // Math in docstrings, on these pages only.
  markdownItPlugins: [katex], // [!code highlight]
});
```

## Options [#options]

### `createPython()` [#createpython]

### CreatePythonOptions

| Prop                 | Type                              | Description                                                                                                                                                                               |
| -------------------- | --------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `file`               | `string`                          | The JSON that `fumapy-generate` writes, as a path from the folder the site runs in. `staticSource()` reads it when the site starts, and `dynamicSource()` again once its files are stale. |
| `groupBy?`           | `PythonGroupBy \| undefined`      | Where the pages go: `module` puts them in a folder named after the package, and `none` at the root, where the package is the index. Default: `"module"`                                   |
| `markdownItPlugins?` | `MarkdownItPlugin[] \| undefined` | markdown-it plugins that the pages are parsed with, as `remarkPlugins` of `fumadocs-python`. The Markdown of the config doesn't apply to the pages.                                       |

`file` is the JSON's path, relative to the folder the site runs in.

### `staticSource()` and `dynamicSource()` [#staticsource-and-dynamicsource]

`baseDir` is the pages' folder, and `dynamicSource()` also takes `staleTime`, in milliseconds.
