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:

Before you begin

If you haven't yet, first install the Airplane CLI by following the instructions at Installing the CLI. (If you have already installed the Airplane CLI, ensure you have the latest version by Updating 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 task file. The file can have any name, but it must end in _airplane.py.
The init command will create your task file in a new Project along with a few other files:
  • requirements.txt: Contains any external dependencies that your task will use.
  • airplane.yaml: A Project configuration file that allows you to set project-wide configuration such as the Python version your task will be executed with.

Develop your task

The default Airplane Python task looks like this:
python
Copied
1
import airplane
2
3
@airplane.task()
4
def delete_user():
5
data = [
6
{"id": 1, "name": "Gabriel Davis", "role": "Dentist"},
7
{"id": 2, "name": "Carolyn Garcia", "role": "Sales"},
8
{"id": 3, "name": "Frances Hernandez", "role": "Astronaut"},
9
{"id": 4, "name": "Melissa Rodriguez", "role": "Engineer"},
10
{"id": 5, "name": "Jacob Hall", "role": "Engineer"},
11
{"id": 6, "name": "Andrea Lopez", "role": "Astronaut"},
12
]
13
14
# Sort the data in ascending order by name.
15
data = sorted(data, key=lambda u: u["name"])
16
17
# You can return data to show output to users.
18
# Output documentation: https://docs.airplane.dev/tasks/output
19
return data
When Airplane executes your task, it will look for a function decorated with airplane.task(). This function may accept one or more arguments, which will be set to the Parameter values passed in by the user.
Although this task is simple, Airplane tasks can Execute other tasks, call Airplane built-ins to perform common operations like query a database or send an email, display interactive Prompts to gather input from operators, and much more!

Develop locally

To test your task locally in Studio, run:
bash
Copied
1
$ airplane dev
For more details on local task execution, including how to set local environment variables and resources, see Studio.

Add Parameters

Parameters allow you to prompt users for input before triggering your task.
In the following example, we add a single parameter user_email to our task. The user_email must be provided when the task is executed:
python
Copied
1
import airplane
2
3
@airplane.task()
4
def delete_user(user_email: str):
5
"""Deletes a user by email.
6
7
Args:
8
user_email: The user to delete
9
"""
10
pass
For more information on configuration parameters, see Python parameter configuration.

Add 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.
In the following example, we add two environment variables to our task. The first is set directly from a value, and the second is set from a config variable:
python
Copied
1
import airplane
2
3
@airplane.task(
4
env_vars=[
5
# Set an env var with a predefined value
6
airplane.EnvVar(
7
name="MY_ENV_VAR",
8
value="MY_ENV_VAR_VALUE",
9
),
10
# Set an env var from a defined config var
11
airplane.EnvVar(
12
name="MY_ENV_VAR",
13
config_var_name="my_config_var_name",
14
),
15
],
16
)
17
def delete_user(user_email: str):
18
"""Deletes a user by email.
19
20
Args:
21
user_email: The user to delete
22
"""
23
pass
Access environment variables using standard Python patterns. For example:
python
Copied
1
my_secret = os.getenv('MY_SECRET')
For more information on configuration environment variables, see Python task configuration.

Install dependencies

To install dependencies, you can add them to your 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 root of your project.
For more information, see Python dependencies.

Deploy to Airplane

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