Skip to departures
Hpjadul
MAC END

Datasette Lite Runs a Full Python App in the Browser with Service Workers

Departs
Crew
John Williams
Passengers
657

A service worker rewrite brings Datasette’s pages, JavaScript plugins and version-specific SQLite builds into browser-based demos.

Datasette Lite Runs a Full Python App in the Browser with Service Workers

Datasette Lite now runs the Datasette Python application in a browser with pages and JavaScript plugins working much like they do on a server. The rewrite uses service workers to route browser requests to a dedicated Pyodide environment, making it possible to share configured demos through URLs without setting up a web server.

The practical value is reproducibility. A link can specify a Datasette release or Git reference, load a database or other data file, install plugins, and select from more than 100 SQLite versions. That gives developers a compact way to reproduce bugs or show features with a particular combination of code and dependencies.

A configured demo in one URL

One example loads a SQLite database of country boundaries and capital cities, adds the datasette-maplibre plugin, and applies metadata describing the tables and homepage. The resulting view combines an interactive map with a table of the underlying data. The map colors countries by continent, clusters points and can show individual boundary maps.

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.

Each part of the URL configures a different layer of the demo. Repeating the install parameter can add multiple plugins from PyPI. The url parameter points to the database, while metadata loads a YAML file that supplies descriptions and other settings. Files served from another host need open CORS headers so the browser can fetch them; the example uses files hosted as Gists.

The fragment after the hash opens a specific SQL view and passes options to the map plugin. In this case, it sets a map position and colors shapes according to the continent column. Encoding the second hash as %23 lets the URL carry both the Datasette page location and the map’s own settings. The supported parameters are documented in the Datasette Lite README.

That level of configuration also helps with software development. A shared link can pin a version of Datasette and a test dataset, so someone else can inspect a bug without installing the application. One example compares pagination on versions 1.0a42 and 1.0a43: the first link reproduces an empty second page, while the second shows the fix.

Why the rewrite uses a service worker

The first Datasette Lite, launched in 2022, ran Python in a Pyodide web worker and inserted generated HTML into the page. It handled basic browsing, but scripts inserted through innerHTML don’t execute. That blocked features that depend on JavaScript, and the earlier worker setup could not load extra JavaScript and CSS resources correctly.

The new design lets Datasette serve a normal set of pages and assets. A service worker registered for the Lite domain intercepts requests, including requests for HTML, JavaScript and CSS. Each browser tab opens an iframe for Datasette pages and starts a web worker with its own application instance.

Requests use a path prefix such as /app/aa94e6851595/, where the random session identifier distinguishes one tab’s application from another. Without that separation, the shared service worker could send requests from different tabs to the same Datasette instance, despite their URLs specifying different plugins or settings.

For each request, the service worker identifies the session and passes the work to the tab that owns it. The page’s script forwards the request to its web worker, where Python runs in Pyodide, adds cookies and calls the Datasette ASGI app. Datasette queries SQLite; the response then travels back through the worker and service worker to the iframe.

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.

Pyodide still runs in a web worker because a service worker may lose its state after about 30 seconds, depending on the browser. The service worker handles web requests, while the longer-lived worker keeps the Python environment available to process them. The approach grew out of an earlier browser experiment and took time to develop into a production version.

Testing different SQLite releases

Datasette is intended to work with SQLite releases from version 3.25 onward. To make version-specific checks easier, Datasette Lite now accepts a sqlite parameter. It resolves the requested version to a WebAssembly wheel, installs it in Pyodide and configures Datasette to use that build.

The pysqlite3-wasm-wheels repository contains builds for 163 SQLite versions. The latest cited wheel is for SQLite 3.54.1. A companion browser tool can run the same SQL query against selected versions and show where their results differ. In the provided example, a query using json_object and json_array returns different JSON values on SQLite 3.54.0 and 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.

That makes browser-based testing useful beyond Datasette’s own changes: developers can inspect how a query behaves across SQLite releases without changing their local Python setup, then close the tab to discard the environment.

A pattern for Python previews

The architecture could apply to other Python applications that can run under Pyodide. It offers a way to publish interactive previews, demonstrate a feature or package a bug reproduction in a link. Compiled extensions written in C or Rust have been a hurdle, though the article reports that they can now be built for Pyodide, with publication to PyPI supported under PEP 783.

Database choice remains a practical constraint. SQLite fits the browser workflow; PostgreSQL and MySQL can run in WebAssembly, but are less established in this setup. For Python projects that can work within those limits, Datasette Lite shows how a browser can host a useful, URL-configured test environment without a separate server.

Source: John Williams · simonwillison.net
Arrived · Hpjadul

Also departing from this gate