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
Upgrading 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:
bashCopied1airplane 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:
pythonCopied1import airplane23@airplane.task()4def delete_user():5data = [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]1314# Sort the data in ascending order by name.15data = sorted(data, key=lambda u: u["name"])1617# You can return data to show output to users.18# Output documentation: https://docs.airplane.dev/tasks/output19return 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:
bashCopied1$ 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:pythonCopied1import airplane23@airplane.task()4def delete_user(user_email: str):5"""Deletes a user by email.67Args:8user_email: The user to delete9"""10pass
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:
pythonCopied1import airplane23@airplane.task(4env_vars=[5# Set an env var with a predefined value6airplane.EnvVar(7name="MY_ENV_VAR",8value="MY_ENV_VAR_VALUE",9),10# Set an env var from a defined config var11airplane.EnvVar(12name="MY_ENV_VAR",13config_var_name="my_config_var_name",14),15],16)17def delete_user(user_email: str):18"""Deletes a user by email.1920Args:21user_email: The user to delete22"""23pass
Access environment variables using standard Python patterns. For example:
pythonCopied1my_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:bashCopied1airplane deploy
Once deployed, go to the Tasks page to run and share your task!