Python build hooks
Run custom Python build scripts
Airplane allows you to run custom build scripts before and after dependencies are installed. This
lets you to customize your task's environment and perform additional setup steps your tasks may
require.
Build hooks in airplane.yaml
configuration file
Build hooks in
airplane.yaml
configuration fileThe simplest place to specify build hooks is in the
airplane.yaml
configuration file.yamlCopied1# airplane.yaml23python:4version: "3.11"5preinstall: echo "preinstall"6postinstall: echo "postinstall"
Build hook files
Build hook files
You can also specify build hooks in their own files. This is useful for writing more complex build
hooks that span multiple lines.
-
To run a script before dependency installation, create a file with the name
airplane_preinstall.sh
in the root directory of your project. -
To run a script after dependency installation, create a file with the name
airplane_postinstall.sh
in the root directory of your project.
Airplane runs your custom build scripts as executables.
Example: Web scraping with selenium
Example: Web scraping with selenium
Let's walk through an example of how we can use custom build scripts to set up
selenium for web scraping in Python.
- We first need a
requirements.txt
file that specifies the Pythonselenium
dependency.
Copied1selenium==4.4.3
- Then we add our custom build script called
airplane_postinstall.sh
that installsFirefox
andgeckodriver
. Both are needed forselenium
.
bashCopied1#!/bin/bash23set -euo pipefail45GECKODRIVER_VER=v0.31.06FIREFOX_VER=91.078apt update \9&& apt upgrade -y \10&& apt install -y1112# Install firefox13apt install -y \14firefox-esr \15libx11-xcb1 \16libdbus-glib-1-2 \17&& curl -sSLO https://download-installer.cdn.mozilla.net/pub/firefox/releases/${FIREFOX_VER}/linux-x86_64/en-US/firefox-${FIREFOX_VER}.tar.bz2 \18&& tar -jxf firefox-* \19&& mv firefox /opt/ \20&& chmod 755 /opt/firefox \21&& chmod 755 /opt/firefox/firefox2223# Install geckodriver24curl -sSLO https://github.com/mozilla/geckodriver/releases/download/${GECKODRIVER_VER}/geckodriver-${GECKODRIVER_VER}-linux64.tar.gz \25&& tar zxf geckodriver-*.tar.gz \26&& mv geckodriver /usr/bin/
- Lastly, we add the Python script,
selenium_demo.py
.
pythonCopied1import airplane23from selenium import webdriver4from selenium.webdriver import FirefoxOptions56@airplane.task()7def my_task():8opts = FirefoxOptions()9opts.add_argument("--headless")10with webdriver.Firefox(options=opts) as driver:11driver.get("https://airplane.dev")12return {"page_source_len": len(driver.page_source)}
For the full source code, see the
python_selenium_example template on GitHub.
You can make a local copy of the template with the Airplane CLI:
Copied1airplane init --template=python_selenium_example