Python3 component

更新时间:
复制 MD 格式

The python3 component supports running Python 3 code.

Actions

Action

Description

Use case

runPythonScript

Runs a Python 3 script

Runs a Python 3 script

Configuration example

This section provides parameter configuration examples for each action of the python3 component. You can import them as a test playbook. The visual flow editor helps you view and test configuration parameters intuitively and understand the component logic and usage. For the import procedure, see Playbook import.

Note

Save the example data as a JSON file first.

Example data

{
    "cells": [
        {
            "position": {
                "x": -550,
                "y": -170
            },
            "size": {
                "width": 36,
                "height": 36
            },
            "attrs": {
                "body": {
                    "fill": "white",
                    "strokeOpacity": 0.95,
                    "stroke": "#63ba4d",
                    "strokeWidth": 2
                },
                "label": {
                    "text": "start",
                    "fontSize": 12,
                    "refX": 0.5,
                    "refY": "100%",
                    "refY2": 4,
                    "textAnchor": "middle",
                    "textVerticalAnchor": "top"
                },
                "path": {
                    "stroke": "#63ba4d"
                }
            },
            "visible": true,
            "shape": "circle",
            "id": "58d87b7d-28d9-4f0e-b135-4adc4f1a70e4",
            "zIndex": 1,
            "data": {
                "nodeType": "startEvent",
                "appType": "basic",
                "nodeName": "start",
                "icon": "icon-circle",
                "description": "Playbook start node. A playbook must have one and only one start node, which requires input data configuration for the playbook."
            },
            "markup": [
                {
                    "tagName": "circle",
                    "selector": "body"
                },
                {
                    "tagName": "text",
                    "selector": "label"
                }
            ],
            "isNode": true
        },
        {
            "shape": "custom-edge",
            "attrs": {
                "line": {
                    "stroke": "#63ba4d",
                    "targetMarker": {
                        "stroke": "#63ba4d"
                    }
                }
            },
            "zIndex": 1,
            "id": "5293c3f9-e1c9-4a49-b0eb-635067dc67e8",
            "data": {
                "nodeType": "sequenceFlow",
                "appType": "basic",
                "isRequired": true,
                "icon": "icon-upper-right-arrow"
            },
            "isNode": false,
            "source": {
                "cell": "58d87b7d-28d9-4f0e-b135-4adc4f1a70e4"
            },
            "target": {
                "cell": "f2f5da63-9789-4967-8ef0-a84f04fd6e08"
            },
            "visible": true,
            "router": {
                "name": "manhattan",
                "args": {
                    "padding": 5,
                    "excludeHiddenNodes": true,
                    "excludeNodes": [
                        "clone_node_id"
                    ]
                }
            },
            "vertices": []
        },
        {
            "position": {
                "x": 180,
                "y": -160
            },
            "size": {
                "width": 36,
                "height": 36
            },
            "attrs": {
                "body": {
                    "fill": "white",
                    "strokeOpacity": 0.95,
                    "stroke": "#63ba4d",
                    "strokeWidth": 2
                },
                "path": {
                    "r": 12,
                    "refX": "50%",
                    "refY": "50%",
                    "fill": "#63ba4d",
                    "strokeOpacity": 0.95,
                    "stroke": "#63ba4d",
                    "strokeWidth": 4
                },
                "label": {
                    "text": "end",
                    "fontSize": 12,
                    "refX": 0.5,
                    "refY": "100%",
                    "refY2": 4,
                    "textAnchor": "middle",
                    "textVerticalAnchor": "top"
                }
            },
            "visible": true,
            "shape": "circle",
            "id": "317dd1be-2d20-460e-977e-1fc936ffb583",
            "zIndex": 1,
            "data": {
                "nodeType": "endEvent",
                "appType": "basic",
                "nodeName": "end",
                "icon": "icon-radio-off-full",
                "description": "end"
            },
            "markup": [
                {
                    "tagName": "circle",
                    "selector": "body"
                },
                {
                    "tagName": "circle",
                    "selector": "path"
                },
                {
                    "tagName": "text",
                    "selector": "label"
                }
            ],
            "isNode": true
        },
        {
            "position": {
                "x": -104,
                "y": -185
            },
            "size": {
                "width": 137,
                "height": 66
            },
            "view": "react-shape-view",
            "attrs": {
                "label": {
                    "text": "python3"
                }
            },
            "shape": "activity",
            "id": "1a98a6d1-bf7d-494b-a109-4767ab6b683c",
            "data": {
                "componentName": "python3",
                "appType": "component",
                "nodeType": "action",
                "icon": "https://sophon-gen-v2.oss-cn-zhangjiakou.aliyuncs.com/componentUpload/1680258193158_python3_logo.png?Expires=1745635126&OSSAccessKeyId=STS.NXFP************&Signature=v0E1aypTCVrWQkqLFygJKP7lohg%3D&security-token=CAIS2AJ1q6Ft5B2yfSjIr5vzG87foZl22KyFUBLSokwYY%2Fh22qDsqTz2IHhMenFpAegcv%2Fw%2BlGFZ6%2F8elrp6SJtIXleCZtF94oxN9h2gb4fb4yUAIjSK0s%2FLI3OaLjKm9u2wCryLYbGwU%2FOpbE%2B%2B5U0X6LDmdDKkckW4OJmS8%2FBOZcgWWQ%2FKBlgvRq0hRG1YpdQdKGHaONu0LxfumRCwNkdzvRdmgm4NgsbWgO%2Fks0OP3AOrlrBN%2Bdiuf8T9NvMBZskvD42Hu8VtbbfE3SJq7BxHybx7lqQs%2B02c5onDWwAJu0%2FXa7uEo4wydVNjFbM9A65Dqufxn%2Fpgt%2Braj4X7xhhEIOVJSSPbSZBbSxJNvU1RXDxQVcEYWxylurjnXvF%2Bs5c2%2B8hAf0rM946UoJvc3YDI5hWbc8mJsTnhSSTAEIv%2By8ptqoFOtH7DkLTHWR7hCtv2306G8%2BSFMytAXxqAASheMaPI1EtW9vmzJTiUzI1Mpy%2FK1KMvL%2B5YrTITRTgQenkGkLFgTOTzW2VSuoF6w%2BpSA8UDTPnYZlyFlgELihq4wDNkqqrtUToxub0PrpMEESyGVIWbCAK0XljETFRtwl7eMM99EaqvKTN8r6%2Bhxw2xWIDH5k25760sl0KS5qaCIAA%3D",
                "ownType": "sys",
                "zIndex": 1,
                "tenantId": "baba",
                "customInput": true,
                "description": "Run Python3 scripts.",
                "id": 0,
                "name": "runPythonScript",
                "operateType": "general",
                "parameters": [
                    {
                        "dataType": "Complex",
                        "defaultValue": "",
                        "description": "Input parameters for the Python script",
                        "name": "inputParams",
                        "needCascader": false,
                        "required": false,
                        "tags": ""
                    },
                    {
                        "dataType": "Text",
                        "defaultValue": "",
                        "description": "Content of the Python script",
                        "name": "pythonScript",
                        "needCascader": false,
                        "required": false,
                        "tags": ""
                    }
                ],
                "riskLevel": 2,
                "nodeName": "python3",
                "actionName": "runPythonScript",
                "actionDisplayName": "runPythonScript",
                "cascaderValue": [
                    {
                        "label": "DataFormat",
                        "value": "${DataFormat}",
                        "children": []
                    }
                ],
                "valueData": {
                    "pythonScript": "aW1wb3J0IGxvZ2dpbmcKZGVmIGV4ZWN1dGUgKHBhcmFtcyk6CiAgaXAgPSBwYXJhbXNbJ2lwJ10KICAjbG9nZ2luZy5pbmZvKCJlbnRlciBleGVjdXRlLGlwIGlzICIraXApCiAgc3VjY2Vzcz1UcnVlCiAgbWVzc2FnZT0nT0snCiAgZGF0YT1beyJpcCI6aXB9XQogIHJldHVybiAoc3VjY2VzcyxtZXNzYWdlLGRhdGEp",
                    "inputParams": "[{\"fieldName\":\"ip\",\"fieldValue\":\"${DataFormat.datalist.*.ip}\",\"sampleData\":\"127.0.0.1\"}]",
                    "keySampleMap": "{\"ip\":\"127.0.0.1\"}"
                },
                "status": "success"
            },
            "zIndex": 1
        },
        {
            "shape": "custom-edge",
            "attrs": {
                "line": {
                    "stroke": "#63ba4d",
                    "targetMarker": {
                        "stroke": "#63ba4d"
                    }
                }
            },
            "zIndex": 1,
            "id": "629151e2-2f65-45ac-8992-6fc05b9b9e8f",
            "data": {
                "nodeType": "sequenceFlow",
                "appType": "basic",
                "isRequired": true,
                "icon": "icon-upper-right-arrow"
            },
            "isNode": false,
            "visible": true,
            "router": {
                "name": "manhattan",
                "args": {
                    "padding": 5,
                    "excludeHiddenNodes": true,
                    "excludeNodes": [
                        "clone_node_id"
                    ]
                }
            },
            "source": {
                "cell": "1a98a6d1-bf7d-494b-a109-4767ab6b683c"
            },
            "target": {
                "cell": "317dd1be-2d20-460e-977e-1fc936ffb583"
            },
            "vertices": []
        },
        {
            "position": {
                "x": -403,
                "y": -185
            },
            "size": {
                "width": 137,
                "height": 66
            },
            "view": "react-shape-view",
            "attrs": {
                "label": {
                    "text": "DataFormat"
                }
            },
            "shape": "activity",
            "id": "f2f5da63-9789-4967-8ef0-a84f04fd6e08",
            "data": {
                "componentName": "DataFormat",
                "appType": "component",
                "nodeType": "action",
                "icon": "https://sophon-gen-v2.oss-cn-zhangjiakou.aliyuncs.com/componentUpload/1719222281702_DataFormat_logo.png?Expires=1745635126&OSSAccessKeyId=STS.NXFP************&Signature=BrZznwYmLW%2BogLrdq1LEnGusLiA%3D&security-token=CAIS2AJ1q6Ft5B2yfSjIr5vzG87foZl22KyFUBLSokwYY%2Fh22qDsqTz2IHhMenFpAegcv%2Fw%2BlGFZ6%2F8elrp6SJtIXleCZtF94oxN9h2gb4fb4yUAIjSK0s%2FLI3OaLjKm9u2wCryLYbGwU%2FOpbE%2B%2B5U0X6LDmdDKkckW4OJmS8%2FBOZcgWWQ%2FKBlgvRq0hRG1YpdQdKGHaONu0LxfumRCwNkdzvRdmgm4NgsbWgO%2Fks0OP3AOrlrBN%2Bdiuf8T9NvMBZskvD42Hu8VtbbfE3SJq7BxHybx7lqQs%2B02c5onDWwAJu0%2FXa7uEo4wydVNjFbM9A65Dqufxn%2Fpgt%2Braj4X7xhhEIOVJSSPbSZBbSxJNvU1RXDxQVcEYWxylurjnXvF%2Bs5c2%2B8hAf0rM946UoJvc3YDI5hWbc8mJsTnhSSTAEIv%2By8ptqoFOtH7DkLTHWR7hCtv2306G8%2BSFMytAXxqAASheMaPI1EtW9vmzJTiUzI1Mpy%2FK1KMvL%2B5YrTITRTgQenkGkLFgTOTzW2VSuoF6w%2BpSA8UDTPnYZlyFlgELihq4wDNkqqrtUToxub0PrpMEESyGVIWbCAK0XljETFRtwl7eMM99EaqvKTN8r6%2Bhxw2xWIDH5k25760sl0KS5qaCIAA%3D",
                "ownType": "sys",
                "zIndex": 1,
                "tenantId": "baba",
                "customInput": false,
                "description": "Generate new data",
                "id": 0,
                "name": "formatdata",
                "operateType": "general",
                "output": [],
                "parameters": [
                    {
                        "dataType": "Complex",
                        "defaultValue": "",
                        "description": "Data to be transformed and generated",
                        "enDescription": "",
                        "name": "outputFields",
                        "needCascader": false,
                        "required": false,
                        "tags": ""
                    }
                ],
                "riskLevel": 2,
                "nodeName": "DataFormat",
                "actionName": "formatdata",
                "actionDisplayName": "formatdata",
                "cascaderValue": [],
                "valueData": {
                    "outputFields": "[{\"fieldName\":\"ip\",\"fieldValue\":\"127.0.0.1\"}]"
                },
                "status": "success"
            },
            "zIndex": 1
        },
        {
            "shape": "custom-edge",
            "attrs": {
                "line": {
                    "stroke": "#63ba4d",
                    "targetMarker": {
                        "stroke": "#63ba4d"
                    }
                }
            },
            "zIndex": 1,
            "id": "097a897e-5525-4227-9050-2d3bb3fa41e8",
            "data": {
                "nodeType": "sequenceFlow",
                "appType": "basic",
                "isRequired": true,
                "icon": "icon-upper-right-arrow"
            },
            "isNode": false,
            "visible": true,
            "router": {
                "name": "manhattan",
                "args": {
                    "padding": 5,
                    "excludeHiddenNodes": true,
                    "excludeNodes": [
                        "clone_node_id"
                    ]
                }
            },
            "source": {
                "cell": "f2f5da63-9789-4967-8ef0-a84f04fd6e08"
            },
            "target": {
                "cell": "1a98a6d1-bf7d-494b-a109-4767ab6b683c"
            },
            "vertices": []
        }
    ]
}

runPythonScript

Note

Python 3 code runs in a built-in Python 3.9 container that belongs to your Alibaba Cloud account. The container provides 0.25 vCPU and 256 MB of memory. The maximum execution time for scripts is 600 seconds.

Configuration

The component parameter configuration has two parts:

  • Parameter area: Configure parameter names and parameter values.

    • Parameter name: Custom names are supported. In your Python code, use execute["parameter_name"] to retrieve parameter values.

    • Parameter value: Supports constants, strings, JSON, arrays, and output values from preceding nodes.

    • Test data: The data values used when testing your Python code.

  • Code area: Write Python code online. The execute function parameter name is customizable and receives the parameters configured in the parameter area.

  • Execution result area:

    • Execution result: Click Run to view the Python code output or error messages in this area.

    • Execution log: View log output to debug Python script errors, such as checking execution position and computation output.

      Note
      • Only warn, info, and error log levels are supported.

      • Log output has a delay. Click Refresh to get the latest entries.

Example

Example:

  • The input parameter name is ip, and the parameter value is the ip field output from the preceding node (named "DataFormat").

  • Uses "params" as the execute function parameter to receive the ip parameter.

  • Uses logging.info to output logs to the execution log.

The parameter value expression is ${DataFormat.datalist.*.ip}, and the test data is 127.0.0.1. The function return structure is {"success": True, "message": "OK", "data": [{"ip": ip}]}. The execution log outputs enter execute,ip is 127.0.0.1, indicating that the script ran successfully.