setup-python-tools について
Pyodide によるブラウザー内 Python 実行を Atlas Chat アプリに追加するためのセットアップスキルです。開発者向けに、導入、ランタイム接続、読み込み表示、初期化中の入力制御を案内します。
主な機能
- 01Pyodide によるブラウザー実行
- 02usePyodideRuntime の設定手順
- 03Atlas Chat ランタイム接続
- 04初期化の進捗とエラー表示
- 05読み込み中の入力無効化
活用シーン
- 01Web チャットで Agent 設定済み Python ツールを有効化
- 02チャット入力欄の上に読み込み状況を表示
- 03Python 初期化前のメッセージ送信を防止
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ cognitedata/ builder-skills --skill setup-python-toolsインストール後、Claude Code で/setup-python-toolsを入力して読み込まれたか確認します。
詳細
Python ツールの実行を設定する
$ARGUMENTS 向けのクライアント側 Pyodide 実行を追加します。アプリが Atlas / EOS サイドバーを使用している場合はスキップしてください(integrate-fusion-agent)。
前提条件: integrate-atlas-chat の src/atlas-agent/ と、@sinclair/typebox が必要です。integrate-atlas-chat/code/ から python.ts、pyodide.ts、pyodide-react.ts、pyodide-runtime.ts を src/atlas-agent/ にコピーします。
CDF の runPythonCode ツールは toolConfirmation + clientTool として届きます。usePyodideRuntime を接続し、pythonRuntime を useAtlasChat に渡します。PythonToolConfig の項目は不要です。Pyodide のサイズは約 30MB で、初回読み込み後はキャッシュされます。
ステップ 1 — アプリを理解する
変更を加える前に、次のファイルを確認してください。
package.json— パッケージマネージャーと既存の依存関係を確認しますuseAtlasChatを呼び出すコンポーネント — 現在のツールと設定を確認します
ステップ 2 — Pyodide をインストールする
アプリのパッケージマネージャーを使用して、正確に [email protected] をインストールします。
このバージョンは実行時に読み込まれる CDN アーティファクトと一致している必要があります。異なるバージョンをインストールするとエラーが発生します。
- pnpm →
pnpm add [email protected] - npm →
npm install [email protected] - yarn →
yarn add [email protected]
@sinclair/typeboxはintegrate-atlas-chatで既にインストールされているはずです。ない場合は追加してください。
ステップ 3 — usePyodideRuntime を設定する
useAtlasChat を呼び出すコンポーネントに、Pyodide ランタイムフックを追加します。
import { loadPyodide } from "pyodide";
import { usePyodideRuntime } from "./atlas-agent/pyodide-react";
import { useAtlasChat } from "./atlas-agent/react";
function MyChat() {
const { sdk, isLoading } = useDune();
// Initialize Python runtime (loads Pyodide, installs packages, sets up Cognite SDK)
const {
runtime: pythonRuntime,
loading: pythonLoading,
progress: pythonProgress,
error: pythonError,
isReady: pythonReady,
} = usePyodideRuntime({
loadPyodide,
client: isLoading ? null : sdk,
requirements: ["pandas", "numpy"], // optional — additional packages
});
// ... useAtlasChat below
}
フック API リファレンス
| 戻り値フィールド | 型 | 説明 |
|---|---|---|
runtime | PythonRuntime | undefined | 初期化済みランタイム。準備ができていない場合は undefined です |
loading | boolean | Pyodide の読み込み中または初期化中は True です |
error | string | null | 初期化に失敗した場合のエラーメッセージです |
progress | { stage: string; percent: number } | UI に表示する現在の初期化進捗です |
isReady | boolean | 便利な判定値:!loading && !error && runtime !== undefined |
読み込み状態の UI
読み込みインジケーターはメッセージリストではなく、チャット入力欄の上に配置します。 コンパクトにまとめ、ステージのテキストとパーセントを表示するピル/バッジにします。エラーバッジは別に表示します。 初回の読み込みには約 30〜60 秒かかります(約 30MB をダウンロードします)。2 回目以降はブラウザーキャッシュから読み込まれるため、2 秒未満です。
{/* Loading — shown above the input while Pyodide initializes */}
{pythonLoading && (
<div className="flex items-center gap-2 rounded-lg border bg-muted/50 px-3 py-2 text-sm text-muted-foreground">
{/* Optional: <IconBrandPython /> from @tabler/icons-react */}
<span>{pythonProgress.stage || "Initializing Python..."}</span>
{pythonProgress.percent > 0 && pythonProgress.percent < 100 && (
<span className="text-xs opacity-70">({pythonProgress.percent}%)</span>
)}
</div>
)}
{/* Error — shown if init fails (after loading finishes) */}
{pythonError && !pythonLoading && (
<div className="flex items-center gap-2 rounded-lg border border-destructive/30 bg-destructive/10 px-3 py-2 text-sm text-destructive">
<span>Python runtime failed to load</span>
</div>
)}
ステップ 4 — useAtlasChat に接続する
ランタイムを useAtlasChat に渡します。これだけで完了です。ツール設定は不要です。
const { messages, send, isStreaming, progress, error, reset, abort } = useAtlasChat({
client: isLoading ? null : sdk,
agentExternalId: "my-agent",
tools: [renderTimeSeries], // regular client tools (declared to agent), if any
pythonRuntime, // from usePyodideRuntime — enables Python tool execution
});
注: Python ツールは tools を介してエージェントに宣言しません。エージェントは CDF 設定から既にそのツールを認識しています。ライブラリは必要に応じてコードを自動的に取得します。
ステップ 5 — Python の読み込み中は入力を無効にする
ランタイムの準備ができる前に、ユーザーがメッセージを送信できないようにします。状態が明確になるよう、送信ボタンだけでなく、入力エリア全体を無効にしてください。
<ChatInput
onSend={handleSend}
disabled={isStreaming || pythonLoading}
// ...
/>
候補チップを表示するホームページがある場合は、それらも無効にしてください。
<ChatHomePage
onSuggestionClick={handleSuggestionClick}
disabled={pythonLoading}
/>
完了
これで、アプリは Pyodide を介してクライアント側で Python ツールを実行できます。エージェントが Python ツールを呼び出すと、ライブラリがエージェント設定からコードを自動的に取得し、ブラウザー内で実行して、その結果をエージェントに返します。