diff --git a/01-hello/src/entry.py b/01-hello/src/entry.py index 7f5eadd..7837417 100644 --- a/01-hello/src/entry.py +++ b/01-hello/src/entry.py @@ -1,5 +1,5 @@ -from workers import handler, Response +from workers import WorkerEntrypoint, Response -@handler -async def on_fetch(request, env): +class Default(WorkerEntrypoint): + async def fetch(self, request, env): return Response("Hello world!") diff --git a/01-hello/wrangler.toml b/01-hello/wrangler.toml index e702f56..b606704 100644 --- a/01-hello/wrangler.toml +++ b/01-hello/wrangler.toml @@ -1,4 +1,4 @@ name = "hello-python" main = "src/entry.py" compatibility_flags = ["python_workers"] -compatibility_date = "2024-03-29" +compatibility_date = "2025-08-14" diff --git a/02-binding/src/entry.py b/02-binding/src/entry.py index 1baf0cb..5218af0 100644 --- a/02-binding/src/entry.py +++ b/02-binding/src/entry.py @@ -1,7 +1,7 @@ -from workers import handler, Response +from workers import WorkerEntrypoint, Response -@handler -async def on_fetch(request, env): +class Default(WorkerEntrypoint): + async def fetch(selfrequest, env): await env.FOO.put("bar", "baz") bar = await env.FOO.get("bar") return Response(bar) # returns "baz" diff --git a/02-binding/wrangler.toml b/02-binding/wrangler.toml index da1f830..bcf576b 100644 --- a/02-binding/wrangler.toml +++ b/02-binding/wrangler.toml @@ -1,8 +1,8 @@ name = "hello-python-bindings" main = "src/entry.py" compatibility_flags = ["python_workers"] -compatibility_date = "2024-03-29" +compatibility_date = "2025-08-14" kv_namespaces = [ { binding = "FOO", id = "" } -] \ No newline at end of file +] diff --git a/03-fastapi/requirements.txt b/03-fastapi/cf-requirements.txt similarity index 100% rename from 03-fastapi/requirements.txt rename to 03-fastapi/cf-requirements.txt diff --git a/04-langchain/requirements.txt b/04-langchain/cf-requirements.txt similarity index 100% rename from 04-langchain/requirements.txt rename to 04-langchain/cf-requirements.txt diff --git a/05-query-d1/src/entry.py b/05-query-d1/src/entry.py index ade57bd..2bc355e 100644 --- a/05-query-d1/src/entry.py +++ b/05-query-d1/src/entry.py @@ -1,7 +1,7 @@ -from workers import handler, Response +from workers import WorkerEntrypoint, Response -@handler -async def on_fetch(request, env): +class Default(WorkerEntrypoint): + async def fetch(self, request, env): query = """ SELECT quote, author FROM qtable diff --git a/05-query-d1/wrangler.toml b/05-query-d1/wrangler.toml index 73653e7..1b9d64f 100644 --- a/05-query-d1/wrangler.toml +++ b/05-query-d1/wrangler.toml @@ -1,5 +1,5 @@ #:schema node_modules/wrangler/config-schema.json -compatibility_date = "2024-08-06" +compatibility_date = "2025-08-14" main = "src/entry.py" compatibility_flags = ["python_workers"] diff --git a/06-vendoring/src/worker.py b/06-vendoring/src/worker.py index 169e335..75420d9 100644 --- a/06-vendoring/src/worker.py +++ b/06-vendoring/src/worker.py @@ -1,11 +1,13 @@ import jinja2 from fastapi import FastAPI, Request +from workers import WorkerEntrypoint environment = jinja2.Environment() template = environment.from_string("Hello, {{ name }}!") -async def on_fetch(request, env): +class Default(WorkerEntrypoint): + async def fetch(self, request, env): import asgi return await asgi.fetch(app, request, env) diff --git a/README.md b/README.md index 20d30bf..2e57c92 100644 --- a/README.md +++ b/README.md @@ -16,16 +16,18 @@ Need to deploy your Worker to Cloudflare? Python Workers are in open beta and ha ## Examples - [**`01-hello/`**](01-hello) — the most basic Python Worker +- [**`05-query-d1/`**](05-query-d1) - shows how to query D1 with Python Workers +- [**`06-vendoring/`**](06-vendoring) - shows how to vendor packages + +These examples are deprecated: + - [**`02-binding/`**](02-binding) — shows how [bindings](https://developers.cloudflare.com/workers/configuration/bindings/) work in Python Workers. Put a key into Workers KV, and then read it. - [**`03-fastapi/`**](03-fastapi) — demonstrates how to use the [FastAPI](https://fastapi.tiangolo.com/) package with Python Workers - [**`04-langchain/`**](04-langchain) — demonstrates how to use the [LangChain](https://pypi.org/project/langchain/) package with Python Workers -- [**`05-query-d1/`**](05-query-d1) - shows how to query D1 with Python Workers -- [**`06-vendoring/`**](06-vendring) - shows how to vendor packages that are not included in the built-in package list ## Open Beta and Limits -- Python Workers are in open beta. Currently, you can only deploy Python Workers that use the standard library. [Packages](https://developers.cloudflare.com/workers/languages/python/packages/#supported-packages) **cannot be deployed** and will only work in local development by default. -- If you wish to join our closed beta which enables deploying packages, fill out [this form](https://forms.gle/827KAtKJTZdgAkNm7). +- Python Workers are in open beta. You can use packages in your Workers by vendoring them using the [pywrangler](https://github.com/cloudflare/workers-py?tab=readme-ov-file#pywrangler) tool. - You must add the `python_workers` compatibility flag to your Worker while Python Workers are in open beta. We’d love your feedback. Join the `#python-workers channel` in the [Cloudflare Developers Discord](https://discord.cloudflare.com/) and let us know what you’d like to see next.