top of page
Logo of top retool developers and retool agency

Creating Workflows with Python Support in Retool


Creating Workflows with Python Support in Retool

Retool Workflows now support Python code blocks, a feature that allows you to tap into the versatility and power of Python for automation, data ingestion,workflows,  analysis, and visualization within your Retool applications. 


Python, a widely used programming language for data processing, scientific computing, and automation tasks, is a valuable tool in your data workflow arsenal. 


This article will give you a detailed overview of how to use Python for creating workflows in Retool with its in-depth user-friendly guidance.



With Python support, you can:

  • Automate repetitive tasks: Use Python scripts to automate routine tasks, reducing manual effort and increasing efficiency.

  • Ingest and process data: Easily ingest data from various sources, clean, transform, and prepare it for analysis or visualization.

  • Perform complex data analysis: Utilize Python’s rich ecosystem of data analysis libraries to perform sophisticated data analysis directly within your Retool workflows.

  • Visualize data: Create custom data visualizations using Python libraries and integrate them into your Retool applications for enhanced data insights.


Inclusion of Autocomplete, Syntax Highlighting, Linting, and Built-In Support for Popular Libraries like NumPy and Pandas


Creating Workflows with Python Support in Retool

Retool’s Python support can provide a robust and developer-friendly coding experience. 


Key features include:

  • Autocomplete: As you type your Python code, Retool provides intelligent autocomplete suggestions, helping you write code faster and with fewer errors.

  • Syntax Highlighting: Retool’s code editor highlights Python syntax, making your code easier to read and understand.

  • Linting: Integrated linting checks your code for potential errors and coding standards violations, ensuring that your scripts are clean and maintainable.

  • Built-In Support for Popular Libraries: Retool supports widely used Python libraries such as NumPy and Pandas. These libraries are essential for numerical computations and data manipulation, allowing you to perform complex operations and analyses efficiently.

Integrating Python code blocks into Retool apps can significantly enhance their functionality and flexibility. You can put it into action by following the guidance given in the following section.


Enable Python for Data Processing and Analysis in Workflows

To incorporate Python into your Retool workflows, follow these steps:


Open Your Workflow:

  • Navigate to the workflow where you want to add Python functionality.


Add a Code Block:

  • Click on the "+" button to add a new block to your workflow.

  • Select "Code" from the list of available blocks.


Select Python as the Language:

  • In the Code block settings, choose "Python" from the dropdown menu of supported languages. It configures the block to interpret and execute Python code.


Details on Writing and Executing Python Code Within Workflows

Once you have set up your Python Code block, you can start writing and executing Python scripts within your Retool workflows.


Writing Python Code:

  • Access the Code Editor:

  • The Code block will display a code editor where you can write your Python scripts.

  • Utilize Autocomplete and Syntax Highlighting:

  • The editor provides autocomplete suggestions and highlighting syntax, making writing and debugging your code easier.

  • Use Popular Libraries:

  • Take advantage of built-in support for popular Python libraries like NumPy and Pandas for data manipulation and analysis.


Example Script:


Here’s an example of a Python script that reads a CSV file, performs data analysis, and outputs the results:


import pandas as pd


# Example: Reading data from a CSV file

data = pd.read_csv('path/to/your/file.csv')


# Performing basic data analysis

summary = data.describe()


# Output the summary statistics

print(summary)


Executing Python Code:

Run the Code:

  • Click the "Run" button to execute your Python script. The output will be displayed in the console below the editor.

Error Handling:

  • If your code contains errors, they will be highlighted in the editor, and error messages will be displayed in the console. It helps debug and fix issues promptly.


Integrating with Other Workflow Blocks


Passing Data Between Blocks:

  • You can pass data between different blocks in your workflow. For example, you can use a Query block to fetch data from a database and then process this data in a Python Code block.


Using Output in Subsequent Blocks:

  • The output from your Python script can be used as input for subsequent blocks, allowing for seamless integration and complex workflow creation.


Incorporating Python Code blocks into your Retool workflows improves their functionality by enabling advanced data processing, automation, and analysis. 


