Experimental SDK for Spin and Componentize-Py
Project description
Experimental Spin SDK for componentize-py
This is an experimental SDK for creating Spin apps using componentize-py
.
API Documentation
Example
Prerequisites
- Python 3.10 or later and pip
- componentize-py
- spin-sdk
- MyPy
- This is optional, but useful for during development.
- Spin 2.2 or later.
- As of this writing Spin 2.2 has not yet been released, so we must use the
main
branch of Spin for the time being. You can find pre-built binaries here.
- As of this writing Spin 2.2 has not yet been released, so we must use the
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.10.0 spin-sdk==0.0.5 mypy==1.8.0
Hello, World
A minimal app requires two files: a spin.toml
and a Python script, which we'll name app.py
:
cat >spin.toml <<EOF
spin_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-http componentize app -o app.wasm"
EOF
cat >app.py <<EOF
from spin_sdk.http import simple
from spin_sdk.http.simple import Request, Response
class IncomingHandler(simple.IncomingHandler):
def handle_request(self, request: Request) -> Response:
return Response(
200,
{"content-type": "text/plain"},
bytes("Hello from Python!", "utf-8")
)
EOF
Once you've created those files, you can check, build, and run your app:
mypy app.py
spin build -u
Finally, you can test your app using e.g. curl
in another terminal:
curl -i http://127.0.0.1:3000
If all goes well, you should see:
HTTP/1.1 200 OK
content-type: text/plain
transfer-encoding: chunked
date: Tue, 09 Jan 2024 18:26:52 GMT
Hello from Python!
Please file an issue if you have any trouble.
See the examples directory in the repository for more examples.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.