How to Fix Python Bokeh Server Not Updating
In this tutorial, you'll learn about How to Fix Python Bokeh Server Not Updating. We cover key concepts, practical examples, and best practices.
Bokeh server apps appear static when the data source is not properly linked to the plot, the document is not modified correctly, or the update callback does not run.
The Wrong Way
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
import random
source = ColumnDataSource(data={"x": [], "y": []})
p = figure()
p.circle("x", "y", source=source)
def update():
source.data["x"].append(random.random())
source.data["y"].append(random.random())
# Forgot to add periodic callback
curdoc().add_root(p)
The plot stays empty because update() is never called.
The Right Way
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.layouts import column
from bokeh.server.server import Server
import random
source = ColumnDataSource(data={"x": [], "y": []})
p = figure()
p.circle("x", "y", source=source)
def update():
new_x = random.random()
new_y = random.random()
source.stream({"x": [new_x], "y": [new_y]}, rollover=100)
curdoc().add_root(p)
curdoc().add_periodic_callback(update, 1000)
The plot updates every second with new points.
Prevention Tips
- Use
source.stream()to push data without replacing existing data. - Use
curdoc().add_periodic_callback()for timed updates. - Use
source.patch()for targeted updates to specific indices. - Use
curdoc().add_timeout_callback()for one-shot delayed updates. - Always modify
source.datathrough Bokeh's methods, not direct dict assignment.
Common Mistakes with bokeh server
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro