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

Python パッケージから API リファレンスを生成します。

<Callout type="warn">
Python のサポートは実験的な機能です。
</Callout>

`fumadocs-python` は、Python パッケージのソースコードと docstring からドキュメントを生成し、モジュールとクラスごとにページを作成します。Python コマンドでパッケージの情報を JSON ファイルに出力し、それをコンテンツソースとして読み込むか、MDX ファイルに変換して利用します。

## セットアップ [#setup]

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

Python コマンドで JSON ファイルを生成します。

```bash
# パッケージの Python 環境にインストールします
pip install ./node_modules/fumadocs-python

# httpx.json を書き出します。`--dir` で出力ディレクトリを変更できます
fumapy-generate httpx

# Google スタイルではなく、NumPy または Sphinx スタイルで docstring のセクションを解析します
fumapy-generate httpx --docstring-style numpy
```

コンポーネントのスタイルを追加します：

```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]

JSON ファイルからソースを作成し、`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` は `/docs/httpx/_client/Client` で配信され、
  // `none` にすると `/docs/_client/Client` で配信されます
  groupBy: 'module',
});

export const source = loader(await python.staticSource(), {
  baseUrl: '/docs',
  // サイドバーのページに `module` または `class` のバッジを付けます
  plugins: [python.loaderPlugin()],
});
```

ページをコンパイルしてレンダリングします。

```tsx title="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();
  // MDX コンポーネントを渡すと、デフォルトのコンポーネントを上書きできます
  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]

docstring は Markdown なので、remark や rehype のプラグインを適用できます：

```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',
  // ソースコードや docstring の例など、コードブロック用の Shiki オプション
  rehypeCodeOptions: {
    themes: {
      light: 'github-light',
      dark: 'github-dark',
    },
  },
  remarkPlugins: [remarkMath],
  rehypePlugins: [rehypeKatex],
});
```

### 動的ソース [#dynamic-source]

`staticSource()` は JSON ファイルを一度だけ読み込みます。サーバーを再起動せずに、再生成されたファイルを読み込むには、`dynamicSource()` と `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();
}
```

`source` の代わりに `await getSource()` を使用してください。`pythonLoader.invalidate()` を呼び出した後は、次の `getSource()` の呼び出し時にファイルが再度読み込まれます。

## MDX ファイル [#mdx-files]

ページを他のコンテンツと同じ場所で管理するには、スクリプトで JSON を MDX ファイルに変換します。ページ間のリンクは `baseUrl` から始まるため、`baseUrl` には出力ディレクトリの URL を設定してください。

```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'));
// 削除済みモジュールのページを取り除く
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,
  };
}
```