跳到正文
原文
Simon Willison 博客· Simon Willison 博客·· 2026-05-31精选AI 评分73

在浏览器中通过 Pyodide 和 Service Worker 运行 Python ASGI 应用

AI 导读

作者展示了如何在浏览器中通过 Pyodide 和 Service Worker 运行 Python ASGI 应用。此前的 Datasette Lite 使用 Web Workers,但无法执行 `<script>` 标签中的 JavaScript。新方案由 Claude Opus 4.8 协助完成开发,解决了这一问题。作者已展示了基础的 ASGI FastCGI 演示和运行 Datasette 1.0a31 的演示,并计划后续将此方法应用于升级 Datasette Lite。

推荐理由

Simon Willison 用 Service Worker 让 Python ASGI 在浏览器里真正跑了起来,这个技巧补上了 Datasette Lite 长期缺的 JS 执行能力,搞 Pyodide 的值得看看。

正文

Research Running 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.

来源:Simon Willison 博客 · simonwillison.net