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:
bashCopied1airplane 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:
yamlCopied1parameters:2- name: User email3slug: user_email4type: shorttext5description: 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.
yamlCopied1python:2envVars:3# From value4ENVIRONMENT:5value: production6# From config variable7MY_SECRET:8config: MY_SECRET_CONFIG_VAR
Access environment variables using standard Python patterns. For example:
pythonCopied1my_secret = os.getenv('MY_SECRET')
Develop your script
Airplane runs your code using Python 3.9.
A minimal Airplane python script looks like this (and gets generated by
airplane init
):pythonCopied1# Put the main logic of the task in the main function.2def main(params):3print("parameters:", params)45# You can return data to show output to users.6# Output documentation: https://docs.airplane.dev/tasks/output7return [8{"element": "hydrogen", "weight": 1.008},9{"element": "helium", "weight": 4.0026}10]
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:bashCopied1$ airplane dev your_task.task.yaml -- --user_email="hello@airplane.dev"2parameters: {'user_email': 'hello@airplane.dev'}
For more details on local task execution, including how to set environment variables, see
Local task execution.
Deploy to Airplane
Finally, run
deploy
to push your script to Airplane:bashCopied1airplane deploy your_task.task.yaml
Once deployed, go to the Tasks page to see and run your task!