Skip to main content

Spin Python SDK

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.

API Documentation

Example

Prerequisites

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.25.1 spin-sdk==5.0.0 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:up/http-trigger@4.1.0 componentize app -o app.wasm"
EOF
cat >app.py <<EOF
from spin_sdk import http
from spin_sdk.http import Request, Response

class HttpHandler(http.Handler):
    async 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:

python -m 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 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.

Metadata

Release files for spin-sdk 5.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spin-sdk 5.0.0
File Size Uploaded
spin_sdk-5.0.0.tar.gz 262.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spin-sdk 5.0.0
File Interpreter ABI Platform
spin_sdk-5.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 812.1 kB

Release files / spin_sdk-5.0.0.tar.gz

Download URL spin_sdk-5.0.0.tar.gz
Size 262.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bedd856e337bc3f4fec7ff9fff9b5ce78a81ac67d0f972a3ae9c9bf7e00ca5e6
BLAKE2b-256 checksum
How to use checksums
a9821b1f7243d6e0577ae48b4a42520fefe30baa4d95af20406d5ffc0f4258a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / spin_sdk-5.0.0-py3-none-any.whl

Download URL spin_sdk-5.0.0-py3-none-any.whl
Size 550.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
274117c4d1210f2ba22b8d76338e966c7dcfd637c2e8b57dbd2e96af4adda7cc
BLAKE2b-256 checksum
How to use checksums
724f839fb2ef9267071b210f0642175f695a6fcea248e7db0df2e28b5ddd387a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page