Fumadocs rebuilt on Hamur, the original is fumadocs.dev
Skip to Content
Fumadocs

Python

Generate API references from Python packages.

Python support is experimental.

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 shiki

Generate 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 numpy

Add the styles of its components:

Tailwind CSS
@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():

lib/source.ts
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:

src/pages/docs/[...slugs].tsx
import type { PageProps } from 'waku/router';
import { unstable_notFound } from 'waku/router/server';
import { DocsBody, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
import { source } from '@/lib/source';

export default async function Page({ slugs }: PageProps<'/docs/[...slugs]'>) {
  const page = source.getPage(slugs);
  if (!page) unstable_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>
  );
}

export async function getConfig() {
  return {
    render: 'static',
    staticPaths: source.generateParams().map((item) => item.slug),
  } as const;
}

Compiler Options

Docstrings are Markdown, so remark and rehype plugins apply to them:

lib/source.ts
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():

lib/source.ts
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