Zum Inhalt springen
Agentic Workflowdocs
v0.8.2Kostenlos installieren

knoten / builtin / core

Code

Run custom python code

Kern ErweiterungWeb-App

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Run your own Python code on the incoming data when no other node does what you need.

Code runs the Python in Code to Execute and passes on the value of its last line. The language is Python only (Python 3 with its standard library, running in the browser). There is no JavaScript mode.

  • The incoming data is available as a variable named input.
  • The value of the final expression becomes the output, under result.
  • Anything you print() appears in the run’s activity log, which helps with debugging.
  • Each run starts from a clean slate in an isolated sandbox: no network, no access to the page or your stored data, and a 30-second time limit. The first run after a pause takes a couple of seconds while Python starts.
  • A calculation or text transformation that would take many nodes otherwise.
  • Reshape a complex structure: regroup a list, merge fields, count values.
  • Parse an unusual format with Python’s standard library (re, json, csv, datetime, statistics).

For simple field changes, Edit Fields or Math need no code. To call an API, use Http Request.

EinstellungTypWas es bewirkt
Code to Execute (erforderlich)CodeThe code will be executed in the context of the node.

input is a normal Python value (a dict, a list or a single value), the same data that {{ $input }} holds in other nodes. Its shape depends on how many nodes are connected:

  • One node connected. input is that node’s output as it produced it:

    # input == {"selectionText": "Hello", "triggerType": "contextmenu"}
    input["selectionText"] # -> "Hello"
  • Several nodes connected. input is a dict keyed by each source node’s label, so you can tell them apart:

    # input == {
    # "Get Selected Text": {"selection": "Hello"},
    # "Http Request": {"data": {...}},
    # }
    input["Get Selected Text"]["selection"] # -> "Hello"

    A node with a blank label is keyed by its id. If two nodes share a label, both keys get a short suffix, such as "Http Request [a1b2]".

  • Nothing connected. input is an empty dict, {}, so input.get("selectionText", "") is always safe.

Put the value you want to pass on as the last line, on its own. An assignment on the last line returns nothing.

words = input.get("selectionText", "").split()
{
"words": len(words),
"longest": max(words, key=len) if words else "",
}
  • Output

Each output item has these fields:

FieldTypWhat it holds
result AnyWhat the code returned; null when it returned nothing
Shape of an output item
{
"result": null
}

Read the returned value downstream with {{ $input.result }}, or a field of it with {{ $input.result.words }}. The value travels as JSON: dicts become objects, sets become lists, integers too large for JSON become text, and values with no JSON form (such as class instances) are returned as their text form.

None. This node needs no connection and has no slots for other nodes.

Count the words in text you select on a page:

  1. Context Menu trigger, Menu Contexts Selection.
  2. Code with the snippet from Returning a value above.
  3. Browser Notification, Title Word count, Message {{ $input.result.words }} words, longest: {{ $input.result.longest }}.
  • “Network access is disabled in the Code node sandbox. Use an HTTP Request node to call APIs.” The code tried to fetch something. Fetch it with Http Request before the Code node and read the response from input.
  • “Code execution timed out after 30000ms”. The code ran longer than 30 seconds (start-up included), often an endless loop. Simplify it or split the work across items.
  • result is null. The last line is an assignment, a print() or None. End with the value itself, for example out on its own line.
  • ModuleNotFoundError. Only the Python standard library is available; packages such as numpy or requests cannot be installed.
  • KeyError on input[...]. The field is not there, or several nodes are connected and input is keyed by node label. Use print(input) to see its shape in the activity log.
  • Edit Fields: set or rename fields without code.
  • Http Request: call APIs, which the Code node cannot do.
  • JSON: reshape JSON without code.
  • Debug: inspect the data between steps.
Seite bearbeiten

Zuletzt aktualisiert:

Was this page useful?
Aria zu Code fragen