Files
dify-docs/ja/use-dify/nodes/code.mdx
Chenhe Gu 767397a9de changed language codes: cn -> zh, jp -> ja. with other supporting structural changes (#565)
* fix redirect language code prefixes

* rename: cn -> zh, jp -> ja

* remove hardcoded ja / zh references

* remove hardcoded 'english' references

* renamed variable names and dict keys to language agnostic names

* fix: add missing language helper methods to PRAnalyzer

- Add get_language_directory() method
- Initialize source_language and target_languages from config
- Fixes AttributeError when generating mixed PR errors

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* test: kitchen sink workflow validation v2

This PR validates the translation workflow after config-driven refactoring:

Changes:
- Add new test file: test-workflow-validation.mdx
- Modify existing file: introduction.mdx
- Update docs.json navigation

Tests:
- New file translation (add workflow)
- Existing file translation (update workflow)
- Navigation sync across languages
- Config-driven language codes (zh/ja instead of cn/jp)
- Source language abstraction (no hardcoded "English")

Expected workflow behavior:
1. Detect changes in en/ directory
2. Translate new file to zh and ja
3. Update modified file translations
4. Sync docs.json for all language sections
5. Commit translated files automatically

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: update workflow paths to use zh/ja instead of cn/jp

Aligns workflow trigger paths with the zh/ja language directory rename.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Revert "fix: update workflow paths to use zh/ja instead of cn/jp"

This reverts commit 9587b7cc5d.

* Revert "test: kitchen sink workflow validation v2"

This reverts commit 4abdd69fd2.

* fix: update workflow paths in doc analyze workflow to use zh/ja instead of cn/jp

* Refactor/language codes (#240)

* test: kitchen sink workflow validation v2

This PR validates the translation workflow after config-driven refactoring:

Changes:
- Add new test file: test-workflow-validation.mdx
- Modify existing file: introduction.mdx
- Update docs.json navigation

Tests:
- New file translation (add workflow)
- Existing file translation (update workflow)
- Navigation sync across languages
- Config-driven language codes (zh/ja instead of cn/jp)
- Source language abstraction (no hardcoded "English")

Expected workflow behavior:
1. Detect changes in en/ directory
2. Translate new file to zh and ja
3. Update modified file translations
4. Sync docs.json for all language sections
5. Commit translated files automatically

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: update workflow paths to use zh/ja instead of cn/jp

Aligns workflow trigger paths with the zh/ja language directory rename.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Revert "fix: update workflow paths to use zh/ja instead of cn/jp"

This reverts commit 9587b7cc5d.

* Revert "test: kitchen sink workflow validation v2"

This reverts commit 4abdd69fd2.

* fix: update workflow paths in doc analyze workflow to use zh/ja instead of cn/jp

---------

Co-authored-by: Claude <noreply@anthropic.com>

* fix: update workflow files to use 'source' instead of 'english'

After refactoring the PR analyzer to use 'source' for source language
PRs (instead of hardcoded 'english'), the workflow files need to match.

Changes:
- sync_docs_analyze.yml: pr_type == 'source' (was 'english')
- sync_docs_update.yml: PR_TYPE != 'source' check
- Updated all comments from "English" to "source language"
- Updated all pr_type values in JSON from "english" to "source"

This ensures the workflows trigger correctly with the refactored
config-driven language system.

Related: language code refactoring (cn/jp → zh/ja)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix/workflow source language references (#245)

* test: kitchen sink workflow validation v2

This PR validates the translation workflow after config-driven refactoring:

Changes:
- Add new test file: test-workflow-validation.mdx
- Modify existing file: introduction.mdx
- Update docs.json navigation

Tests:
- New file translation (add workflow)
- Existing file translation (update workflow)
- Navigation sync across languages
- Config-driven language codes (zh/ja instead of cn/jp)
- Source language abstraction (no hardcoded "English")

Expected workflow behavior:
1. Detect changes in en/ directory
2. Translate new file to zh and ja
3. Update modified file translations
4. Sync docs.json for all language sections
5. Commit translated files automatically

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: update workflow paths to use zh/ja instead of cn/jp

Aligns workflow trigger paths with the zh/ja language directory rename.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Revert "fix: update workflow paths to use zh/ja instead of cn/jp"

This reverts commit 9587b7cc5d.

* Revert "test: kitchen sink workflow validation v2"

This reverts commit 4abdd69fd2.

* fix: update workflow paths in doc analyze workflow to use zh/ja instead of cn/jp

* fix: update workflow files to use 'source' instead of 'english'

After refactoring the PR analyzer to use 'source' for source language
PRs (instead of hardcoded 'english'), the workflow files need to match.

Changes:
- sync_docs_analyze.yml: pr_type == 'source' (was 'english')
- sync_docs_update.yml: PR_TYPE != 'source' check
- Updated all comments from "English" to "source language"
- Updated all pr_type values in JSON from "english" to "source"

This ensures the workflows trigger correctly with the refactored
config-driven language system.

Related: language code refactoring (cn/jp → zh/ja)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>

* fix

* fix docs.json language codes

* rename previous version docs: cn -> zh, jp -> ja

* rm duplicate redirect entires

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-28 04:00:02 -08:00

140 lines
6.3 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "コード"
description: "データ処理のためのカスタムPythonまたはJavaScriptの実行"
icon: "code"
---
<Note> ⚠️ このドキュメントはAIによって自動翻訳されています。不正確な部分がある場合は、[英語版](/en/use-dify/nodes/code)を参照してください。</Note>
コードードは、カスタムPythonまたはJavaScriptを実行して、ワークフロー内で複雑なデータ変換、計算、ロジックを処理します。事前設定されたードが特定の処理ニーズに十分でない場合に使用してください。
<Frame caption="コードノード設定インターフェース">
<img src="https://assets-docs.dify.ai/dify-enterprise-mintlify/en/guides/workflow/node/9969aa1bc1912aebe366f5d8f5dde296.png" alt="Code node interface" />
</Frame>
## 設定
**入力変数**を定義してワークフロー内の他のノードからデータにアクセスし、これらの変数をコード内で参照します。関数は、宣言した**出力変数**を含む辞書を返す必要があります。
```python
def main(input_variable: str) -> dict:
# 入力を処理
result = input_variable.upper()
return {
'output_variable': result
}
```
## 言語サポート
ニーズと習熟度に基づいて、**Python**と**JavaScript**から選択できます。どちらの言語も、データ処理用の一般的なライブラリにアクセス可能な安全なサンドボックス内で実行されます。
<Tabs>
<Tab title="Python">
Pythonには`json`、`math`、`datetime`、`re`などの標準ライブラリが含まれています。データ分析、数学的演算、テキスト処理に最適です。
```python
def main(data: list) -> dict:
import json
import math
average = sum(data) / len(data)
return {'result': math.ceil(average)}
```
</Tab>
<Tab title="JavaScript">
JavaScriptは標準の組み込みオブジェクトとメソッドを提供します。JSON操作と文字列演算に適しています。
```javascript
function main(data) {
const processed = data.map(item => item.toUpperCase());
return { result: processed };
}
```
</Tab>
</Tabs>
## エラーハンドリングと再試行
失敗したコード実行に対する自動再試行動作を設定し、コードがエラーに遭遇した場合のフォールバック戦略を定義します。
<Frame caption="エラーハンドリング設定オプション">
<img src="https://assets-docs.dify.ai/2024/12/58f392734ce44b22cd8c160faf28cd14.png" alt="Code Error handling" />
</Frame>
**再試行設定**では、設定可能な間隔最大5000msで最大10回の自動再試行が可能です。一時的な処理問題の対処にこれを有効にしてください。
**エラーハンドリング**では、コード実行が失敗した場合のフォールバックパスを定義でき、コードが問題に遭遇してもワークフローの実行を継続できます。
<Frame caption="再試行設定インターフェース">
<img src="https://assets-docs.dify.ai/2024/12/9fdd5525a91dc925b79b89272893becf.png" alt="Retry settings" />
</Frame>
## 出力検証と制限
コード出力は厳格な制限で自動的に検証されます:
- **文字列**最大長80,000文字、nullバイトは削除されます
- **数値**-999999999から999999999の範囲、浮動小数点数は小数点以下10桁に制限
- **オブジェクト/配列**複雑なネスト構造を防ぐため最大深度5レベル
これらの制限により、パフォーマンスが確保され、ワークフローでのメモリー問題が防止されます。
## セキュリティ上の考慮事項
コアクセス、ネットワークリクエスト、システムコマンドを防ぐ厳格なサンドボックス内で実行されます。これにより、プログラミングの柔軟性を提供しながらセキュリティが維持されます。
セキュリティ上の理由から、一部の操作は自動的にブロックされます。システムファイルへのアクセスや潜在的に危険な操作の実行を試みることは避けてください:
<Frame caption="Cloudflare WAFによるセキュリティフィルタリング">
<img src="https://assets-docs.dify.ai/2024/12/ad4dc065c4c567c150ab7fa7bfd123a3.png" alt="Cloudflare WAF blocking" />
</Frame>
コードが保存されない場合は、ブラウザのネットワークタブを確認してください - セキュリティフィルタが潜在的に危険な操作をブロックしている可能性があります。
## 依存関係サポート
コードードは、PythonとJavaScript両方の外部依存関係をサポートします
```python
# Python: numpy、pandas、requestsなどをインポート
import numpy as np
import pandas as pd
def main(data: list) -> dict:
df = pd.DataFrame(data)
return {'mean': float(np.mean(df['values']))}
```
```javascript
// JavaScript: lodash、momentなどをインポート
const _ = require('lodash');
function main(data) {
return { unique: _.uniq(data) };
}
```
依存関係はサンドボックス環境に事前インストールされています。Difyインストールで利用可能なパッケージリストを確認してください。
## セルフホスト型セットアップ
セルフホスト型Difyインストールの場合、安全なコード実行のためにサンドボックスサービスを開始してください
```bash
docker-compose -f docker-compose.middleware.yaml up -d
```
サンドボックスサービスはDockerが必要で、セキュリティのためにメインシステムからコード実行を分離します。
## 一般的な使用例
**データ解析** - APIや他のードから返される複雑なJSON構造から特定の値を抽出します。
**数学的演算** - 事前設定されたノードでは処理できない計算、統計分析、データ集約を実行します。
**データ変換** - データ形式の変換、複数のデータソースの結合、下流処理のための情報の再構築を行います。
**条件ロジック** - 単純なif-else条件を超える複雑な意思決定ロジックを実装します。