From aa111d970c14957b269060201c2c1197e6d50cdd Mon Sep 17 00:00:00 2001 From: Deborah Barnard Date: Wed, 18 Oct 2023 15:22:23 +0100 Subject: [PATCH] wip --- docs/code/cookbook/builtin/all.md | 59 ++++++++++----- docs/code/cookbook/builtin/execution.md | 56 ++++++++++----- .../builtin/get-workflow-static-data.md | 72 +++++++++++++------ docs/code/cookbook/builtin/vars.md | 22 +++--- docs/code/cookbook/code-node/console-log.md | 35 ++++++++- mkdocs.yml | 2 +- 6 files changed, 179 insertions(+), 67 deletions(-) diff --git a/docs/code/cookbook/builtin/all.md b/docs/code/cookbook/builtin/all.md index df34947d2..be79a48eb 100644 --- a/docs/code/cookbook/builtin/all.md +++ b/docs/code/cookbook/builtin/all.md @@ -2,33 +2,58 @@ contentType: reference --- -# `$("").all(branchIndex?: number, runIndex?: number)` +# `("").all(branchIndex?: number, runIndex?: number)` This gives access to all the items of the current or parent nodes. If you don't supply any parameters, it returns all the items of the current node. ## Getting items -```typescript -// Returns all the items of the given node and current run -const allItems = $("").all(); +=== "JavaScript" + ```js + // Returns all the items of the given node and current run + let allItems = $("").all(); -// Returns all items the node "IF" outputs (index: 0 which is Output "true" of its most recent run) -const allItems = $("IF").all(); + // Returns all items the node "IF" outputs (index: 0 which is Output "true" of its most recent run) + let allItems = $("IF").all(); -// Returns all items the node "IF" outputs (index: 0 which is Output "true" of the same run as current node) -const allItems = $("IF").all(0, $runIndex); + // Returns all items the node "IF" outputs (index: 0 which is Output "true" of the same run as current node) + let allItems = $("IF").all(0, $runIndex); -// Returns all items the node "IF" outputs (index: 1 which is Output "false" of run 0 which is the first run) -const allItems = $("IF").all(1, 0); -``` + // Returns all items the node "IF" outputs (index: 1 which is Output "false" of run 0 which is the first run) + let allItems = $("IF").all(1, 0); + ``` +=== "Python" + ```python + # Returns all the items of the given node and current run + allItems = _("").all(); + + # Returns all items the node "IF" outputs (index: 0 which is Output "true" of its most recent run) + allItems = _("IF").all(); + + # Returns all items the node "IF" outputs (index: 0 which is Output "true" of the same run as current node) + allItems = _("IF").all(0, _runIndex); + + # Returns all items the node "IF" outputs (index: 1 which is Output "false" of run 0 which is the first run) + allItems = _("IF").all(1, 0); + ``` ## Accessing item data Get all items output by a previous node, and log out the data they contain: -```typescript -previousNodeData = $("").all(); -for(let i=0; i").all(); + for(let i=0; i").all(); + for item in previousNodeData: + # item is of type + # You need to convert it to a Dict + itemDict = item.json.to_py() + print(itemDict) + ``` diff --git a/docs/code/cookbook/builtin/execution.md b/docs/code/cookbook/builtin/execution.md index 96a594d2b..c288d4f22 100644 --- a/docs/code/cookbook/builtin/execution.md +++ b/docs/code/cookbook/builtin/execution.md @@ -2,40 +2,58 @@ contentType: reference --- -# `$execution` +# `execution` -## `$execution.id` +## `execution.id` Contains the unique ID of the current workflow execution. -```typescript -const executionId = $execution.id; +=== "JavaScript" + ```js + let executionId = $execution.id; + ``` +=== "Python" + ```python + executionId = _execution.id + ``` -return [{json:{executionId}}]; -``` - -## `$execution.resumeUrl` +## `execution.resumeUrl` The webhook URL to call to resume a [waiting](/integrations/builtin/core-nodes/n8n-nodes-base.wait/) workflow. See the [Wait > On webhook call](/integrations/builtin/core-nodes/n8n-nodes-base.wait/#webhook-call) documentation to learn more. -## `$execution.customData` +## `execution.customData` This is only available in the Code node. -```js -// Set a single piece of custom execution data -$execution.customData.set("key", "value"); +=== "JavaScript" + ```js + // Set a single piece of custom execution data + $execution.customData.set("key", "value"); -// Set the custom execution data object -$execution.customData.setAll({"key1": "value1", "key2": "value2"}) + // Set the custom execution data object + $execution.customData.setAll({"key1": "value1", "key2": "value2"}) -// Access the current state of the object during the execution -const customData = $execution.customData.getAll() + // Access the current state of the object during the execution + var customData = $execution.customData.getAll() -// Access a specific value set during this execution -const customData = $execution.customData.get("key") -``` + // Access a specific value set during this execution + var customData = $execution.customData.get("key") + ``` +=== "Python" + ```python + # Set a single piece of custom execution data + _execution.customData.set("key", "value"); + + # Set the custom execution data object + _execution.customData.setAll({"key1": "value1", "key2": "value2"}) + + # Access the current state of the object during the execution + customData = _execution.customData.getAll() + + # Access a specific value set during this execution + customData = _execution.customData.get("key") + ``` Refer to [Custom executions data](/workflows/executions/custom-executions-data/) for more information. diff --git a/docs/code/cookbook/builtin/get-workflow-static-data.md b/docs/code/cookbook/builtin/get-workflow-static-data.md index 6455e5b01..7c5f5a481 100644 --- a/docs/code/cookbook/builtin/get-workflow-static-data.md +++ b/docs/code/cookbook/builtin/get-workflow-static-data.md @@ -7,7 +7,7 @@ hide: contentType: reference --- -# `$getWorkflowStaticData(type)` +# `getWorkflowStaticData(type)` This gives access to the static workflow data. @@ -26,34 +26,64 @@ same in the whole workflow. Every node in the workflow can access it. The node s Example with global data: -```javascript -// Get the global workflow static data -const workflowStaticData = $getWorkflowStaticData('global'); +=== "JavaScript" + ```javascript + // Get the global workflow static data + const workflowStaticData = $getWorkflowStaticData('global'); -// Access its data -const lastExecution = workflowStaticData.lastExecution; + // Access its data + const lastExecution = workflowStaticData.lastExecution; -// Update its data -workflowStaticData.lastExecution = new Date().getTime(); + // Update its data + workflowStaticData.lastExecution = new Date().getTime(); -// Delete data -delete workflowStaticData.lastExecution; -``` + // Delete data + delete workflowStaticData.lastExecution; + ``` +=== "Python" + ```python + # Get the global workflow static data + workflowStaticData = _getWorkflowStaticData('global') + + # Access its data + lastExecution = workflowStaticData.lastExecution + + # Update its data + workflowStaticData.lastExecution = new Date().getTime() + + # Delete data + delete workflowStaticData.lastExecution + ``` Example with node data: -```js -// Get the static data of the node -const nodeStaticData = $getWorkflowStaticData('node'); +=== "JavaScript" + ```js + // Get the static data of the node + const nodeStaticData = $getWorkflowStaticData('node'); -// Access its data -const lastExecution = nodeStaticData.lastExecution; + // Access its data + const lastExecution = nodeStaticData.lastExecution; -// Update its data -nodeStaticData.lastExecution = new Date().getTime(); + // Update its data + nodeStaticData.lastExecution = new Date().getTime(); -// Delete data -delete nodeStaticData.lastExecution; -``` + // Delete data + delete nodeStaticData.lastExecution; + ``` +=== "Python" + ```python + # Get the static data of the node + nodeStaticData = _getWorkflowStaticData('node') + + # Access its data + lastExecution = nodeStaticData.lastExecution + + # Update its data + nodeStaticData.lastExecution = new Date().getTime() + + # Delete data + delete nodeStaticData.lastExecution + ``` diff --git a/docs/code/cookbook/builtin/vars.md b/docs/code/cookbook/builtin/vars.md index e5168d054..3aa1a6725 100644 --- a/docs/code/cookbook/builtin/vars.md +++ b/docs/code/cookbook/builtin/vars.md @@ -3,18 +3,24 @@ description: Access your environment's custom variables. contentType: reference --- -# `$vars` +# `vars` !!! info "Feature availability" * Available on Self-hosted Enterprise and Pro and Enterprise Cloud plans. * You need access to the n8n instance owner account to create variables. -`$vars` contains all [Variables](/variables/) for the active environment. It's read-only: you can access variables using `$vars`, but must set them using the UI. +`vars` contains all [Variables](/variables/) for the active environment. It's read-only: you can access variables using `vars`, but must set them using the UI. -```js -// Access a variable -$vars. -``` +=== "JavaScript" + ```js + // Access a variable + $vars. + ``` +=== "Python" + ```python + # Access a variable + _vars. + ``` -!!! note "$vars and $env" - `$vars` gives access to user-created variables. It's part of the [Environments](/source-control-environments/) feature. `$env` gives access to the [configuration environment variables](/hosting/environment-variables/environment-variables/) for your n8n instance. +!!! note "vars and env" + `vars` gives access to user-created variables. It's part of the [Environments](/source-control-environments/) feature. `env` gives access to the [configuration environment variables](/hosting/environment-variables/environment-variables/) for your n8n instance. diff --git a/docs/code/cookbook/code-node/console-log.md b/docs/code/cookbook/code-node/console-log.md index 90f72cbec..11f4421e7 100644 --- a/docs/code/cookbook/code-node/console-log.md +++ b/docs/code/cookbook/code-node/console-log.md @@ -3,7 +3,7 @@ description: How to use console.log() or print() contentType: howto --- -# Using console.log or print in the Code node +# Output to the browser console with `console.log()` or `print()` in the Code node You can use `console.log()` or `print()` in the Code node to help when writing and debugging your code. @@ -30,3 +30,36 @@ For example, set your Code node **Language** to **Python**, copy the following c a = "apple" print(a) ``` + +### Handling an output of `[object Object]` + +If the console displays `[object Object]` when you print, check the data type, then convert it as needed. + +To check the data type: + +```python +print(type(myData)) +``` + +#### JsProxy + +If `type()` outputs ``, you need to convert the JsProxy to a native Python object using `to_py()`. This occurs when working with data in the n8n node data structure, such as node inputs and outputs. For example, if you want to print the data from a previous node in the workflow: + +```python +previousNodeData = _("").all(); +for item in previousNodeData: + # item is of type + # You need to convert it to a Dict + itemDict = item.json.to_py() + print(itemDict) +``` + +Refer to the Pyodide documentation on [JsProxy](https://pyodide.org/en/stable/usage/api/python-api/ffi.html#pyodide.ffi.JsProxy){:target=_blank .external-link} for more information on this class. + + + + + + + + diff --git a/mkdocs.yml b/mkdocs.yml index 17947bcf0..2d0e9820e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -641,7 +641,7 @@ nav: - Get number of items returned by last node: code/cookbook/code-node/number-items-last-node.md - Split binary file data into individual items: code/cookbook/code-node/split-binary-file-data.md - Get the binary data buffer: code/cookbook/code-node/get-binary-data-buffer.md - - Using console.log: code/cookbook/code-node/console-log.md + - Output to the browser console: code/cookbook/code-node/console-log.md - API: - api/index.md - Authentication: api/authentication.md