Python
Write custom tasks in Python
Airplane makes it easy to write and deploy Python-based tasks when you need to write custom code (that SQL or REST might not be able to handle).

Getting Started

To create a Python task, visit the New Task page.

Define Your Task

In this first step, you'll be asked to give your task a name and an (optional) description.

Parameters

Here, you can add parameters for your task - these parameters will later be based to your code. (See the Parameters documentation for a full reference on what parameters are available.)

Build Your Task

You'll only define environment variables in this next section. For custom code tasks, the code is deployed separately after the task is created.

Environment Variables

Environment variables can be entered directly ("from value"), or you can reference config variables ("from config variable"). Use config variables for secrets and/or values you want to share across multiple tasks.

Configure Settings

In this last step, you can optionally configure permissions, timeouts, run constraints, and more. These settings are optional.
That's it! Finish the task creation wizard and you have a shrinkwrapped task for safely running Python scripts.

Deploy Your Code

Airplane runs your code using Python 3.9.
Once created, the UI will show you instructions on how to init and deploy the task. Your task has a slug, which is a human-readable ID, which you'll need for running the init and deploy commands.
In your own code repository, run airplane init to create a blank task file:
1
$ airplane init --slug YOUR_TASK_SLUG ./your_task_file.py
Copied!
A minimal Airplane task in Python looks like this (and gets generated by airplane init):
your_task_file.py
1
# Linked to https://app.airplane.dev/t/your_task_slug [do not edit this line]
2
3
import airplane
4
import json
5
6
def main(params):
7
print("parameters:", params)
Copied!
When Airplane executes your script, it will look for a main function. This function should accept a single argument (params) which will be set to an object of the parameter values passed in by the user: { "email": "[email protected]", "dry_run": false }.

Python Dependencies and Task Root

To install dependencies, you can add them to a requirements.txt file, and Airplane will pip install from it when deploying.
You should place this requirements.txt file either next to your script or in a parent directory. The closest directory containing requirements.txt becomes the task root - the task root is uploaded when deploying.
If you want to import from other modules, ensure that the requirements.txt is placed in a place where the other modules will get included:
1
/
2
requirements.txt
3
airplane_tasks/
4
my_task.py
5
other_module/
6
__init__.py
7
other_submodule.py
Copied!

Develop Locally

To test your task locally, ensure you have the Airplane CLI installed, then run the dev command:
1
$ airplane dev ./your_task_file.py -- --name="[email protected]"
Copied!
For more details on local task execution, including how to set environment variables, see these docs.

Deploy to Airplane

Finally, run deploy to push your script to Airplane:
1
airplane deploy ./your_task_file.py
Copied!
Once deployed, go to the Tasks page to see and run your task!

SDK

The Airplane Python SDK is available via PyPI (airplanesdk). To install it, run:
1
pip install airplanesdk
Copied!

Output

This SDK can be used to produce Outputs that will be rendered in the UI when someone runs your task. To learn more, see the Outputs documentation:
1
import airplane
2
3
# Output using the default output name
4
airplane.write_output("Show me what you got")
5
6
# You can also separate outputs into groups by attaching names:
7
airplane.write_named_output("saying", "Show me what you got")
8
airplane.write_named_output("saying", "Welcome to the club, pal")
9
airplane.write_named_output("name", "Summer")
Copied!
Last modified 1mo ago