Edge
Tutorials
Python Flask Server

Tutorial for deploying a Python Flask Server on Wasmer Edge

In this guide, you'll learn the process of deploying a python flask application on Wasmer Edge. We'll be deploying a basic flask server with default index endpoint.

Deploying a Python Application

Install Wasmer CLI

Install the latest version of Wasmer CLI following the instructions here.

ℹ️

You can see all commands available with wasmer --help. You can also read the CLI documentation online.

⚠️

Please check that you have the latest version of wasmer runtime as this tutorial depends on version 4.2.3 or higher.

Log in into Wasmer

Create a new account in Wasmer (opens in a new tab). Then, log in into the Wasmer CLI and follow the provided steps to provide the CLI access to your Wasmer account.

wasmer login

Initialize the Python Application Starter template

$ wasmer app create
App type:
  Static website
  HTTP server
  Browser shell
  JS Worker (experimental)
> Python Application
ℹ️

This is an interactive command. You can also use the --type flag to specify the app type.

Further you will be prompted to enter your package name and app name. You can choose any name you like.

🚧

The app names should be globally unique across all apps on the registry.

You directory composition should look like this:

Configure the Python Virtual Environment

$ python -m venv .env

You directory composition should look like this with .env folder added:

Activate the Python Virtual Environment

$ source .env/bin/activate

Install the Python Flask package

$ pip install flask

Writing the Python Flask Application

Modify the main.py file to look like this:

from flask import Flask
 
app = Flask(__name__)
 
@app.route("/")
def index():
    return "Hello, from Wasmer Edge 🚀"

You can change the return string to anything you like.

Test the flask application locally

$ python -m flask --app /src/main run --debug --no-reload
 * Serving Flask app './src/main'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

Configure the wasmer.toml and app.yaml files

In our wasmer.toml we need to set up some args and env variables.

[package]
name = "wasmer/python-flask-server"
version = "0.1.1"
description = "wasmer/python-flask-server python application"
readme = "README.md"
 
[dependencies]
"wasmer/python" = "^3.12.6"
 
[fs]
"/src" = "./src"
"/cpython/lib/python3.12" = "./.env/lib/python3.11"
 
[[command]]
name = "script"
module = "wasmer/python:python"
runner = "wasi"
 
[command.annotations.wasi]
main-args = [ # This is the command we want the python runner to run
    "-m",
    "flask",
    "--app",
    "/src/main",
    "run",
    "--debug",
    "--no-reload",
]
env = ["PYTHONEXECUTABLE: /bin/python"] # This is the path to the python executable

In our app.yaml we want to provide the same args as we did in the wasmer.toml file.

---
kind: wasmer.io/App.v0
name: wasmer-python-flask-server-myapp-example
package: wasmer/python-flask-server-myapp-example@0.1.0
cli_args: # This is the command we want the python runner to run on Wasmer Edge
  - -m
  - flask
  - --app
  - /src/main
  - run
  - --debug
  - --no-reload
env:
  PYTHONEXECUTABLE: /bin/python # This is the path to the python executable

Now, we can test if our application runs with wasmer

$ wasmer run . --net
 * Serving Flask app '/src/main'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

The above command will start a web server on http://127.0.0.1:5000.

Let's try to cURL the server:

$ curl http://127.0.0.1:5000
Hello, from Wasmer Edge 🚀

Deploying your Python Application

Deploying is the easiest part. Just run the following command:

$ wasmer deploy
Loaded app from: /.../wasmer-edge/python-flask/app.yaml
Deploying app wasmer-python-flask-server-myapp-example...
 
  App dynamite-bud/wasmer-python-flask-server-myapp-example was successfully deployed!
 
> App URL: https://wasmer-python-flask-server-myapp-example.wasmer.app
> Versioned URL: https://2mzhyi8cqzxr.id.wasmer.app
> Admin dashboard: https://wasmer.io/apps/wasmer-python-flask-server-myapp-example
 
Waiting for new deployment to become available...
(You can safely stop waiting now with CTRL-C)

Now you can visit the URL and see your application running on Wasmer Edge.

ℹ️

You must be in the directory holding the wasmer.toml and app.yaml config files.

💁

You can view the above info again using wasmer app info.

Conclusion

Congratulations! You have successfully deployed your python flask application on Wasmer Edge.

Resources

wasmerio/python-flask-server-myapp-example