Back to Perspective

DuckDB Virtual Server

docs/md/how_to/python/virtual_server/duckdb.md

5.2.01.9 KB
Original Source

DuckDB Virtual Server

Perspective provides a built-in virtual server for DuckDB, allowing <perspective-viewer> clients to query a server-side DuckDB database over WebSocket.

For browser-only usage via DuckDB-WASM, see the JavaScript DuckDB guide.

Installation

bash
pip install perspective-python duckdb

Usage

Create a server that exposes a DuckDB database to browser clients:

python
import duckdb
import tornado.web
import tornado.ioloop
from perspective.virtual_servers.duckdb import DuckDBVirtualServer
from perspective.handlers.tornado import PerspectiveTornadoHandler

# Create DuckDB connection and load data
conn = duckdb.connect()
conn.execute("CREATE TABLE my_table AS SELECT * FROM 'data.parquet'")

# Create virtual server backed by DuckDB
server = DuckDBVirtualServer(conn)

# Serve over WebSocket
app = tornado.web.Application([
    (r"/websocket", PerspectiveTornadoHandler, {"perspective_server": server}),
])

app.listen(8080)
tornado.ioloop.IOLoop.current().start()

Connect from the browser:

javascript
const websocket = await perspective.websocket("ws://localhost:8080/websocket");
const table = await websocket.open_table("my_table");
document.getElementById("viewer").load(table);

Window functions

Window columns are DuckDB's own functions, under their DuckDB names — the advertised name is emitted into the OVER clause verbatim.

Aggregatingsum avg count min max product median
Deviation / variancestddev_samp stddev_pop var_samp var_pop
Navigationfirst_value last_value nth_value lag lead
Rankingrow_number rank dense_rank percent_rank cume_dist ntile
Perspective's owndiff rate

Examples