By choosing Python in a Code block and utilizing the advanced features of the Retool code editor, you can write and run intricate Python scripts within your workflows. For additional information., check out Python support from Retool.


Python Limitations in Workflows


While integrating Python code blocks in Retool can enhance the functionality and flexibility of your applications, there are several limitations to consider:


Limitations of Using Python in Workflows


While Retool's integration of Python code blocks significantly enhances its capabilities, there are some limitations to be aware of:


  • No Support for Function Block Calls:

Python code blocks cannot call or execute functions defined in other function blocks within the workflow. Each Python block operates independently, and function calls must be self-contained within the same block or else you must use functions that need to be defined outside of your code blocks which can then be used as reusable queries 


  • Restricted Usage in Loop and Filter Blocks:

Python code cannot be directly used within Loop or Filter blocks. These blocks have specific structures and functionalities that currently do not support the integration of Python code. As a result, looping or filtering logic that relies on Python must be handled within standard Python code blocks.


 Data Output Limitations and Library Restrictions


  • Data Output Limitations to Serialized JSON:

The output from Python code blocks is limited to serialized JSON. It means complex data structures must be converted to JSON-compatible formats before outputting. Ensure that your data transformations and analysis produce results that can be serialized into JSON for seamless integration with other blocks in the workflow.


  • Non-Support for User-Imported Libraries:

(note: This has changed now. It’s in beta phase You can import libraries by modifying requirements.txt file) 


Retool's Python environment supports popular libraries like NumPy and Pandas. However, it does not help users import additional libraries. This restriction means that any custom or less common libraries needed for specific functionalities cannot be used within Retool workflows. Users must rely on the pre-supported libraries for all their scripting needs.


Utilizing Built-in Python Libraries


Retool’s Python environment includes a comprehensive set of pre-installed libraries, enabling powerful data processing and analysis capabilities within your workflows without you manually finding and adding them. It saves a lot of time and increases work productivity.


Overview of the Pre-installed Libraries Available for Immediate Use


Retool provides a robust Python environment with over 20 pre-installed libraries. These libraries cover various functionalities, from numerical operations and data manipulation to web requests and JSON handling, allowing users to perform a wide range of data processing, analysis, and automation tasks without installing additional packages. 

This built-in support enables developers to leverage powerful tools within their workflows quickly.


 Examples of Libraries and Their Functions

  • NumPy

Function: NumPy is a fundamental package for scientific computing using Python. It supports large, multi-dimensional arrays and matrices and a collection of mathematical functions that operate on them.


  • Pandas

Function: Pandas is a powerful data manipulation and analysis library. It provides data structures like Series and DataFrame, ideal for handling structured data.


  • Requests

Function: Requests is a simple and elegant HTTP library for Python that is built for humans. It allows you to send HTTP requests easily.


  • JSON

Function: The JSON module provides an easy way to encode and decode JSON     data.


  • Matplotlib

Function: Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.


Also, For more information, refer to Retool Python support.


After exploring the powerful capabilities of Retool's pre-installed Python libraries, you may wonder how these integrate with other programming environments. 


In the next section, we'll guide you through the seamless transition between JavaScript and Python in Retool workflows. This allows you to harness the strengths of both languages to enhance application functionality and efficiency.


Transitioning Between JavaScript and Python in Workflows


Transitioning between JavaScript and Python in Retool workflows allows you to leverage the strengths of both languages, creating powerful and efficient applications. Scroll down to learn how to do it.


Guidance on Switching Languages in Code Blocks for Optimal Use of Tools

When working with Retool workflows, you might find situations where you need to use both JavaScript and Python to leverage the strengths of each language. Here's how you can effectively transition between JavaScript and Python in your workflows for optimal use of tools.


Identify the Best Language for Each Task:

Java and Python can be compared based on their general suitability for various development tasks within the Retool environment. Here is a comparison table highlighting which language might be better suited for each task:

Task

Best Language

Data Transformation and Manipulation

Python

Custom Queries and API Requests

Python

Custom Components

JavaScript

Event Handling

Python

State Management

Python

Error Handling and Validation

Python

Automation and Workflows

Python

Batch Processing

JavaScript

Custom Styling

JavaScript

Steps to Transition Between JavaScript and Python:

Transition Between JavaScript and Python

Setting Up the Workflow:

Determine which parts of your workflow are better suited for JavaScript and which are better suited for Python. 


Creating JavaScript Code Blocks:

Use JavaScript code blocks for tasks such as making asynchronous requests (e.g., fetch), and handling user inputs.


Creating Python Code Blocks:

Use Python code blocks for heavy data processing, analysis, or machine learning tasks.


Passing Data Between JavaScript and Python:

Use Retool’s in built functions to pass data between code blocks. This object allows you to transfer data seamlessly from JavaScript to Python and vice versa.


Integrating Results and Visualizing Data:

Use JavaScript to visualize the processed data from Python. For example, you can use JavaScript libraries like D3.js or Chart.js to create dynamic charts and graphs.


Combining Results in Retool UI:

Display the results in Retool’s UI components, such as tables, charts, or custom components, to create an interactive and data-rich application.


Wondering about the practical application of Python in Retool workflows? Then you are in the right place, dive into the following section for the complete details.


Practical Applications of Python in Retool Workflows


Python can be effectively integrated into Retool workflows to enhance data processing, automation, and API interactions. Here are a few of the  practical application cases:


  • Data Transformation and Cleaning

Case Description: Data often needs to be cleaned and transformed before it can be visualized or analyzed within Retool to ensure accuracy and usability.


  • API Integration and Data Fetching

Case Description: Fetching data from external APIs, processing it, and displaying it in Retool.


  • Automation and Workflow Orchestration

Case Description: Automating repetitive tasks and orchestrating complex workflows.


  • Machine Learning and Predictive Analytics

Case Description: Incorporating machine learning models to provide predictive analytics and insights within Retool applications.


Example Workflow: Creating a Weekly Report Using SQL and Pandas for Data Manipulation


Creating a Weekly Report Using SQL and Pandas for Data Manipulation

Setting Up the SQL Query:

First, fetch the required data from your database using an SQL query.


Processing Data with Pandas:

Use a Python code block to process the fetched data and create a weekly report.


Using SMTP for Email Report Distribution:

Send the generated report via email using an SMTP server


Scheduling Workflow Executions:

Use Retool's scheduling feature to automate the execution of this workflow every week.


Setting Up a Scheduler:

  • Navigate to your workflow settings in Retool and set up a scheduler.

  • Define the schedule to run the workflow at your desired frequency, e.g., every Monday at 8 AM.


Collaboration on data management and reporting is quite useful in an organization as it improves work efficiency and productivity. You can explore the complete details in the following section.


Collaboration on Data Management and Reporting


Retool's capabilities for creating shared data insights and reusable workflows foster collaboration across different teams within an organization. 


Collaborating on data management and reporting brings substantial benefits to an organization, including improved accuracy, efficiency, better decision-making, transparency, compliance, and continuous improvement. 


Promoting Cross-Collaboration Between Different Teams Through Shared Data Insights


Effective data management and reporting are crucial for fostering collaboration between different teams within an organization. Retool’s robust platform can enhance cross-team collaboration by providing shared data insights that are accessible and actionable. Here’s how:

  • Centralized Data Access: Retool allows teams to access a centralized data repository, ensuring everyone works with the same information. It eliminates data silos and promotes a unified approach to data analysis.

  • Real-Time Data Sharing: By creating dashboards and reports in Retool, teams can share real-time data insights. It ensures that all teams have up-to-date information, facilitating better decision-making.

  • Customizable Views: Different teams can customize their views and dashboards to focus on the data most relevant to their functions. For example, the sales team can track sales performance while the marketing team monitors campaign effectiveness.

  • Collaborative Development: Retool’s intuitive interface allows non-technical team members to contribute to creating data insights and reports. It democratizes data analysis and encourages collaboration across the organization.


Examples of Workflows Acting as Portable Entities for Quick Handoffs

