All docs / Build
Python apps
Write apps in Python with the same bindings: entry classes, requests, KV, D1 and R2 from Python.
Open in the consoleApps can be written in Python (Pyodide on workerd, as in Cloudflare Python Workers). Create one with rr init my-app --python, by choosing Python when creating an app, or by asking Studio for Python.
Entry class
python
from workers import Response, WorkerEntrypoint
from urllib.parse import urlparse
import json
class Default(WorkerEntrypoint):
async def fetch(self, request):
path = urlparse(request.url).path
if path == "/api/hello":
return Response(json.dumps({"hello": "world"}), headers={"content-type": "application/json"})
return Response("not found", status=404)
async def scheduled(self, controller):
print("cron fired:", controller.cron)Bindings from Python
python
class Default(WorkerEntrypoint):
async def fetch(self, request):
env = self.env
# KV
await env.CACHE.put("greeting", "hello")
greeting = await env.CACHE.get("greeting")
# D1
row = await env.DB.prepare("SELECT count(*) AS n FROM posts").first()
result = await env.DB.prepare("SELECT id, title FROM posts WHERE id = ?").bind(1).all()
# R2
await env.FILES.put("hello.txt", "hi")
obj = await env.FILES.get("hello.txt")
text = await obj.text() if obj else None
return Response(f"{greeting} · {row.n} posts · {text}")