Introduction to WebGPU from Python#

The webgpu package wraps the WebGPU API in a small rendering framework so you can write interactive GPU graphics directly from Python / Jupyter.

Useful links

First example#

The package ships renderer classes for common geometry. Draw creates a WebGPU canvas and renders everything you pass to it.

[1]:
from webgpu.jupyter import Draw
from webgpu.triangles import TriangulationRenderer

points = [(0, 0, 0), (0, 1, 0), (1, 0, 0)]
Draw(TriangulationRenderer(points, color=(1, 0, 0, 1)))
▶ Click to interact
[1]:

Core concepts#

Concept

Role

Renderer

Owns shader code, vertex data, and GPU bindings

Scene

Holds one or more renderers plus a Camera and Light

Draw

Creates a Scene, attaches a Canvas, and displays it

Camera, light, and canvas are set up automatically by Draw. You only need to provide Renderer objects.

Multiple renderers in one scene#

Pass a list to Draw to compose several renderers into one scene.

[2]:
from webgpu.jupyter import Draw
from webgpu.triangles import TriangulationRenderer

tri1 = TriangulationRenderer([(0, 0, 0), (0, 1, 0), (1, 0, 0)], color=(1, 0, 0, 1))
tri2 = TriangulationRenderer([(1, 0, 0), (1, 1, 0), (2, 0, 0)], color=(0, 0, 1, 1))
Draw([tri1, tri2])
▶ Click to interact
[2]: