Nodes/ComfyUI-ADIC/Python代码执行器
ComfyUI Node

Python代码执行器

A tiny Python sandbox for munging strings in your graph

By jinchanz·Created about a year ago·Updated a day ago· 5
Python代码执行器
    • output
    • logs
    code# 在这里编写Python代码 # 可用变量: # - input1, input2, input3: 输入数据 # - json, re, math, random, datetime, timedelta: 预导入的模块 # # 可用函数:len, str, int, float, list, dict, tuple, set, range # enumerate, zip, map, filter, sorted, max, min, sum, any, all # # 示例:将换行分割的字符串转换为JSON数组 # lines = input1.strip().split('\n') # result = [line.strip() for line in lines if line.strip()] # output = json.dumps(result, ensure_ascii=False) # 请将最终结果赋值给 'output' 变量 output = "请在上方编写代码"
    input1
    input2
    input3
    safe_modetrue

    ComfyUI gives you a visual language, but occasionally you just want split(), a regex, or a JSON transform - without writing a whole custom node. PythonCodeExecutor is the escape hatch: a node that runs your Python snippet against up to three string inputs and returns the result. It's the "yes, you can do that" node for the fiddly data-shaping tasks this pack keeps running into.

    Here's how it works. You write code into the code box. The environment pre-loads input1, input2, input3 plus a whitelist of modules (json, re, math, random, datetime, timedelta) and a set of builtins (len, str, int, list, dict, sorted, map, filter, etc.). Your code runs through exec, and whatever you assign to the output variable comes out the other side - a string stays a string, anything else gets JSON-serialized. print() output lands in the second output, logs, alongside execution notes.

    The default code template is even useful: it shows the canonical "newline-separated string → JSON array" example, which is the same job StringToJsonArray does one-note.

    Inputs and outputs

    • code - required, multiline. The meat.
    • input1 / input2 / input3 - optional string inputs (auto-JSON-ified if you wire something non-string).
    • safe_mode - present, but the tooltip is a confession: security mode is forced on and this parameter is deprecated. Ignore it.
    • Outputs: output (STRING) and logs (STRING).

    The sandbox is a speed bump, not a wall

    The safety is a regex blocklist: import os|sys|subprocess|shutil|pickle|marshal|shelve|dill, eval|exec|compile|open, dunder attribute access (__class__, __base__, ...), getattr, globals, and friends. That stops accidental footguns and casual scripts dead in their tracks - try import os and you get a clean error JSON.

    But be clear-eyed: a regex filter is not a real sandbox. There are ways around string-matching filters (obfuscation, Unicode tricks, reaching restricted builtins through allowed names), so treat this as a convenience for your own trusted workflows, not as a security boundary for untrusted ones. Don't load a shared workflow that runs arbitrary code here and assume it's contained. If you need a real boundary, run untrusted workflows in a Docker sandbox, not this node.

    When you'd reach for it

    Building JSON for the pack's API nodes (the template with json.dumps covers 90% of that), normalizing text, parsing a batch ID out of a log line, deriving a filename. It's the utility node you discover you want after the third time you hand-format JSON. Note it outputs strings only - if you need IMAGE or LATENT transforms, this isn't your tool.

    Installing

    cd ComfyUI/custom_nodes
    git clone https://github.com/jinchanz/ComfyUI-ADIC
    

    Restart ComfyUI, or install "ComfyUI-ADIC" via ComfyUI Manager. Stdlib-only, no pip extras, no models. The README never documents it; the source (module/nodes_api_basic.py) carries the full security notes.

    CategoryMalette

    Inputs (5)

    NameTypeDefaultDescription
    codeSTRING# 在这里编写Python代码 # 可用变量: # - input1, input2, input3: 输入数据 # - json, re, math, random, datetime, timedelta: 预导入的模块 # # 可用函数:len, str, int, float, list, dict, tuple, set, range # enumerate, zip, map, filter, sorted, max, min, sum, any, all # # 示例:将换行分割的字符串转换为JSON数组 # lines = input1.strip().split('\n') # result = [line.strip() for line in lines if line.strip()] # output = json.dumps(result, ensure_ascii=False) # 请将最终结果赋值给 'output' 变量 output = "请在上方编写代码"
    input1optSTRING
    input2optSTRING
    input3optSTRING
    safe_modeoptBOOLEANtrue已强制启用安全模式(防止沙箱逃逸),此参数已废弃

    Outputs (2)

    NameTypeDescription
    outputSTRING
    logsSTRING