跳到正文
原文
Simon Willison·· 11 天前AI 评分58

Datasette Lite 用 Service Worker 和 Pyodide 在浏览器运行完整服务端应用

Running the full server-side Datasette Python app in the browser with service workers and Pyodide

AI 导读

Datasette Lite 完成基于 Service Worker 与 Pyodide 的重写,可在浏览器里运行几乎与服务端应用无异的 Datasette。

正文 · AI 翻译

17th November 2026

Datasette Lite 是我将 Datasette Python 应用移植为借助 WebAssembly 和 Pyodide 直接在浏览器中运行的版本。我已发布使用 service workers 对 Datasette Lite 的完整重写,现在它几乎与真正的服务端应用无法区分。

现在,你可以使用精心构造的、指向 lite.datasette.io 的 URL 完成以下所有操作:

  • 加载客户端 Datasette,以浏览任意 SQLite 数据库、JSON 文件、CSV 文件或 SQL 脚本
  • 选择要从 PyPI 加载的已发布 Datasette 版本
  • 从 Datasette 的 GitHub 仓库加载特定的 commit、tag 或分支名
  • 从 100 多个 SQLite 版本中挑选,供 Datasette 使用
  • 从 PyPI 或指向 wheel 文件的 URL 安装额外的 Datasette 插件,包括 JavaScript 可视化工具

能够加载特定的 Datasette 引用、插件和 SQLite 版本,使它成为链接到开发中功能演示或 bug 复现的绝佳工具,而无需启动完整的 Web 服务器来提供该演示。

举个例子,看看下面这个 URL:

https://lite.datasette.io/?install=datasette-maplibre&url=https://gist.githubusercontent.com/simonw/39fa5d399465766039549912e2b3d291/raw/f6dfba99a75cbb8b5d9386223220eb52272767ee/demo.db&metadata=https://gist.githubusercontent.com/simonw/39fa5d399465766039549912e2b3d291/raw/f6dfba99a75cbb8b5d9386223220eb52272767ee/metadata.yml#/demo/capitals_and_countries%23map=0/4.2/180&color=continent

打开该链接,经过短暂的加载画面后,你就会看到这样一个页面:

Screenshot of a web map interface. Toolbar: "View" set to "Clusters", "Colour by" set to "continent", plus "Filter to this area" and "Download GeoJSON" buttons. A world map shows countries outlined and shaded by continent, with coloured dots and numbered cluster circles. A popup for a point in South America reads: capital Caracas, country Venezuela, continent South America, capital_population 2985000, latitude 10.502944, longitude -66.918983. Legend: Africa (52), Asia (45), Europe (39), North America (16), South America (13), Oceania (6). Text: "All 171 rows are on the map". Below, a table with columns country, continent, capital_population, latitude, longitude, boundary. First row: Afghanistan, Asia, 3277000, 34.518636, 69.181314, with a small map in the boundary column showing Afghanistan outlined and shaded in blue.

这是运行新插件 datasette-maplibre 的 Datasette,它加载了一个包含各国 GeoJSON 形状的数据库,并同时显示一张合并地图以及表格里的各个边界地图。

我们来拆解这个 URL,看看它做了什么:

https://lite.datasette.io/
  ?install=datasette-maplibre
  &url=https://.../demo.db
  &metadata=https://.../metadata.yml
  #/demo/capitals_and_countries%23map=0/4.2/180&color=continent

?install=datasette-maplibre 会从 PyPI 安装该额外插件。你可以随意多次传入 &install=...,以加载更多插件。

&url=https://.../demo.db 会从所提供的 URL 加载该 SQLite 数据库。这些 URL 需要带有 开放的 CORS 标头——我把这类文件托管在 Gists 里,因为它们能方便地让网络托管的文件带上开放的 CORS 标头。

&metadata=https://.../metadata.yml 会加载 这个 metadata.yml 文件。该元数据文件为各个表以及 Datasette 主页添加 HTML 描述。访问 这里的主页,可导览该插件的其他功能,这些功能全部定义在元数据文件中。

#/demo/capitals_and_countries%23map=0/4.2/180&color=continent 是一个片段哈希,会变成 Datasette 内部的某个位置——这里它加载的是 /demo/capitals_and_countries SQL 视图,然后再向其追加另一个片段哈希(%23 是经过 URL 编码的 #),告诉地图插件缩放到某一位置,并根据数据中的 continent 列设置地图上形状的颜色。

这个 URL 做了不少工作!关于构建可通过 URL 寻址的软件的这一技巧,我去年在 可通过 URL 寻址的 Pyodide Python 环境 里写得更详细。

