# Fumadocs (Framework Mode, Next.js): Python
URL: /docs/next/integrations/content/python
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/content/python.mdx

Generate API references from Python packages.

<Callout type="warn">
Python support is experimental.
</Callout>

`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 [#setup]

```package-install
fumadocs-python shiki
```

Generate the JSON file with the Python command:

```bash
# 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:

```css title="Tailwind CSS"
@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
/* [!code ++] */
@import 'fumadocs-python/preset.css';
```

## Content Source [#content-source]

Create a source from the JSON file, and pass it to `loader()`:

```ts title="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:

```tsx title="app/docs/[[...slug]]/page.tsx"
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 [#compiler-options]

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

```ts title="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 [#dynamic-source]

`staticSource()` reads the JSON file once. To read a regenerated file without restarting the server, use `dynamicSource()` with `dynamicLoader()`:

```ts title="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 [#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:

```js tab="scripts/generate-docs.mjs"
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');
```

```tsx tab="components/mdx.tsx"
import defaultMdxComponents from 'fumadocs-ui/mdx';
import type { MDXComponents } from 'mdx/types';
import * as Python from 'fumadocs-python/components';

const { createPythonComponents, ...PythonComponents } = Python;

export function getMDXComponents(components?: MDXComponents): MDXComponents {
  return {
    ...defaultMdxComponents,
    ...PythonComponents,
    ...components,
  };
}
```