Using the sqlite3 Python module in Pyodide - Python WebAssembly

Pyodide provides "Python with the scientific stack, compiled to WebAssembly" - it's an incredible project which lets you run a full working Jupyter notebook, complete with complex packages such as numpy and pandas, entirely in your browser without any server-side Python component running at all.

It turns out it also now includes a working version of the standard library sqlite3 module, by bundling a WebAssembly compiled version of SQLite!

Trying this in the REPL

pyodide.org/en/stable/console.html provides an interactive REPL for trying eut Pyodide. You can run a one-liner to demonstrate the available SQLite version like this:

Welcome to the Pyodide terminal emulator 🐍
Python 3.9.5 (default, Sep 16 2021 11:22:45) on WebAssembly VM
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.connect(":memory:").execute("select sqlite_version()").fetchall()
[('3.27.2',)]

Querying an existing database file from JupyterLite

JupyterLite is "a JupyterLab distribution that runs entirely in the web browser, backed by in-browser language kernels."

Their online demo is at jupyterlite.github.io/demo/lab/index.html. I opened that demo and created a new Pyolite notebook there, then used the bridge to the JavaScript fetch() function to download the 11MB power plants database file from this URL:

https://global-power-plants.datasettes.com/global-power-plants.db

(Downloading this via fetch() works because Datasette includes CORS headers for these files.)

from js import fetch

res = await fetch("https://global-power-plants.datasettes.com/global-power-plants.db")
buffer = await res.arrayBuffer()

# Now write that to the in-memory simulated filesystem:
open("tmp/power.db", "wb").write(bytes(buffer.valueOf().to_py()))

# And run some queries against it:
import sqlite3
c = sqlite3.connect("tmp/power.db")
c.execute('select * from "global-power-plants" limit 10').fetchall()

This works!

Screenshot of JupyterLite running my example code

Created 2021-10-18T11:04:21-07:00, updated 2021-10-18T11:40:37-07:00 · History · Edit