跳到正文
Simon Willison's Weblog·· 2 小时前精选AI 评分54

Datasette Lite 用 service worker 重写,可在浏览器运行完整 Datasette 应用

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

AI 导读

Simon Willison 发布 Datasette Lite 的完整重写版,改用 service worker 在浏览器中运行 Datasette,称其与真实服务端应用几乎无差别。

变化判断

Datasette Lite 改用 service worker 重写后,能在浏览器里跑完整服务端应用,开发者用一条 URL 就能固定版本、插件和 SQLite 版本复现问题,不必再起服务器。

正文 · AI 翻译

2026年11月17日

Datasette Lite 是我把 Datasette 这个 Python 应用移植到浏览器里、借助 WebAssembly 和 Pyodide 直接运行的版本。我用 service worker 对 Datasette Lite 做了彻底重写并已发布,现在它和真正的服务端应用几乎没什么区别了。

现在,只要构造一个精心设计的 URL 指向 lite.datasette.io,你就能做到下面这些事:

  • 加载客户端版 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 在运行新的 datasette-maplibre 插件:它加载了一个包含各国 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 可寻址的 Pyodide Python 环境》一文中,更详细地写过这种构建 URL 可寻址软件的技巧。

支持的完整 URL 参数列表在 Datasette Lite 的 README 中有说明。

我已经开始在 Datasette 的开发流程中使用 Datasette Lite 了。下面是最近的一个 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——点下一页,第二页是空的。

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

实现原理:借助 Service Worker

Datasette Lite 并不是新项目。第一版早在 2022 年就发布了,最初多少带点玩笑性质(在浏览器里跑服务端应用确实挺好玩),但很快就证明了自己的价值。

初版的问题在于它只是个权宜之计。它的做法是在 Pyodide 的 Web Worker 里运行 Datasette,通过这套栈模拟请求,再用 innerHTML 把生成的 HTML 注入顶层页面。

这对基础版 Datasette 管用,但任何用到 JavaScript 的功能都会失效:innerHTML 里的 script 标签不会执行,而它所用的 Web Worker 技巧也无法加载额外的 JavaScript 或 CSS 文件。

新版本采用了完全不同的思路,基于 Service Worker。它让 JavaScript 能够拦截对新页面和资源的 HTTP 请求,改由客户端代码来响应。

Datasette Lite 为整个 lite.datasette.io 域注册了一个 Service Worker。父页面随后创建一个 iframe,向形如下面这样的 URL 发送 Datasette 页面请求:

/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 配置了什么插件或其他设置,所有标签页都会共用那一个底层实例。

所以父页面会生成一个随机的会话 ID,把它作为前缀,用于后续所有隐藏的资源和页面加载。Service Worker 再用这个会话 ID,把这些请求经由父页面路由到为该会话专门运行 Datasette 实例的 Web Worker。

(我们仍然需要 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.

这套系统早在今年 5 月就作为一次AI 研究性探索起步,由 Claude Opus 4.8 生成。直到 11 月(以及 Claude Fable 5.2),我才对它的运作方式熟悉到有把握把它正式上线!

附加功能:自选 SQLite 版本

这个功能是我昨晚加的,起因是在调查 SQLite 的一个潜在 bug。

Datasette 旨在兼容自 3.25(2018 年 9 月发布)以来的任何已发布 SQLite 版本。此后 SQLite 又发布了 87 个版本。

能够针对特定版本的 SQLite 启动 Datasette,对演示 SQLite 新特性和排查疑难问题都非常有用。

过去我在 Mac 上为了让不同版本的 SQLite 配合 Python 运行,用过各种办法,但过程并不轻松。

WebAssembly 正好适合这个场景。我可以完全在浏览器里加载自定义的 SQLite 构建,就地测试,关掉标签页就丢弃。

datasette/pysqlite3-wasm-wheels 是我的新仓库,里面存放了针对 163 个不同 SQLite 版本编译的 pysqlite3 WebAssembly Python wheel。每个版本对应一个文件名,像这样:

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 worker 在浏览器里跑完整的服务端 Datasette Python 应用

来源:Simon Willison's Weblog · simonwillison.net