Bokeh Server example

from bokeh.plotting import hplot, figure, show, output_server, Session
from numpy import arange, sin, cos

Using the bokeh-server instead of output_file or output_notebook incurs several advantages.

  1. I keep my notebooks under version control–it makes sense to reduce the number of unneeded images (which unavoidably change every commit) embedded into the notebook.
  2. Dynamic features like animation and applets seem to only work, or at least work much better, when using the Bokeh server.
  3. You can share your plots with other people on your network, like your academic advisor.

start the server

The first step to using the Bokeh server is to start its process:

> bokeh-server -m --ip

The -m flag tells the server to run in multiple user mode. Right now, this is suggested in the guide on how to use the Bokeh server. The --ip argument lets you specify which IP the server should listen on. is actually the default (I include this because, at least for me, this is an important part of my setup; I need to make sure the IP address is on the VPN to which my lab and I connect).

open Bokeh Plot Server page in browser

We are almost ready to make plots. Open a new tab in your browser and go to the page (replace with the IP address you use when invoking bokeh-server). Port 5006 is the default port for bokeh-server. You can change this on startup using the --port flag.

start Jupyter/IPython notebook (or just Python for that matter)

Now we need to run our Python commands to generate plots using Bokeh. For this, I use Jupyter/IPython Notebook. (Who am I kidding? If you’re reading this, you’re probably definitely using Jupyter/IPython Notebook.)

create a Session object

The Session object takes care of establishing a connection with your bokeh-server. When creating this object, you can spcify a name, root_url, and whether or not to load_from_config file when establishing the session, among other input parameters. I choose to ignore the name (it will default to root_url). Make sure to specify the root_url as the url (\<ip\>:\<port\>) on your bokeh-server is listening.

session = Session(root_url='', load_from_config=False)

register your user

The first time you use your Bokeh server instance (TODO?? in multiple user mode), you’ll need to create a user. This can be done with the session.register command, which takes username and password arguments as strings.

TODO: Once your user is registered, they

session.register('anon', '1234')

login and set plotting output to go to server

server.login is pretty self explanatory. output_server takes a document name as its first argument. Without a session argument, it looks for a server running on localhost. For our situation, we need to pass in our session instance.

session.login('anon', '1234')
output_server('test', session=session)

ready to make some plots

Note: HBox, VBox are deprecated since 0.8.2.

Now let’s make a set of two simple plots of cos and sin (note, we’ll make use of the numpy and bokeh imports from the first cell).

hplot allows us to stack our plots horizontally–otherwise they will stack vertically as they’re appended to the document.

x = arange(0, 10, .1)
y1 = sin(x)
y2 = cos(x)
fig1 = figure(title='sin', plot_width=500, plot_height=200)
fig1.line(x, y1)
fig2 = figure(title='cos', plot_width=500, plot_height=200)
fig2.line(x, y2)
plot = hplot(fig1, fig2)

call show to make plot appear in our plot document

Because of our call to output_server from earlier, calls to show will send the plot data to the server, which will publish them to the plot page we opened in our browser earlier.


And here’s the result you should see in your Bokeh Plot Server tab when you open your test document.



Leave a Reply