CARVIEW |
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!
Related
- webassembly Run Python code in a WebAssembly sandbox - 2023-02-02
- sqlite Building a specific version of SQLite with pysqlite on macOS/Linux - 2021-08-14
- deno Running Python code in a Pyodide sandbox via Deno - 2023-05-10
- pyodide Cryptography in Pyodide - 2023-11-26
- python Running PyPy on macOS using Homebrew - 2022-09-14
- sqlite Finding the SQLite version used by Web SQL in Chrome - 2022-10-28
- sqlite Compile and run a new SQLite version with the existing sqlite3 Python library on macOS - 2023-08-22
- sqlite Using LD_PRELOAD to run any version of SQLite with Python - 2020-06-17
- sqlite Trying out SQLite extensions on macOS - 2022-08-03
- sqlite Saving an in-memory SQLite database to a file in Python - 2023-04-08
Created 2021-10-18T11:04:21-07:00, updated 2021-10-18T11:40:37-07:00 · History · Edit