受支持的全部 URL 参数在 Datasette Lite README 中有说明。

我已经开始把 Datasette Lite 用作 Datasette 开发流程的一部分。这里有 一个近期的 issue,描述了一个分页 bug——我在那里发了一条评论,用指向 Datasette Lite 的链接演示了旧的错误行为以及新的修复:

该 bug 的演示:https://lite.datasette.io/?ref=1.0a42&sql=https://gist.github.com/simonw/75ececf61d50e6e8d50f0c150150ee81#/data/scores?_size=2&_sort=score——点击 next,第二页是空的。

下面是修复该 bug 后的同一页面(运行于 1.0a43):https://lite.datasette.io/?ref=1.0a43&sql=https://gist.github.com/simonw/75ececf61d50e6e8d50f0c150150ee81#/data/scores?_size=2&_sort=score

其工作原理:使用 service workers

Datasette Lite 并不是一个新项目。我早在 2022 年就发布了第一个版本,最初多少像个玩笑(在浏览器里运行服务端应用挺好笑的),但它很快就开始证明自己的价值。

那个初始版本的问题在于它是一种hack。它的做法是在 Pyodide web worker 中运行 Datasette,通过这一整套技术栈模拟请求,然后使用 innerHTML 将生成的 HTML 注入回顶层页面。

这对基本的 Datasette 是可行的,但意味着任何使用 JavaScript 的功能都无法工作,因为 innerHTML 中的 script 标签不会执行,而且它所采用的 web worker 技巧也无法加载额外的 JavaScript 或 CSS 文件。

新版本采用了基于 Service Workers 的完全不同的方法。它们允许 JavaScript 拦截针对新页面和资源的 HTTP 请求,并改为由客户端代码来提供这些内容。

Datasette Lite 会为整个 lite.datasette.io 域名注册一个 service worker。父页面随后创建一个 iframe,将 Datasette 页面的请求发送到如下形式的 URL:

/app/aa94e6851595/ # the homepage (HTML)
/app/aa94e6851595/data/places # a table page (HTML)
/app/aa94e6851595/-/static/navigation-search.js # (JavaScript)
/app/aa94e6851595/-/static/app.css # (CSS)

这些是常规的 Datasette URL,但带有 /app/aa94e6851595/ 前缀。这是怎么回事?

之所以需要 /app/<session>/ 前缀,是因为存在多个标签页。

以上面的 URL 为例,它会加载带有 datasette-maplibre 插件的演示实例。该 URL 定义了一个完整的自定义 Datasette 实例。你可能在一个标签页中运行这个实例,同时在另一个标签页中运行我的其他某个演示链接。

lite.datasette.io 的 service worker 在所有这些标签页之间共享。如果它把请求路由到单个 Datasette 实例,那么每个标签页都会共享那个底层实例,无论它们的 URL 试图配置哪些插件或其他设置。

因此,父页面会生成一个随机的 session ID,并将其用作后续所有隐藏资源与页面加载的前缀。service worker 随后可以使用该 session ID,将这些请求经由父页面路由回一个 web worker,由它为该特定会话运行一个专用的 Datasette 实例。

(我们仍然需要 web worker,因为 service worker 中的 Pyodide 无法将状态维持超过大约 30 秒,具体取决于浏览器。这里牵涉的环节很多!)

我让 Claude Fable 5.2 生成了几张 SVG 示意图来帮助解释这一点。下面是其中我最喜欢的一张,现已嵌入 Datasette Lite 的 README:

Architecture diagram titled "What happens when you click a link in Datasette Lite", with a legend showing a solid arrow for "request" and a dashed arrow for "response". A blue box labeled "index.html: the page you opened - Each tab picks a random session and starts one Web Worker." contains two white boxes: "iframe Datasette pages at /app/session/…" and "shell.js the page's own script: relays". Below it is a yellow box labeled "sw.js: the service worker - Intercepts requests for /app/session/…." To the right is a green box marked 4 and labeled "webworker.js: a Web Worker - Pyodide: Python in WebAssembly" containing three stacked white boxes: "ASGI bridge + cookie jar", "Datasette ASGI app" and "SQLite databases". Numbered arrows connect them: solid arrow 1 from the iframe down to sw.js, solid arrow 2 from sw.js up to shell.js, solid arrow 3 from shell.js across to the Web Worker, dashed arrow 5 from the Web Worker back to shell.js, dashed arrow 6 from shell.js down to sw.js, and dashed arrow 7 from sw.js up to the iframe. A numbered key at the bottom reads: 1 The page requests /app/session/fixtures. 2 sw.js intercepts it and posts it to the owning tab. 3 shell.js forwards it to the Web Worker. 4 Python adds cookies and calls Datasette, which queries SQLite. 5 The status, headers and body return to the page, 6 then to sw.js over a MessagePort, 7 which hands the browser a Response. The iframe renders it.

