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 documents python-dotenv.
Setup
Install the Package
npm i @vuesho/pythonInstall the Generator
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:
python3 -m venv .venv
source .venv/bin/activate
pip install ./node_modules/@vuesho/pythonPython 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.
Read the Package
fumapy-generate reads an installed package or a package in your project, and writes its API to <package>.json:
pip install httpx
fumapy-generate httpxThe 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),numpyorsphinx
Docstring text outside the style's sections is read as Markdown.
Add the Source and Plugin
createPython() reads the JSON, staticSource() returns its pages as a content source under baseDir, and pythonPlugin() renders the pages:
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" }),
}).plugins(recommendedPreset(), pythonPlugin());Run vuesho dev and open /api/httpx. To give the api folder a title, add a meta file, content/api/meta.json.
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:
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:
const python = createPython({ file: "httpx.json", groupBy: "none" }); 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
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:
createSite({
docs: docs.toSource(),
api: python.staticSource({ baseDir: "api" }),
api: python.dynamicSource({ baseDir: "api", staleTime: 60_000 }),
});To publish a new version without a build, replace the JSON in the server's output, like .output/server/httpx.json.
Customization
To change the look, copy the components with the CLI and edit them:
npx vuesho add components --from @vuesho/pythonOr replace the python body in your theme, 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:
import { katex } from "@mdit/plugin-katex";
const python = createPython({
file: "httpx.json",
// Math in docstrings, on these pages only.
markdownItPlugins: [katex],
});Options
createPython()
Prop
Type
file is the JSON's path, relative to the folder the site runs in.
staticSource() and dynamicSource()
baseDir is the pages' folder, and dynamicSource() also takes staleTime, in milliseconds.