Running Python ASGI apps in the browser via Pyodide + a service worker
TL;DR · AI Summary
By running Python ASGI web applications entirely in the browser using Pyodide and a dedicated service worker, this project intercepts all same-origin requests under `/app/` and executes them against the Python app via the ASGI protocol—removing the need for a backend server except for static files. The mechanism is demonstrated with both a FastAPI demo and the full Datasette app, confirming its generality across ASGI apps.
Key Takeaways
- The solution uses Pyodide + Service Worker to execute ASGI protocols end-to-end
- A full Datasette 1.0a31 instance runs in the browser (https://simonw.github.io/r
- Unlike the earlier Web Worker approach, this method supports `<script>>` tag exe
Outline
Jump quickly between sections.
Describes how Datasette Lite’s earlier Web Worker implementation failed to execute `<script>>` tags, prompting the search for a better solution.
Leverages Pyodide to embed Python runtime in the browser and Service Worker to intercept `/app/` requests and forward them via ASGI to the Python app.
Service Worker captures fetch requests → parses ASGI scope/body → invokes ASGI app in Pyodide → returns response; no backend required beyond static assets.
Includes a basic FastAPI ASGI demo and a full Datasette 1.0a31 demo, confirming the approach works across diverse ASGI frameworks.
Author is still understanding the internals, but plans to integrate the technique into Datasette Lite itself once clarified.
Mindmap
See how the topics connect at a glance.
查看大纲文本(无障碍 / 无 JS 友好)
- Browser-based Python ASGI Apps with Pyodide & Service Worker
- 核心目标
- 零后端服务(仅需静态文件)
- 支持任意 ASGI 应用
- 关键技术栈
- Pyodide (Python in WASM)
- Service Worker (请求拦截)
- ASGI Protocol (Python ↔ Browser)
- 落地案例
- FastAPI Demo
- Datasette 1.0a31 Full Run
Highlights
Key sentences worth saving and sharing.
This project intercepts all same-origin requests under `/app/` and executes them against the Python app via the ASGI protocol—removing the need for a backend server except for static files.
Any JavaScript in `<script>>` tags would not be executed — breaking some Datasette functionality and a whole lot of Datasette plugins.
I'm still getting my head around exactly how it works, but once I've done that I plan to upgrade Datasette Lite itself.
ResearchRunning Python ASGI apps in the browser via Pyodide + a service worker— By running Python ASGI web applications entirely in the browser using Pyodide and a dedicated service worker, this project intercepts all same-origin requests under /app/ and executes them against the Python app via the ASGI protocol—removing the need for a backend server except for static files. The mechanism is demonstrated with both a FastAPI demo and the full Datasette app, confirming its generality across ASGI apps.
Datasette Lite is my version of Datasette that runs entirely in the browser using Pyodide in WebAssembly.
When I first built it four years ago I used Web Workers and code that intercepts navigation operations and fetches the generated HTML by running the Python app.
This worked, but had the disadvantage that any JavaScript in <script> tags would not be executed - breaking some Datasette functionality and a whole lot of Datasette plugins.
This morning I set Claude Opus 4.8 the task (in Claude Code for web) of figuring out how to run Python ASGI apps in Pyodide using Service Workers instead, and it seems to work! Here's a basic ASGI FastCGI demo and here's a demo that runs Datasette 1.0a31.
I'm still getting my head around exactly how it works, but once I've done that I plan to upgrade Datasette Lite itself.