Skip to main content

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.

info

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]

Using Python in a workflow

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:

LibraryDescription
numpyWorking with arrays, matrices, and numerical operations.
bsonEncoding and decoding Binary JSON (BSON) format.
pandasData manipulation and analysis, using data structure called DataFrame.
scipyScientific computing, including optimization, stats, and signal processing.
python-pdfGenerating and manipulating PDF files.
charset-normalizerCharacter encoding detection and normalization.
python-dateutilParsing, formatting, and manipulating dates and times.
cryptographyCryptographic recipes for encryption, hashing, and digital signatures.
pyyamlParsing and generating YAML (Yet Another Markup Language) files.
pytzConverting and working with time zones.
rsaWorking with RSA encryption and decryption.
protobufSerializing and deserializing data using Google's Protocol Buffers (protobuf) format.
pillowLoading, processing, and saving images in various formats.
packagingVersion handling and packaging of Python projects.
jinja2Generating complex text files on-the-fly using a template-based syntax.
markupsafeSecure handling of untrusted strings for use with Jinja2 templates.
async-timeoutSetting timeouts on asyncio tasks.
beautifulsoup4Parsing HTML and XML, and screen-scraping web pages for data.
openaiInteracting with OpenAI's API, providing access to AI models like GPT-3 for text completion.
requestsMaking HTTP requests to APIs and web services.
AsyncioCreating asynchronous programs with coroutines and asynchronous I/O.
matplotlibCreating 2D and 3D plots, charts, and visualizations.
seabornA data visualization library based on Matplotlib that provides statistical graphics.
tabulateGenerating pretty table output from lists, tuples, or dictionaries.
jsonencoding and decoding JSON (JavaScript Object Notation) files.
datetimeWorking 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.