Code
Execute code in an event.
Summit has built-in support for running blocks of code containing Python, PHP, Ruby, JavaScript, or TypeScript. To do so, simply click + Add New
on the canvas or the +
in the pipeline view and type your language of choice to find the plugin that suits you.
Here's an example of a Python code block while in pipeline view:
The output of this code block is anything you print. You can print strings (as in the example above), or you can print objects (like a Python dictionary).
Summit will automatically attempt to translate the output of your print statements into JSON. So if you print a Python dictionary or list in a code block, that dictionary or list will be available to you elsewhere in your model by referring to the code event using liquid syntax.
For the above example:
{{ python_stdout }}
Would refer to "Hello, World!\n"
elsewhere in your model code. And if this were a dictionary, you could key into it. Or a list, index into it, etc.
While using the canvas view, PHP code should be wrapped in <?php
?>
inside the code block.
=Code(```php
<?php
echo "Hello, world!";
?>
```)
String on failure
If the printed object can be turned into a valid dictionary, number, list, or string, it will be. These objects may only contain literals (not executable code). If a printed object cannot be converted into a literal representation, it will simply return its string representation.
Libraries
In addition to each language's standard library, the following libraries may be imported.
Language | Libraries | Version |
---|---|---|
Python | Standard, plus requests , numpy , pillow | 3.12.0 |
JavaScript / TypeScript | Standard, plusfetch , JSON | Boa , TypeScript via wsc |
PHP | Standard-only | 8.2.6 |
Ruby | Standard-only | 3.3.0 |
File System Access
The coding environment can read files that are passed in to the code block. In order to write out, however, you must use either an HTTP request or stdout to print the contents back into your Summit model scope.
Environment Variables
You can weave environment variables from your vault directly into your code.
For example:
import requests
url = "https://api.example.com/endpoint"
headers = {
"Authorization": "Bearer {{ MY_SECRET_TOKEN }}",
"Content-Type": "application/json"
}
payload = {
"key": "value"
}
response = requests.post(url, headers=headers, json=payload)
# Return the response to Summit model scope via stdout
if response.status_code == 200:
print(response.json())
else:
# Return an exception via stderr
response.raise_for_status()
Custom Runtimes
Do you need a specific SDK or set of libraries installed to do your magic? Summit has full support for custom runtimes for paying subscribers to our Team plan and above. Send us a chat message and let us know what you need.
Limitations
There are a few limits to be aware of:
Resource | Constraint |
---|---|
Input code size | 1MiB |
Size of stdin | 10MiB |
Size of stdout | 20MiB |
Size of stderr | 10MiB |
Execution duration | 30 seconds |
Memory consumption | 128MiB |
Updated 4 days ago