Skillversitas

Bokeh in Jupyter Notebook

Bokeh renders interactively inside Jupyter notebooks and JupyterLab: plots appear inline in cells, and you can toggle between inline rendering and the Bokeh server for apps that need callbacks and large data. Setup is a single magic line per notebook.

Enable Notebook Rendering

output_notebook()   # from bokeh.io
# or for JupyterLab:
output_notebook(hide_banner=True)
import numpy as np
from bokeh.plotting import figure

x = np.linspace(0, 10, 100)
p = figure(title="Inline in notebook")
p.line(x, np.sin(x), line_width=2, color="#5b8ff9")
p.circle(x, np.sin(x), size=3)
p   # last expression renders the plot inline

Inline vs show

from bokeh.io import show

show(p)        # explicit render (equivalent to the implicit last-expression render)

Widgets and Callbacks in the Notebook

from bokeh.models import Slider
from bokeh.layouts import column

slider = Slider(start=0, end=5, value=1, step=0.1, title="freq")

def update():
    line.y = np.sin(slider.value * x)

slider.on_change("value", update)
show(column(slider, p))

With output_notebook active, widget callbacks run in the notebook's kernel, so you can inspect variables in other cells while the widget works.

Running a Server from the Notebook

from bokeh.io import show
from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler

app = Application(FunctionHandler(my_app_function))
server = Server({"app": app}, port=5006, show=False)
server.io_loop.start()   # blocks; run in a separate cell or process
show("http://localhost:5006/app", button_label="Open server app")

Multiple Plots in One Cell

from bokeh.layouts import row

show(row(p1, p2))   # one call renders both side by side

Tips

  • Keep cell execution order in mind - widgets reference objects defined in earlier cells.
  • Restart the kernel to clear registered Bokeh documents if you get duplicate-render warnings.
  • For heavy data, switch to the server pattern so the DataFrame lives in the server process, not the notebook kernel.

Summary

Render Bokeh in Jupyter with output_notebook(), then plots appear inline when shown (explicitly or as a cell's last expression). Widgets and on_change callbacks work in the kernel, and you can launch a full Bokeh server from a notebook cell for apps that need persistent state or large data.