Execute Python with the Code block
Learn how to use Python code in a workflow.
Use Code blocks to write custom Python code that can transform data and perform complex logic. You can also use popular Python libraries to further extend the functionality of workflows.
To use Python code blocks on self-hosted deployments, you must have the code-executor container configured.
Add a Python Code block
To use Python in a workflow, add a Code block to the canvas and select Python.
Write and execute Python code
The Python code editor has much of the same features as the JavaScript editor, such as autocomplete and syntax highlighting. For example, you can transform an array of records in a similar manner to map()
with JavaScript:
data = query1.data
return [{
"fullName": customer['name'],
"emailAddress": customer['email']
} for customer in data]
Python limitations
JavaScript is the primary method for manipulating and transforming data in Retool. To maintain interoperability with other blocks, Python does not support:
- Function block calls or triggering queries.
- Usage within Loop and Filter blocks.
- Data output in types other than serialized JSON.
- User-imported Python libraries.
Available libraries
Python queries include built-in support for many popular libraries. This enables you to extend the functionality of workflows beyond data transformation. Available libraries include:
Library | Description |
---|---|
numpy | Working with arrays, matrices, and numerical operations. |
bson | Encoding and decoding Binary JSON (BSON) format. |
pandas | Data manipulation and analysis, using data structure called DataFrame. |
scipy | Scientific computing, including optimization, stats, and signal processing. |
python-pdf | Generating and manipulating PDF files. |
charset-normalizer | Character encoding detection and normalization. |
python-dateutil | Parsing, formatting, and manipulating dates and times. |
cryptography | Cryptographic recipes for encryption, hashing, and digital signatures. |
pyyaml | Parsing and generating YAML (Yet Another Markup Language) files. |
pytz | Converting and working with time zones. |
rsa | Working with RSA encryption and decryption. |
protobuf | Serializing and deserializing data using Google's Protocol Buffers (protobuf) format. |
pillow | Loading, processing, and saving images in various formats. |
packaging | Version handling and packaging of Python projects. |
jinja2 | Generating complex text files on-the-fly using a template-based syntax. |
markupsafe | Secure handling of untrusted strings for use with Jinja2 templates. |
async-timeout | Setting timeouts on asyncio tasks. |
beautifulsoup4 | Parsing HTML and XML, and screen-scraping web pages for data. |
openai | Interacting with OpenAI's API, providing access to AI models like GPT-3 for text completion. |
requests | Making HTTP requests to APIs and web services. |
Asyncio | Creating asynchronous programs with coroutines and asynchronous I/O. |
matplotlib | Creating 2D and 3D plots, charts, and visualizations. |
seaborn | A data visualization library based on Matplotlib that provides statistical graphics. |
tabulate | Generating pretty table output from lists, tuples, or dictionaries. |
json | encoding and decoding JSON (JavaScript Object Notation) files. |
datetime | Working with dates and times. |
Configure block settings
You can configure the following settings for Code blocks.
Timeout after (ms)
The duration to wait before the block times out. Default is 10000
.
Retry count
The maximum number of attempts to retry the query if an error is returned.
Interval
The minimum length of time to wait between query retries. This is useful if a query is triggering a rate limit.
Exponential
Whether to increase the interval between retries, also known as exponential backoff. This is useful for handling rate limits.
Finally
Whether to stop the workflow and return an error if the block fails, or continue to allow an error handler to run.
Coefficient
When exponential backoff is enabled, the constant factor by which the time delay between retry attempts increases after each failure.
Max interval
When exponential backoff is enabled, the maximum time to delay between retries.