Extending Bokeh and WebGL
Bokeh is extensible in two directions: custom Python models (new renderers, tools, and layouts built from the model API) and lower-level rendering via WebGL for very large datasets. This chapter covers the extension model and where WebGL fits for performance.
Building Custom Models
Any Bokeh behavior is a model with properties; you subclass and register new ones. A minimal custom renderer shows the pattern:
from bokeh.core.has_props import abstract
from bokeh.models import Model, FloatParam, BoolParam
class MyGlowConfig(Model):
'''Example custom model with typed properties.'''
radius = FloatParam(default=10)
enabled = BoolParam(default=True)
color = StringParam(default="gold")
For a full custom glyph you subclass the glyph base classes, define its data fields, and provide the corresponding JS renderer (Bokeh renderers are Python plus a JS twin). This is how Bokeh itself ships its glyphs.
A Custom Tool
from bokeh.models import Tool
class MeasureTool(Tool):
'''Sketch of a custom tool: define properties, wire JS behavior.'''
# tools also require a JS implementation for client-side interaction
...
WebGL for Large Data
from bokeh.plotting import figure
# canvas/WebGL-backed glyphs for hundreds of thousands of points
p = figure()
p.scatter(x, y, size=2, alpha=0.6) # rendered efficiently on the GPU
# for 100k+ points prefer:
p.glyph = CanvasRenderer(...) # or use the canvas-based figure options
Guidance: SVG rendering (the default) is sharp and fine up to roughly 50k points; beyond that, switch to canvas/WebGL-backed rendering, downsample, or use the Bokeh server with a WebGL-friendly glyph to keep the browser fast.
Bokeh Extensions Ecosystem
- bokeh-plotly and similar bridges let you embed other engines' output.
- Third-party renderers and model packages follow the same "Python model + JS" contract.
- Keep custom JS in a dedicated file registered with the model, so the Python side stays declarative.
Choosing Your Extension Path
- Can you do it with existing models + callbacks? Do that first - least maintenance.
- Need a new data-driven visual? Build a custom glyph (Python model + JS renderer).
- Need new interaction? Extend a Tool with a JS behavior.
- Need more speed? Move to canvas/WebGL rendering or server-side downsampling.
Summary
Extend Bokeh by building custom models (typed properties, Python plus a JS twin for renderers and tools) and by switching to canvas/WebGL-backed rendering when point counts outgrow SVG. Start with existing models and callbacks, add custom glyphs only when the visual is new, and use WebGL/server-side downsampling for very large datasets.