Skip to content

Commit 3147599

Browse files
committed
Per discussion with @dom96, updated all examples to use @handler wrapper. Additionally updated publish to a queue example to use json.dumps() as opposed to the converter function and removed the unnecessary import
1 parent 95b293d commit 3147599

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/content/docs/workers/languages/python/examples.mdx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ In addition to those examples, consider the following ones that illustrate Pytho
1414
## Parse an incoming request URL
1515

1616
```python
17-
from workers import Response
17+
from workers import handler, Response
1818
from urllib.parse import urlparse, parse_qs
1919

20+
@handler
2021
async def on_fetch(request, env):
2122
# Parse the incoming request URL
2223
url = urlparse(request.url)
@@ -38,8 +39,9 @@ async def on_fetch(request, env):
3839
## Parse JSON from the incoming request
3940

4041
```python
41-
from workers import Response
42+
from workers import handle, Response
4243

44+
@handler
4345
async def on_fetch(request):
4446
name = (await request.json())["name"]
4547
return Response(f"Hello, {name}")
@@ -50,10 +52,11 @@ async def on_fetch(request):
5052
```python
5153
# To use the JavaScript console APIs
5254
from js import console
53-
from workers import Response
55+
from workers import handler, Response
5456
# To use the native Python logging
5557
import logging
5658

59+
@handler
5760
async def on_fetch(request):
5861
# Use the console APIs from JavaScript
5962
# https://developer.mozilla.org/en-US/docs/Web/API/console
@@ -78,14 +81,11 @@ async def on_fetch(request):
7881

7982
```python
8083
from js import Object
81-
from pyodide.ffi import to_js as _to_js
84+
import json
8285

83-
from workers import Response
84-
85-
# to_js converts between Python dictionaries and JavaScript Objects
86-
def to_js(obj):
87-
return _to_js(obj, dict_converter=Object.fromEntries)
86+
from workers import handler, Response
8887

88+
@handler
8989
async def on_fetch(request, env):
9090
# Bindings are available on the 'env' parameter
9191
# https://developers.cloudflare.com/queues/
@@ -94,7 +94,7 @@ async def on_fetch(request, env):
9494
# We can also pass plain text strings
9595
await env.QUEUE.send("hello", contentType="text")
9696
# Send a JSON payload
97-
await env.QUEUE.send(to_js({"hello": "world"}))
97+
await env.QUEUE.send(json.dumps({"hello": "world"}))
9898

9999
# Return a response
100100
return Response.json({"write": "success"})
@@ -103,8 +103,9 @@ async def on_fetch(request, env):
103103
## Query a D1 Database
104104

105105
```python
106-
from workers import Response
106+
from workers import handler, Response
107107

108+
@handler
108109
async def on_fetch(request, env):
109110
results = await env.DB.prepare("PRAGMA table_list").all()
110111
# Return a JSON response

0 commit comments

Comments
 (0)