Bokeh Overview
Bokeh is a Python library for creating interactive visualizations that run in modern web browsers. Unlike matplotlib, which produces static images, Bokeh renders plots as JavaScript and HTML, giving every figure built-in pan, zoom, hover, and selection behaviour. The word "Bokeh" refers to the aesthetic blur found in photography, but the library itself is entirely about clarity: making dense, multivariate data easy to explore.
What Makes Bokeh Different
Bokeh is distinctive on three fronts. First, it is browser-native: a plot is a JSON document describing glyphs, data sources, and tools, which the BokehJS client library renders on canvas. Second, it is server-capable: the Bokeh Server keeps Python and the browser synchronized, so widgets, sliders, and selections can drive live Python computations. Third, it offers large-data support: through WebGL rendering and server-side downsampling, Bokeh handles datasets with hundreds of thousands of points far more gracefully than CPU-bound plotting libraries.
Two APIs
Bokeh exposes two layers. The low-level bokeh.models interface lets you compose plots from individual glyph objects, datasources, and renderers. The high-level bokeh.plotting interface, which most developers use, wraps those primitives:
from bokeh.plotting import figure, show
p = figure(width=400, height=300, title="Quarterly Revenue")
p.line([1, 2, 3, 4], [12, 18, 14, 22], line_width=2)
show(p)
The figure() constructor creates a model that knows how to draw, and each convenience method such as p.line() or p.circle() adds a glyph renderer with sensible defaults.
Core Architecture
A Bokeh document is a tree of models. At the top sits a Document; its root is typically a layout such as row, column, or gridplot. Inside a plot you will find axes, grids, ranges, glyphs, and tools, each serializable to JSON:
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
source = ColumnDataSource(data=dict(x=[1, 2, 3], y=[4, 5, 6]))
p = figure(width=350, height=250)
p.circle(x="x", y="y", source=source, size=10)
print(len(p.renderers)) # one renderer per glyph
Output Options
Bokeh can deliver content in several ways: an interactive standalone HTML file, an inline script embedded in a template, a Flask or Django component through bokeh.embed, or a live application served by bokeh serve. The same Python plot code works across all targets, which means a prototype notebook cell can become a production dashboard without rewriting.
When to Use Bokeh
- Interactive exploration with linked brushing, hover inspection, and zoom
- Dashboards that must update as data changes
- Scatter plots and time series with tens or hundreds of thousands of points
- Python-driven web apps deployed without writing JavaScript
Summary
Bokeh bridges Python analysis and browser interactivity: you describe plots declaratively in Python, and BokehJS renders them as rich, tool-bearing HTML canvases, with the Bokeh Server available whenever Python must respond to user input.