Bokeh slider for “Phenology of two interdependent traits in migratory birds in response to climate change”

(7 January 2017: some updates, see end of the post.)

A while I ago I made a slider using ipywidgets that could be embedded in a html page (handy for blog posts). This week I decided to see where things were at with IPython or Jupyter.

As of July 2015 the ipywidgets package is unsupported. The author recommends using IPython’s built-in interactive tools. However IPython doesn’t have static widgets yet, according to this issue. A StackOverflow answer mentioned Bokeh so I decided to give that a go.

Bokeh slider

Here is a slider that replicates my earlier ipywidgets effort:

This is pretty nice. It’s an interactive slider, works on desktop and mobile, and doesn’t have any of the notebook stuff around it. Just the graph with the interactive widget. Bokeh also provides tools for zooming and panning around. It’s also worth mentioning that Bokeh provides a GUI library (things like hboxes, vboxes, layouts, etc) and my impression is that you could have multiple plots changing based on one slider, two plots tied together on some parameter, or whatever else you dreamt up.

The slider is implemented in and is run using bokeh-server –ip –script One strange thing that I ran into was that the slider wasn’t interactive unless I opened up port 5006 on my server, even though Nginx is doing the proxy_pass stuff. I suspect that some of the Bokeh-generated Javascript expects to be able to connect to the host on 5006.

Here’s the relevant Nginx config settings:

server {

    # listen, root, other top level config...

    # Reverse proxy for Bokeh server running on port 5006:

    location /bokeh {

    location /static {

    location /bokehjs {

    # rest of the config...


In terms of coding, the Bokeh model is a bit different to the usual plotting procedure in that you set up
data sources, e.g.

obj.line_source  = ColumnDataSource(data=dict(

and then plot commands use that data source. You don’t pass NumPy arrays in directly:

plot = figure(plot_height=400, plot_width=400,
              tools=toolset, x_range=[130, 180], y_range=[110, 180])

plot.line('x_cV', 'x_cV',          source=obj.line_source, line_width=4, color='black')
plot.line('x_cV', 'arrival_date',  source=obj.line_source, line_width=4, color='purple', legend='Arrival time')
plot.line('x_cV', 'laying_date',   source=obj.line_source, line_width=4, color='red',    legend='Laying time')
plot.line('x_cV', 'hatching_date', source=obj.line_source, line_width=4, color='green',  legend='Hatching date')

Then, the input_change method calls my update_data method which actually updates the data sources. It doesn’t have to explicitly make a call to redraw the plot.

def update_data(self):
    u_q = self.u_q_slider.value  = get_line_data_for_bokeh(float(u_q))


Update 7 January 2016

The slider described above doesn’t work with the latest version of Bokeh, so here’s one that does:

Launching is slightly different (see this script):

bokeh serve --prefix=bokehslider 
                               --use-xheaders --port=5100

Happily for me this now plays nicely with my https nginx config:

    location /bokehslider {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host:$server_port;
            proxy_buffering off;

And there is no need to open up port 5006 as I said before.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s