Documentation
Python (Beta)

Python

Python is a popular programming language that is easy to learn and to use.

It's commonly used for building prototypes of web applications, server-side templated pages, and backend API servers.

Instructions

Set up an Airbase project with the Python framework

Inside a project folder (e.g. example/), run airbase configure and select python from the list of frameworks when prompted.

user@laptop example % airbase configure
 
Configuring project for Airbase
? Project Name: example
? Framework: python
? Command to build production deployment: echo "no build required"
? Command to start production server: python app.py
 
{
  name: "example"
  framework: "python"
    scripts: {
    build: "echo \"no build required\""
    start: "source venv/bin/activate && python app.py"
  }
}
 
? Save configuration? Yes
 
Configuration saved in ./airbase.json
Run airbase build to create a deployment package.
user@laptop example %

In this example, we'll be using the Flask framework to build a hello world app. This command will create a venv, install Flask, and save the dependencies to requirements.txt for future use.

  python3 -m venv venv && source venv/bin/activate && pip install flask && pip freeze > requirements.txt

Create an app.py with a simple Hello World application written in Flask.

from flask import Flask
 
app = Flask(__name__)
 
@app.route('/')
def index():
return 'Hello World'
 
app.run(host= '0.0.0.0', port=3000)

Update the airbase.json configuration file with the include globs for all your files and their dependencies.

  {
    "name": "example"
    "framework": "python"
    "scripts": {
      "build": "echo \"no build required\""
      "start": "source venv/bin/activate && python app.py"
    },
+   "include": [
+     "venv/**/*",
+     "app.py"
+   ]
  }

Build and deploy your application

Now that the Airbase project has been set up, run airbase build as normal to build your application.

Then deploy the application with airbase deploy.

Further Reading

Using venv to manage Python packages

We recommend using a venv to create an isolated Python environment for development and testing.

Using just one command, we can initialise and install all the dependencies in the virtual environment:

python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt

And with another command, the entire environment can be frozen and reproduced for later use:

python3 -m venv venv && source venv/bin/activate && pip freeze > requirements.txt

When the venv is activated, the name of the venv will appear at the start of the terminal prompt, like this:

(venv) user@laptop template-flask-helloworld % python3 --version
Python 3.11.5
(venv) user@laptop template-flask-helloworld %

Activating the venv

The activation script needs to be sourced in the terminal session and can't be executed directly.

Re-enter the environment by running

source venv/bin/activate

Deactivating the venv

To leave the venv environment at any time, run

deactivate