Skip to content
Agentic Workflowdocs
v0.9.0Install free

nodes / builtin / core

Python Code

Run your own Python code on the incoming items, in a sandbox in the browser

Core ExtensionWeb app

Formerly Code. The step was renamed in AWFlow 0.9; searching for the old name still finds it.

Run your own Python code on the incoming items when no other step does what you need. For JavaScript, use JavaScript Code, which starts faster.

Python Code runs your Python (Python 3 with its standard library, in the browser) and passes on the value of its last line. Anything you print() appears in the run’s activity log. The code runs 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 few seconds while Python starts.

At the top of Setup, a green card says what the step can reach: What it touches: nothing outside this step. No internet (use HTTP Request for calls), no web pages, tabs or your files, no connections or stored data, and how long it may run (Stops after …), with Change limits (the step’s Time limit). In Ready to run and on a Marketplace listing, the step shows as a calm grey Runs code in a sandbox row, not a warning.

Code is never searched for {{ }}: f"{{name}}" and {{ $input.x }} reach Python exactly as written (the editor warns Looks like an expression: use items instead).

Python Code version 2 (new steps in AWFlow 0.9)

Section titled “Python Code version 2 (new steps in AWFlow 0.9)”

Pick how it runs with Run:

  • Once for all items (default): one interpreter for the whole list, so 50 items take seconds, not minutes. items is the list of items (dicts), input the first item. The last line must be a list: each element becomes one item.
  • Once for each item: input is the current item and item_index its position. A dict result is the new item; any other value becomes { "result": value }.

Both modes also get workflow and execution dicts (ids, name, run mode).

Once for all items
# items is a list of dicts; the last line is the output
[{**i, "total": i["qty"] * i["price"]} for i in items]
Once for each item
# input is the current item; the last line is the new item
{**input, "total": input["qty"] * input["price"]}

Errors name the line and the item: Line 4, item 3: KeyError: 'email'. The full traceback stays in the activity log. A run that’s too long stops with Stopped after 30 s (time limit).

They run once per item, {{ }} in the code is replaced before it runs, and the value comes out under result. The sections below describe them. Update in the step’s header moves to version 2 and sets Once for each item, so the same code gives the same items.

  • 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. For counts and sums per group, Summarize items needs no code.

SettingTypeWhat it does
Code to run (required)CodeThe code runs in the context of this step.

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

Version 2 returns your items as they are. In steps saved before 0.9, 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. Python Code, Once for each item, ending with {**input, "words": len(input.get("selectionText", "").split())}.
  3. Browser Notification, Title Word count, Message {{ $input.words }} words.
  • “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 this step and read the response from input.
  • “Stopped after 30 s (time limit)”. The code ran longer than 30 seconds (start-up included), often an endless loop. Simplify it or split the work across items.
  • “Once for all items” code must end with a list. End with a list, for example items or a list comprehension.
  • 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 page

Last updated:

Was this page useful?
Ask Aria about Python Code