This is an SDK for creating Spin apps using Python.
Note that this SDK supersedes an earlier, experimental version, which may be found in the old-sdk branch.
- Python 3.10 or later and pip
- componentize-py
- spin-sdk
- Spin 4.0 or later.
- MyPy -- This is optional, but useful for during development.
Once you have Python and pip installed, you can use the latter to create and enter a virtual environment and then install the desired packages
python -m venv .venv
source .venv/bin/activate
pip install componentize-py==0.23.0 spin-sdk==4.0.0 mypy==1.8.0A minimal app requires two files: a spin.toml and a Python script, which we'll
name app.py:
cat >spin.toml <<EOFspin_manifest_version = 2[application]name = "hello"version = "0.1.0"authors = ["Dev Eloper <dev@example.com>"][[trigger.http]]route = "/..."component = "hello"[component.hello]source = "app.wasm"[component.hello.build]command = "componentize-py -w spin:up/http-trigger@4.0.0 componentize app -o app.wasm"EOFcat >app.py <<EOFfrom spin_sdk import httpfrom spin_sdk.http import Request, Responseclass HttpHandler(http.Handler): async def handle_request(self, request: Request) -> Response: return Response( 200, {"content-type": "text/plain"}, bytes("Hello from Python!", "utf-8") )EOFOnce you've created those files, you can check, build, and run your app:
python-mmypyapp.pyspinbuild-uFinally, you can test your app using e.g. curl in another terminal:
curl -i http://127.0.0.1:3000If all goes well, you should see something like:
HTTP/1.1 200 OK
content-type: text/plain
content-length: 18
date: Thu, 11 Apr 2024 17:42:31 GMT
Hello from Python!
Please file an issue if you have any trouble.
See the examples directory in the repository for more examples.