nodes / builtin / core
Python Code
Run your own Python code on the incoming items, in a sandbox in the browser
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.
What it does
Section titled “What it does”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.
itemsis the list of items (dicts),inputthe first item. The last line must be a list: each element becomes one item. - Once for each item:
inputis the current item anditem_indexits 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).
# items is a list of dicts; the last line is the output[{**i, "total": i["qty"] * i["price"]} for i in items]# 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).
Steps saved before AWFlow 0.9
Section titled “Steps saved before AWFlow 0.9”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.
When to use it
Section titled “When to use it”- 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.
Inputs and settings
Section titled “Inputs and settings”| Setting | Type | What it does |
|---|---|---|
| Code to run (required) | Code | The code runs in the context of this step. |
Reading input (steps saved before 0.9)
Section titled “Reading input (steps saved before 0.9)”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.
inputis that node’s output as it produced it:# input == {"selectionText": "Hello", "triggerType": "contextmenu"}input["selectionText"] # -> "Hello" -
Several nodes connected.
inputis 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.
inputis an empty dict,{}, soinput.get("selectionText", "")is always safe.
Returning a value
Section titled “Returning a value”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 "",}Outputs
Section titled “Outputs”-
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.
Dependencies and credentials
Section titled “Dependencies and credentials”None. This node needs no connection and has no slots for other nodes.
Example workflow
Section titled “Example workflow”Count the words in text you select on a page:
- Context Menu trigger, Menu Contexts
Selection. - Python Code, Once for each item, ending with
{**input, "words": len(input.get("selectionText", "").split())}. - Browser Notification, Title
Word count, Message{{ $input.words }} words.
Common issues
Section titled “Common issues”- “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
itemsor a list comprehension. resultisnull. The last line is an assignment, aprint()orNone. End with the value itself, for exampleouton its own line.ModuleNotFoundError. Only the Python standard library is available; packages such asnumpyorrequestscannot be installed.KeyErroroninput[...]. The field is not there, or several nodes are connected andinputis keyed by node label. Useprint(input)to see its shape in the activity log.
Related nodes
Section titled “Related nodes”- Edit Fields: set or rename fields without code.
- JavaScript Code: the same in JavaScript.
- HTTP Request: call APIs, which code steps cannot do.
- JSON: reshape JSON without code.
- Test helper: make sample data while you build.