Retool workflows can be portable and triggered using webhooks, enabling quick handoffs and team collaboration. Here are some practical examples:

Example 1: Sales Data API

  • Workflow Setup:

  • SQL Query: Fetch sales data from the database.

  • Python Processing: Aggregate and analyze the data using Pandas.

  • Output: Provide the webhook url with api key to trigger the workflow and receive the processed data in JSON format as output.


Example 2: Marketing Campaign Performance API

  • Workflow Setup:

  • API Query: Retrieve campaign performance data from a marketing platform.

  • Python Processing: Clean and summarize the data.

  • Output: Provide the webhook url with api key to trigger the workflow and receive the summarized data in JSON format as output


By providing centralized data access, real-time sharing, and customizable views, Retool enhances cross-team collaboration. Start your journey by partnering with Toolpioneers.

Retool's collaborative features are powerful, but its true potential is unlocked when you combine them with automated workflows.


Let's explore how Retool's webhook functionality allows you to take shared data insights and reports to the next level by creating automated actions based on real-time data updates.


Building Webhook-Based Workflows

Webhook-based workflows in Retool allow you to automate processes by receiving POST requests from external services. Here’s how you can set up and configure webhook-based workflows in Retool.


Creating Workflows with Webhook Mode for Receiving POST Requests


Webhook Mode for Receiving POST Requests

Step-by-Step Guide:

Create a New Workflow:

Navigate to the Retool dashboard and create a new workflow.


Select Webhook Mode:

When setting up the workflow, choose the webhook mode. This mode enables the workflow to receive POST requests from external sources.


Define the Webhook Endpoint:

Retool will auto-generate a unique URL endpoint for your workflow. This URL will be used to receive POST requests. You can find this URL in the workflow settings.


Set Up the Workflow:

Add the necessary blocks to your workflow to process the incoming data. This might include code blocks (JavaScript or Python), database queries, and other operations you must perform on the received data.


Configuring JSON Input Shapes for Webhook’s Request Body, Auto-Generating Webhook URL Endpoints


Configuring JSON Input Shapes:

Define the JSON Schema:

Specify the expected shape of the incoming JSON payload for webhook’s request body. It helps validate the data and ensures the workflow processes it correctly.


Set Up JSON Validation:

Use a JSON validation library in your code block to validate the incoming payload against the defined schema.


Auto-Generating URL Endpoints:

Retrieve the Auto-Generated URL:

Retool provides a unique URL endpoint for your webhook in the workflow settings. External services will send their POST requests to this URL.


Use the URL in External Services:

Copy the auto-generated URL and configure it in the external service or application sending the webhook POST requests.


Example: If you are using a service like Stripe, GitHub, or a custom application, enter the Retool-provided URL in the webhook settings of that service.

By leveraging webhook-based workflows in Retool, you can automate and streamline data processing tasks by receiving and handling POST requests from external services.


Data Visualization and Reporting


Python-based libraries like Seaborn are powerful tools for creating detailed and aesthetically pleasing data visualizations. Seaborn is built on Matplotlib and provides a high-level interface for drawing attractive and informative statistical graphics. Here’s how you can employ Seaborn within Retool to generate data visualizations:


Example Workflow to Create a Data Visualization with Seaborn:


Set Up the Python Environment:

Ensure that Seaborn is available in your Retool Python environment. Retool’s built-in support for popular Python libraries typically includes Seaborn.


Fetch Data Using SQL:

Start by fetching the data you want to visualize using an SQL query.


Process and Visualize Data with Seaborn:

Use a Python block to process the data and create visualizations.


Integrating Visual Reports into Retool UIs Through Webhook Response Blocks


Step 1: Create a Webhook-Based Workflow

Set Up a New Workflow:

  • Navigate to the Retool dashboard and create a new workflow.

  • Choose webhook mode to enable the workflow to receive POST requests from external services.

Auto-Generate Webhook URL:

  • Retool will provide a unique URL endpoint for the webhook. This URL will receive POST requests.


Step 2: Configure the Webhook to Receive Data

Define JSON Input Shape:

  • Specify the expected JSON structure for the incoming POST requests.

Set Up the External Service:

  • Configure the external service (e.g., Stripe, GitHub) to send POST requests to the Retool webhook URL with the appropriate data payload.


Step 3: Process Data and Create Visualizations

Add a Python Code Block to Process Data:

  • Use a Python block to process the incoming data and generate visual reports using a library like Seaborn.


Step 4: Return the Visualization to the Webhook Response

Configure Webhook Response:

  • Ensure the webhook response returns the base64-encoded image generated by the Python block.


Step 5: Integrate the Visualization into Retool UI

Add an Image Component to Retool UI:

  • In your Retool app, add an Image component to the UI where you want to display the visual report.

Handle Webhook Response and Set Image Source:

  • Use a Retool query to handle the webhook response and set the source of the Image component.

Bind the Image Component to the JavaScript Output:

  • Bind the src property of the Image component to the output of the Retool query..

                 

Configuring Python Code Block Settings


To ensure optimal performance of Python code blocks in Retool workflows, it’s essential to configure settings such as timeout, retry count, and interval settings. Here’s a guide on how to adjust these settings effectively.


Adjusting Settings for Optimal Performance


This section explains how to adjust settings for optimal performance.


Timeout Settings:

Purpose: The timeout setting determines the maximum time a Python code block can run before terminating it, preventing long-running scripts from blocking the workflow.


How to Adjust:

  1. Open your workflow in Retool.

  2. Click on the Python code block you want to configure.

  3. In the settings panel, locate the "Timeout" option.

  4. Set an appropriate timeout value (e.g., 30 seconds, 60 seconds).


Retry Count:

Purpose: The retry count setting specifies how often Retool should attempt to re-run the Python code block if it fails. This setting helps handle transient errors or temporary issues.


How to Adjust:

  1. Open your workflow in Retool.

  2. Click on the Python code block you want to configure.

  3. In the settings panel, locate the "Retry Count" option.

  4. Set the number of retry attempts (e.g., 3 retries).


Interval Settings:

Purpose: The interval setting defines the delay between each retry attempt. This helps prevent immediate re-runs in case of temporary issues that might resolve themselves after a short period.


How to Adjust:

  1. Open your workflow in Retool.

  2. Click on the Python code block you want to configure.

  3. In the settings panel, locate the "Retry Interval" option.

  4. Set the interval duration between retries (e.g., 5 seconds).


Also, check out the Retool’s self-hosted exploration and learn more about it.


Engaging with the community and providing feedback helps a lot of new users as well as old users in performing with Retool. You can check how Retool acts as an excellent platform for community and feedback from the following section.


Engaging with the Community


Retool is committed to fostering a vibrant and engaged user community. This section covers more on it and tells you how Retool actually does it.


Access to Python Code Blocks for Cloud Customers, with Plans to Extend to On-Premises Customers


Retool provides access to Python code blocks primarily for Cloud customers, allowing them to directly leverage Python’s powerful data processing and visualization capabilities within their workflows. This feature enhances the ability to create dynamic and complex applications with ease.

  • Cloud Customers: Python code blocks are readily available, enabling seamless integration with existing Retool workflows. Cloud customers can also use built-in libraries like Pandas, NumPy, and Seaborn to enhance their data processing and visualization tasks.

  • On-Premises Customers: Retool plans to extend access to Python code blocks for on-premises customers shortly. It will also enable on-premises users to benefit from Python’s robust capabilities, fostering a more inclusive and powerful development environment.


By providing access to powerful tools like Python code blocks for Cloud customers and planning to extend this to on-premises customers, Retool ensures a broad reach of its capabilities. 


Conclusion 


Explore the full potential of your Retool applications with Python! Enhance your workflows with powerful data manipulation, seamless API integrations, and advanced automation capabilities. 

Ready to improve your projects? Partner with Toolpioneers today and transform your software development process. Discover how our expert guidance can help you leverage the power of Python within Retool for unmatched efficiency and innovation.

bottom of page