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

Visit the New Task page to get started.

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 define parameters for your task—these parameters will later be passed 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:
bash
Copied
1
$ airplane init --slug YOUR_TASK_SLUG
A minimal Airplane task in Python looks like this (and gets generated by airplane init):
python
Copied
1
# your_task_file.py
2
# Linked to https://app.airplane.dev/t/your_task_slug [do not edit this line]
3
4
import airplane
5
import json
6
7
def main(params):
8
print("parameters:", params)
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": "hello@airplane.dev", "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:
Copied
1
/
2
requirements.txt
3
airplane_tasks/
4
my_task.py
5
other_module/
6
__init__.py
7
other_submodule.py

Develop locally

To test your task locally, ensure you have the Airplane CLI installed, then run the dev command:
bash
Copied
1
$ airplane dev ./your_task_file.py -- --name="hello@airplane.dev"
2
Hello, hello@airplane.dev!
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:
bash
Copied
1
airplane deploy ./your_task_file.py
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:
bash
Copied
1
pip install airplanesdk

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:
python
Copied
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")