这个系统始于五月,最初是使用 Claude Opus 4.8 生成的一次AI 研究 spike。直到十一月(以及 Claude Fable 5.2),我才觉得自己对它的全部工作原理足够熟悉,可以有信心地将其发布到生产环境!

额外功能:选择你的 SQLite 版本

我昨晚添加了这个功能,它是在调查SQLite 中的一个潜在 bug时衍生出来的。

Datasette 旨在可运行于自 3.25(2018 年 9 月发布)以来的任何已发布 SQLite 版本。自那以后又发布了 87 个版本。

能够用特定版本的 SQLite 来启动 Datasette,对于演示新的 SQLite 功能以及调试奇怪的问题非常有用。

过去我曾用过各种技巧,让不同版本的 SQLite 在我的 Mac 上与 Python 一起运行,但这并不是一个简单的过程。

WebAssembly 非常适合做这件事。我可以完全在浏览器中加载自定义的 SQLite 构建,在那里测试它,然后在关闭标签页时将其丢弃。

datasette/pysqlite3-wasm-wheels 是我的新仓库,其中包含为 pysqlite3 编译的 WebAssembly Python wheel,涵盖 163 个不同的 SQLite 版本。每个版本都会得到一个类似这样命名的文件:

pysqlite3-0.6.2-3.54.1-cp314-cp314-pyemscripten_2026_0_wasm32.whl

这是给 SQLite 3.54.1 的,也就是最新版本。

Datasette Lite 现在接受一个特殊的 ?sqlite= URL 参数,它会接受类似 3.54.1 的版本号,解析出对应 wheel 文件的路径,然后获取该 wheel 并将其安装到 Pyodide 环境中,再配置 Datasette 使用它。

这些 wheel 构建好之后,我决定让 Claude Fable 5.2 为我构建[这个附加工具](https://datasette.github.io/pysqlite3-wasm-wheels/?versions=3.54.1,3.54.0&sql=select+json_object(%27a%27%2C+(select+json_array(1%29%29%29),用于在任意选定的 SQLite 版本之间比较一条 SQL 查询。下面这个例子展示了 select json_object('a', (select json_array(1))) 在 SQLite 3.54.0 和 3.54.1 之间的差异:

Screenshot of a web tool for comparing SQL across SQLite versions. Heading "SQLite versions (163 available)" sits above a filter text input with placeholder "Filter, e.g. 3.4", a "Clear selection" button and a blue "Start REPL with 2 versions" button. Below is a scrollable grid of pill-shaped checkboxes, one per version, running from 3.54.1 down past 3.30.0, with 3.54.1 and 3.54.0 checked. A "REPL" heading follows, with two green status badges reading "3.54.1 ready" and "3.54.0 ready", then a textarea containing "select json_object('a', (select json_array(1)))" beside a blue "Run" button, and a row of clickable example queries labeled "Examples:". The output section shows the prompt "sqlite> select json_object('a', (select json_array(1)))" followed by an orange warning "Versions differ: 2 different results" and two side-by-side yellow result panels, each with the query as its column header. The "SQLite 3.54.1" panel returns a JSON object where key "a" has a string value: the array containing 1, wrapped in double quotes. The "SQLite 3.54.0" panel returns a JSON object where key "a" has an actual array containing 1, with no quotes around it.

这应该也适用于其他所有 Python 应用

Datasette Lite 的工作方式并没有任何 Datasette 独有之处。几乎任何 Python 应用都应该能用上同样的办法——借助 WebAssembly 和 Pyodide 完全在浏览器中运行,作为一种低成本的方式来提供预览环境、演示功能以及复现 bug。

C 和 Rust 扩展曾经是这类做法的阻碍,但现在任何优秀的编程智能体都能把它们编译成兼容 Pyodide 的 WebAssembly。截至 2026 年 4 月的 PEP 783,你可以甚至将它们发布到 PyPI。

一个问题是数据库。SQLite 完全没有问题,但在 WebAssembly 中运行 PostgreSQL 或 MySQL 虽然可行,仍是一条少有人走的路。

我希望看到像这样的 WebAssembly 预览环境成为整个 Python 生态系统中的常见模式。

给 Hacker News:借助 service workers 在浏览器中运行完整的服务端 Datasette Python 应用

来源:Simon Willison · simonwillison.net