Architecture

How a Pyodide app in the browser ends up holding a real database cursor.

A PyCorner app is a real Streamlit script running in your browser (via Pyodide-in-stlite). Browsers can’t open arbitrary TCP sockets or stash secret API keys safely, so we put a thin server-side layer between Pyodide and the outside world. The whole point is that your application code doesn’t see this layer — it’s import pycorner; api.get(...) and everything Just Works.

The four moving parts

  1. The editor + preview (your browser tab). Standard Streamlit script, running on Pyodide.
  2. The PyCorner backend (Django on app.pycorner.dev). Holds Resource definitions, mints scoped tokens, executes server-side drivers, proxies HTTP.
  3. The tunnel agent (optional, runs on your network). A small Python script that holds a persistent WebSocket back to PyCorner. It’s the only way for PyCorner to reach a target that isn’t internet-accessible, and the only way to make outbound calls from your IP instead of ours.
  4. The pycorner client lib (auto-injected into your Stlite filesystem). Provides connect(), api(), and tunnel.install().

Three resource kinds

There are three modes you can use. They share a common URL scheme (/api/v1/data/x/...) and the same auth token, but they differ in where the work happens.

Mode 1 — Database

Pyodide  →  https POST /x/db/<name>/query  →  backend driver (pg8000/pymysql/...)
                                              ↓
                                            Postgres / MySQL

Your app calls pycorner.connect("name").execute(sql). The driver runs on PyCorner’s backend; results come back as JSON. Optional agent_id in the resource config routes the driver’s socket through your tunnel agent — used when the DB isn’t internet-reachable.

Mode 2 — API proxy

Pyodide  →  https <METHOD> /x/api/<name>/<path>  →  backend  →  upstream URL
                                                    + injected headers

The backend forwards the request to the upstream you configured, splicing in any upstream_headers you set (Authorization tokens, API keys, …). Your app never sees the secret. On Pro and Team the resource can be configured with an agent_id so the request egresses from your IP rather than ours — useful for fixed-IP allowlists or APIs behind your firewall.

Mode 3 — Raw TCP tunnel

Pyodide  →  wss /ws/x/tunnel/<name>/  →  daphne  →  agent  →  127.0.0.1:5432
   ↑              raw bytes both ways                      (or any TCP target
   socket monkey-patched to talk over the WS               on the agent's network)

pycorner.tunnel.install() patches socket.create_connection so pg8000.connect(host="proddb", …) transparently routes through a WebSocket-as-socket bridge to your tunnel agent. The agent opens a TCP socket to the configured target and pipes raw bytes both ways. Browser-resident drivers (pg8000, pymysql, plus Redis/etc. with the right pure-Python lib) work natively.

This is the “advanced” path: you get a real socket experience but it only works for libraries available in Pyodide. Mode 1 is the default for DB workloads precisely because it doesn’t have that constraint.

Auth — scoped preview tokens

When the editor mounts a preview, it calls POST /api/v1/projects/<uuid>/preview-token/ to mint a short-lived JWT scoped to (owner, project). The editor injects this token as a virtual file /_pycorner/token inside the Pyodide filesystem; the client library reads it on import and signs every backend call with it.

Token claims include:

Resource lookup is always scoped: Resource.resolve(owner, project, name) first checks for a project-scoped match, falls back to a user-scoped (project=NULL) match. Two users with the same resource name don’t collide. Two projects of the same user collide only if both define a project-scoped resource with the same name.

Where state lives

WhereWhat
PyCorner databaseResources, tunnel agent registrations, projects, users
PyCorner daphne processThe active WebSocket of each connected agent (in-memory)
Your tunnel agent (on-prem)The open TCP sessions to your local services
Your browser (the editor)User JWT, project files, preview token
Pyodide / stlite workerThe running app, the pycorner client lib, the preview token as a file

Nothing about your apps’ state is persisted in the editor’s Pyodide filesystem — every Save remounts. Connections (DB cursors, tunnel sockets) live only for the duration of one preview run.

Networking — what nginx sees

Two upstreams matter:

The editor route serves the SPA with COOP same-origin + COEP require-corp, enabling SharedArrayBuffer in the stlite worker. That’s what makes pyodide.ffi.run_sync work, which in turn is what lets the synchronous socket.recv(n) of pg8000 block on async WebSocket messages.