1. Overview
The Code tool is a powerful, built-in GoInsight utility that allows you to execute Python code snippets directly within your workflows. It provides a flexible way to perform custom data manipulation, logic, or calculations that might not be available through other pre-built tools.
With the GoInsight Code node, you can seamlessly integrate custom Python scripts into your automation processes. This enables you to:
- Execute Python Code: Run arbitrary Python code using the standard library.
- Capture Output: Capture all standard output (from print() statements) and error messages generated during execution.
- Integrate Results: Use the execution results, including the captured output and success status, as inputs for subsequent nodes in your workflow.
2. Prerequisites
As a native GoInsight tool, the Code node does not require any external accounts or pre-configuration. It is available for use in any workflow out of the the box.
3. Credentials
For detailed instructions on how to configure and manage credentials, please refer to our official documentation: Credentials Configuration Guide.
4. Supported Operations
This section provides a summary of the actions available with the Code tool, followed by detailed descriptions of their parameters and outputs.
Summary
The Code node focuses on the core resource of code execution.
| Resource | Operation | Description |
|---|---|---|
| Code Execution | Python Executor | Executes a given Python code string and captures all printed output. |
Operation Details
Below are the detailed specifications for each supported operation.
Python Executor
A tool that executes a given Python code string, capturing all printed output during execution. It only supports standard library modules (no third-party package imports).
Input Parameters:
- Code: The Python code string to be executed.
Output:
- Success (bool): Indicates whether the code executed successfully.
- Output (string): The captured printed output or the error message if execution failed.
5. Example Usage
This section will guide you through creating a simple workflow that executes a Python script to print a greeting and captures its output.
The workflow will consist of three nodes: Start -> Python Executor -> Answer.
- Add the Code Node
- In the workflow canvas, click the "+" button to add a new node.
- Select the "Tools" tab in the pop-up panel.
- Find and select "Code" from the list of tools.
- From the list of operations for "Code", click on "Python Executor" to add the node to your canvas.
- Configure the Node
- Click on the newly added "Python Executor" node to open its configuration panel on the right.
- Credentials: This tool is built-in and does not require any credentials to be configured.
- Parameters: Fill in the required input parameters.
- Code: Enter the Python script you want to execute. For this example, let's use a simple print statement: print("Hello from GoInsight!")
- Run and Validate
- Once the Code parameter is filled, the error indicator on the top-right of the canvas should disappear.
- Click the "Run" button on the top-right of the canvas to execute the workflow.
- After a successful run, you can click the log icon (top right) to view the detailed input and output of the node. You will see that the Output field contains the string "Hello from GoInsight!".
After completing these steps, your workflow is fully configured. When executed, it will run the Python script and make its output available for use in subsequent steps, such as displaying it with an Answer node.
6. FAQs
Q: Why is my import requests or import pandas script failing?
A: The Python Executor has a security and performance constraint: it only supports modules from the Python standard library. Third-party packages like requests, pandas, numpy, etc., cannot be imported and will result in an error.
Q: How can I use data from a previous node in my Python code?
A: You can use GoInsight's variable interpolation syntax to insert data from previous nodes directly into your code string. For example, if a previous node named Get_User has an output name, you can use it like this:
user_name = "{{Get_User.output.name}}"
print(f"Hello, {user_name}!")
Q: What happens if my code has a syntax error or runtime exception?
A: If the code fails to execute for any reason, the workflow will not halt. Instead, the Python Executor node will handle the error gracefully:
- The Success output field will be set to false.
- The Output field will contain the detailed error message and traceback from the Python interpreter, which can help you debug the issue.
7. Official Documentation
For more information on the capabilities and limitations of the built-in Code tool, please refer to the official GoInsight documentation: GoInsight Documentation Hub.
Leave a Reply.