Tech With Galvan
Back to Articles
DevelopmentJun 13, 2027 • 4 min read

Streamlit + Bokeh: Interactive Visualizations

Build interactive Bokeh charts in Streamlit — hover, zoom, pan, linked brushing, and streaming. When to use Bokeh vs Plotly vs Altair.

Galvan
Galvan

Founder & Creator

Introduction

Bokeh is a Python library for interactive browser-based visualizations — hover, zoom, pan, linked brushing, and real-time streaming. Streamlit renders Bokeh charts with st.bokeh_chart. This post covers the patterns that matter.

Setup

pip install streamlit bokeh pandas numpy

Step 1: Basic line chart

import streamlit as st
from bokeh.plotting import figure
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

p = figure(title="Sine Wave", x_axis_label="x", y_axis_label="y", width=700, height=400)
p.line(x, y, legend_label="sin(x)", line_width=2, color="#3b82f6")

st.bokeh_chart(p, use_container_width=True)

Step 2: Scatter with hover

from bokeh.models import HoverTool

p = figure(title="Scatter", width=700, height=400)
p.scatter("x", "y", size=8, color="category", source=data, legend_field="category", alpha=0.7)

hover = HoverTool(tooltips=[("X", "@x"), ("Y", "@y"), ("Category", "@category")])
p.add_tools(hover)
st.bokeh_chart(p, use_container_width=True)

Step 3: Bar chart

from bokeh.transform import factor_cmap
from bokeh.palettes import Spectral6

fruits = ["Apples", "Pears", "Nectarines", "Plums", "Grapes", "Strawberries"]
counts = [5, 3, 4, 2, 4, 6]

p = figure(x_range=fruits, height=400, title="Fruit Counts")
p.vbar(x=fruits, top=counts, width=0.9, color=factor_cmap("x", palette=Spectral6, factors=fruits))
p.xgrid.grid_line_color = None
p.y_range.start = 0
st.bokeh_chart(p, use_container_width=True)

Step 4: Linked brushing

from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource

source = ColumnDataSource(data)
p1 = figure(width=400, height=300, title="Plot 1")
p1.circle("x", "y", source=source, size=8, color="navy", alpha=0.5)

p2 = figure(width=400, height=300, title="Plot 2")
p2.circle("x", "z", source=source, size=8, color="firebrick", alpha=0.5)
p1.x_range = p2.x_range

st.bokeh_chart(gridplot([[p1, p2]]), use_container_width=True)

Selecting points in one plot highlights them in the other.

Step 5: Real-time streaming

from bokeh.models import ColumnDataSource
import time

source = ColumnDataSource(data=dict(x=[], y=[]))
p = figure(width=700, height=400, x_range=(0, 100))
p.line("x", "y", source=source)

placeholder = st.empty()
for i in range(100):
    source.stream({"x": [i], "y": [np.sin(i * 0.1)]}, rollover=50)
    placeholder.bokeh_chart(p, use_container_width=True)
    time.sleep(0.1)

source.stream() appends data with rollover limiting buffer size.

Step 6: JS callbacks (no server round-trip)

from bokeh.models import Slider, CustomJS

slider = Slider(start=0, end=10, value=1, step=0.1, title="Frequency")
p = figure(width=700, height=400)
r = p.line(x, np.sin(x), line_width=2)

callback = CustomJS(args=dict(line=r, slider=slider), code="""
    const freq = slider.value;
    const data = line.data_source.data;
    for (let i = 0; i < data.x.length; i++) data.y[i] = Math.sin(data.x[i] * freq);
    line.data_source.change.emit();
""")
slider.js_on_change("value", callback)

st.bokeh_chart(slider, use_container_width=True)
st.bokeh_chart(p, use_container_width=True)

JS callbacks run in the browser — instant updates without server round-trips.

Bokeh vs Plotly vs Altair vs Matplotlib

Bokeh Plotly Altair Matplotlib
Interactivity High (JS) High Medium None
Linked brushing Yes No Yes No
Streaming Yes Limited No No
Learning curve Medium Medium Medium Low
Best for Advanced interactivity Dashboards Statistical viz Publications

Common mistakes

  • Not using use_container_width=True — charts won’t resize.
  • Too many glyphs — 10k+ points lag. Use ColumnDataSource with sampling.
  • Forgetting p.xgrid.grid_line_color = None — default grid is noisy.
  • Mixing Bokeh versions — 3.x has breaking changes from 2.x. Pin your version.
  • Not closing figures — Bokeh figures are lightweight; no cleanup needed, but avoid creating them in loops without reusing.

Key Concepts

  • Bokeh = browser-based interactive visualization (Leaflet for charts)
  • figure() = create a plot
  • ColumnDataSource = shared data for linked plots
  • HoverTool = tooltips on hover
  • stream() = append data for real-time
  • CustomJS = client-side callbacks (no server round-trip)

What to Try Next

FAQ

Bokeh vs Plotly — which is better?

Bokeh = more control over interactivity (linked brushing, streaming, JS callbacks). Plotly = easier to use, more chart types, better for dashboards. Use Bokeh for advanced interactivity; Plotly for quick dashboards.

Can Bokeh handle 100k+ points?

Not smoothly in the browser. For large datasets, use Datashader (renders to image) or switch to server-side rendering. Bokeh is best for <10k points.

Is Bokeh free?

Yes — open source (BSD license). Rendering happens client-side (BokehJS), no server cost. Works offline.