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.
This guide will get you up and running with an example Python task. For specific details see:

Getting started

We'll use the Airplane CLI airplane to demonstrate how to get started with a Python task.
If you haven't yet, first install the Airplane CLI by following the instructions at Installing the CLI.

Create your task

Once you've installed and logged in to the CLI, navigate to the folder where you want to create your task and run:
bash
Copied
1
airplane tasks init
You'll be prompted for some information:
  • Enter a name for the task.
  • Select Python as the kind.
  • Enter a name for the script and the definition file. The script contains the task code (e.g. delete_user_by_email.py) while the definition file contains task metadata (e.g. delete_user_by_email.task.yaml).

Parameters

Parameters allow you to prompt users for input before triggering your task. See Parameters for more information.
Open up the definition file (the file with extension .task.yaml) to add parameters to the task.
Example:
yaml
Copied
1
parameters:
2
- name: User email
3
slug: user_email
4
type: shorttext
5
description: The email address of the user
See Task definition for a full reference on available parameters.

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.
yaml
Copied
1
python:
2
envVars:
3
# From value
4
ENVIRONMENT:
5
value: production
6
# From config variable
7
MY_SECRET:
8
config: MY_SECRET_CONFIG_VAR
Access environment variables using standard Python patterns. For example:
python
Copied
1
my_secret = os.getenv('MY_SECRET')

Develop your script

Airplane runs your code using Python 3.10.
A minimal Airplane python script looks like this (and gets generated by airplane tasks init):
python
Copied
1
# This is your task's entrypoint. When your task is executed, this
2
# function will be called.
3
def main(params):
4
data = [
5
{ "id": 1, "name": "Gabriel Davis", "role": "Dentist" },
6
{ "id": 2, "name": "Carolyn Garcia", "role": "Sales" },
7
{ "id": 3, "name": "Frances Hernandez", "role": "Astronaut" },
8
{ "id": 4, "name": "Melissa Rodriguez", "role": "Engineer" },
9
{ "id": 5, "name": "Jacob Hall", "role": "Engineer" },
10
{ "id": 6, "name": "Andrea Lopez", "role": "Astronaut" },
11
]
12
13
# Sort the data in ascending order by name.
14
data = sorted(data, key=lambda u: u["name"])
15
16
# You can return data to show output to users.
17
# Output documentation: https://docs.airplane.dev/tasks/output
18
return data
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: { "user_email": "hello@airplane.dev" }.

Install dependencies

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.
For more information, see Python dependencies.

Develop locally

To test your task locally, ensure you have the Airplane CLI installed, then run the dev command:
bash
Copied
1
$ airplane dev
For more details on local task execution, including how to set environment variables, see Studio.

Deploy to Airplane

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