Skip to main content

Experimental SDK for Spin and Componentize-Py

Project description

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. (The old template remains on main temporarily to preserve compatibility for some inbound links; it will be removed or replaced.)

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.11.0 spin-sdk==2.0.0rc1 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:

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:

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spin-sdk-2.0.0rc2.tar.gz (68.1 kB view details)

Uploaded Source

Built Distribution

spin_sdk-2.0.0rc2-py3-none-any.whl (92.5 kB view details)

Uploaded Python 3

File details

Details for the file spin-sdk-2.0.0rc2.tar.gz.

File metadata

  • Download URL: spin-sdk-2.0.0rc2.tar.gz
  • Upload date:
  • Size: 68.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for spin-sdk-2.0.0rc2.tar.gz
Algorithm Hash digest
SHA256 457f43e69e9c11090c7d31c9cc0e24da4ee2d1679fb2ad4636c517dc38a6256e
MD5 7512c3e5f6ad8e1f32bc0005172174f9
BLAKE2b-256 ca8cfd6f14c0ce119a996ca0e89407d61cc4f234661639f274782ffc35cfceef

See more details on using hashes here.

Provenance

File details

Details for the file spin_sdk-2.0.0rc2-py3-none-any.whl.

File metadata

  • Download URL: spin_sdk-2.0.0rc2-py3-none-any.whl
  • Upload date:
  • Size: 92.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for spin_sdk-2.0.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 fb5af4475e3cc4b013167c2b718dc0349c0ef5593f63572b70e2d31fbf6277cb
MD5 8e27ede7b1ea81d432a7c020cd72126d
BLAKE2b-256 0633696e49c1681ef5154c59ddf9ebe53c7776bf0deaff54eceba4d2a2d68b7a

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page