from bokeh.plotting import hplot, figure, show, output_server, Session from numpy import arange, sin, cos
bokeh-server instead of
output_notebook incurs several advantages.
- 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.
- Dynamic features like animation and applets seem to only work, or at least work much better, when using the Bokeh server.
- 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 127.0.0.1
-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.
127.0.0.1 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
127.0.0.1 with the IP address you use when invoking
5006 is the default port for
bokeh-server. You can change this on startup using the
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.)
Session object takes care of establishing a connection with your
bokeh-server. When creating this object, you can spcify a
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='http://192.168.3.137:5006/', 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
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.login('anon', '1234') output_server('test', session=session)
ready to make some plots¶
VBox are deprecated since
Now let’s make a set of two simple plots of
sin (note, we’ll make use of the
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)
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.