Python
Generate API references from Python packages.
fumadocs-python documents a Python package from its source code and docstrings, with a page for each module and class. A Python command collects the package into a JSON file, which you load as a content source or convert into MDX files.
Setup
npm install fumadocs-python shikiGenerate the JSON file with the Python command:
# install it in the Python environment of your package
pip install ./node_modules/fumadocs-python
# writes httpx.json, `--dir` changes the output directory
fumapy-generate httpx
# parse docstring sections in NumPy or Sphinx style, instead of Google style
fumapy-generate httpx --docstring-style numpyAdd the styles of its components:
@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
@import 'fumadocs-python/preset.css';Content Source
Create a source from the JSON file, and pass it to loader():
import { loader } from 'fumadocs-core/source';
import { createPython } from 'fumadocs-python';
const python = createPython({
file: './httpx.json',
// `httpx._client.Client` is served at `/docs/httpx/_client/Client`,
// `none` serves it at `/docs/_client/Client`
groupBy: 'module',
});
export const source = loader(await python.staticSource(), {
baseUrl: '/docs',
// adds a `module` or `class` badge to pages in the sidebar
plugins: [python.loaderPlugin()],
});Compile and render a page:
import { source } from '@/lib/source';
import { notFound } from 'next/navigation';
import { DocsBody, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
export default async function Page({ params }: PageProps<'/docs/[[...slug]]'>) {
const page = source.getPage((await params).slug);
if (!page) notFound();
const data = await page.data.load();
// pass MDX components to override the default ones
const { body, toc } = await data.render();
return (
<DocsPage toc={toc}>
<DocsTitle>{page.data.title}</DocsTitle>
<DocsBody>{body}</DocsBody>
</DocsPage>
);
}Compiler Options
Docstrings are Markdown, so remark and rehype plugins apply to them:
import { createPython } from 'fumadocs-python';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
const python = createPython({
file: './httpx.json',
// Shiki options for code blocks, like source code and docstring examples
rehypeCodeOptions: {
themes: {
light: 'github-light',
dark: 'github-dark',
},
},
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
});Dynamic Source
staticSource() reads the JSON file once. To read a regenerated file without restarting the server, use dynamicSource() with dynamicLoader():
import { dynamicLoader } from 'fumadocs-core/source';
const pythonLoader = dynamicLoader(python.dynamicSource(), {
baseUrl: '/docs',
plugins: [python.loaderPlugin()],
});
export function getSource() {
return pythonLoader.get();
}Use await getSource() in place of source. After you call pythonLoader.invalidate(), the next getSource() reads the file again.
MDX Files
To keep the pages next to your other content, convert the JSON into MDX files with a script. Links between pages start with baseUrl, so set it to the URL of the output directory:
import * as fs from 'node:fs/promises';
import * as Python from 'fumadocs-python';
const content = JSON.parse(await fs.readFile('./httpx.json', 'utf-8'));
// remove pages of deleted modules
await fs.rm('content/docs/httpx', { recursive: true, force: true });
await Python.write(Python.convert(content, { baseUrl: '/docs' }), 'content/docs');How is this guide?
Last updated on
