Deploying Bokeh Apps

In [ ]:
import numpy as np
import holoviews as hv


HoloViews is an incredible convenient way of working interactively and exploratively within a notebook or commandline context, however when you have implemented a polished interactive dashboard or some other complex interactive visualization you often want to deploy it outside the notebook. The bokeh server provides a very convenient way of deploying HoloViews plots and interactive dashboards in a scalable and flexible manner. The bokeh server allows all the usual interactions that HoloViews lets you define and more including:

  • responding to plot events and tool interactions via Linked Streams
  • generating and interacting with plots via the usual widgets that HoloViews supports for HoloMap and DynamicMap objects.
  • using periodic and timeout events to drive plot updates
  • combining HoloViews plots with custom bokeh plots to quickly write highly customized apps.


In this guide we will cover how we can deploy a bokeh app from a HoloViews plot in a number of different ways:

  1. Inline from within the Jupyter notebook

  2. Starting a server interactively and open it in a new browser window.

  3. From a standalone script file

  4. Combining HoloViews and Bokeh models to create a more customized app

If you have read a bit about HoloViews you will know that HoloViews objects are not themselves plots, instead they contain sufficient data and metadata allowing them to be rendered automatically in a notebook context. In other words when a HoloViews object is evaluated a backend specific Renderer converts the HoloViews object into bokeh models, a matplotlib figure or a plotly graph. This intermediate representation is then rendered as an image or as HTML with associated Javascript, which is what ends up being displayed.

The workflow

The most convenient way to work with HoloViews is to iteratively improve a visualization in the notebook. Once you have developed a visualization or dashboard that you would like to deploy you can use the BokehRenderer to save the visualization or deploy it as a bokeh server app.

Here we will create a small interactive plot, using Linked Streams , which mirrors the points selected using box- and lasso-select tools in a second plot and computes some statistics:

In [ ]:
%%opts Points [tools=['box_select', 'lasso_select']]

# Declare some points
points = hv.Points(np.random.randn(1000,2 ))

# Declare points as source of selection stream
selection = hv.streams.Selection1D(source=points)

# Write function that uses the selection indices to slice points and compute stats
def selected_info(index):
    arr = points.array()[index]
    if index:
        label = 'Mean x, y: %.3f, %.3f' % tuple(arr.mean(axis=0))
        label = 'No selection'
    return points.clone(arr, label=label)(style=dict(color='red'))

# Combine points and DynamicMap
layout = points + hv.DynamicMap(selected_info, streams=[selection])

Working with the BokehRenderer

When working with bokeh server or wanting to manipulate a backend specific plot object you will have to use a HoloViews Renderer directly to convert the HoloViews object into the backend specific representation. Therefore we will start by getting a hold of a BokehRenderer :

In [ ]:
renderer = hv.renderer('bokeh')

All Renderer classes in HoloViews are so called ParameterizedFunctions, they provide both classmethods and instance methods to render an object. You can easily create a new Renderer instance using the .instance method:

In [ ]:
renderer = renderer.instance(mode='server')

Renderers can also have different modes, in this case we will instantiate the renderer in 'server' mode, which tells the Renderer to render the HoloViews object to a format that can easily be deployed as a server app. Before going into more detail about deploying server apps we will quickly remind ourselves how the renderer turns HoloViews objects into bokeh models.


The BokehRenderer converts the HoloViews object to a HoloViews Plot , which holds the bokeh models that will be rendered to screen. As a very simple example we can convert a HoloViews Image to a HoloViews plot:

In [ ]:
hvplot = renderer.get_plot(layout)
<LayoutPlot LayoutPlot01808>

Using the state attribute on the HoloViews plot we can access the bokeh Column model, which we can then work with directly.

In [ ]:

Column ( id = '5a8b7949-decd-4a96-b1f8-8f77ec90e5bf', …)

In the background this is how HoloViews converts any HoloViews object into bokeh models, which can then be converted to embeddable or standalone HTML and be rendered in the browser. This conversion is usually done in the background using the figure_data method:

In [ ]:
html = renderer.figure_data(hvplot)

Bokeh Documents

In bokeh the Document is the basic unit at which Bokeh models (such as plots, layouts and widgets) are held and serialized. The serialized JSON representation is then sent to BokehJS on the client side browser. When in 'server' mode the BokehRenderer will automatically return a server Document:

In [ ]:
(<bokeh.document.Document at 0x11afc7590>,
 {'file-ext': 'html', 'mime_type': u'text/html'})

We can also easily use the server_doc method to get a bokeh Document , which does not require you to make an instance in 'server' mode.

In [ ]:
doc = renderer.server_doc(layout)
doc.title = 'HoloViews App'

Deploying with bokeh serve

Deployment from a script with bokeh serve is one of the most common ways to deploy a bokeh app. Any .py or .ipynb file that attaches a plot to bokeh's curdoc can be deployed using bokeh serve . The easiest way to do this is using the BokehRenderer.server_doc method, which accepts any HoloViews object generates the appropriate bokeh models and then attaches them to curdoc . See below to see a full standalone script:

import numpy as np
import holoviews as hv
import holoviews.plotting.bokeh

renderer = hv.renderers('bokeh')

points = hv.Points(np.random.randn(1000,2 ))(plot=dict(tools=['box_select', 'lasso_select']))
selection = hv.streams.Selection1D(source=points)

def selected_info(index):
    arr = points.array()[index]
    if index:
        label = 'Mean x, y: %.3f, %.3f' % tuple(arr.mean(axis=0))
        label = 'No selection'
    return points.clone(arr, label=label)(style=dict(color='red'))

layout = points + hv.DynamicMap(selected_info, streams=[selection])

doc = renderer.server_doc(layout)
doc.title = 'HoloViews App'

In just a few steps, i.e. by our plot to a Document renderer.server_doc we have gone from an interactive plot which we can iteratively refine in the notebook to a deployable bokeh app. Note also that we can also deploy an app directly from a notebook. By adding BokehRenderer.server_doc(holoviews_object) to the end of the notebook any regular .ipynb file can be made into a valid bokeh app, which can be served with bokeh serve example.ipynb .

In addition to starting a server from a script we can also start up a server interactively, so let's do a quick deep dive into bokeh Application and Server objects and how we can work with them from within HoloViews.

Bokeh Applications and Server

A bokeh Application encapsulates a Document and allows it to be deployed on a bokeh server. The method provides an easy way to create an Application and either display it immediately in a notebook or manually include it in a server app.

To let us try this out we'll define a slightly simpler plot to deploy as a server app. We'll define a DynamicMap of a sine Curve varying by frequency, phase and an offset.

In [ ]:
def sine(frequency, phase, amplitude):
    xs = np.linspace(0, np.pi*4)
    return hv.Curve((xs, np.sin(frequency*xs+phase)*amplitude))(plot=dict(width=800))

ranges = dict(frequency=(1, 5), phase=(-np.pi, np.pi), amplitude=(-2, 2), y=(-2, 2))
dmap = hv.DynamicMap(sine, kdims=['frequency', 'phase', 'amplitude']).redim.range(**ranges)

app =
<bokeh.application.application.Application object at 0x11c0ab090>

Once we have a bokeh Application we can manually create a Server instance to deploy it. To start a Server instance we simply define a mapping between the URL paths and apps that we want to deploy. Additionally we define a port (defining port=0 will use any open port), and the IOLoop . To get an IOLoop we simply use IOLoop.current() , which will already be running if working from within a notebook but will give you a new IOLoop outside a notebook context.

In [ ]:
from tornado.ioloop import IOLoop
from bokeh.server.server import Server

loop = IOLoop.current()
server = Server({'/': app}, port=0, loop=loop)

Next we can define a callback on the IOLoop that will open the server app in a new browser window and actually start the app (and if outside the notebook the IOLoop):

In [ ]:
def show_callback():'/')
# Outside the notebook ioloop needs to be started
# loop.start() 

After running the cell above you should have noticed a new browser window popping up displaying our plot. Once you are done playing with it you can stop it with:

In [ ]:

The method allows us to the same thing automatically (but less flexibly) using the show=True and new_window=True arguments:

In [ ]:
server =, show=True, new_window=True)

We will once again stop this Server before continuing:

In [ ]:

Inlining apps in the notebook

Instead of displaying our app in a new browser window and manually creating a Server instance we can also display an app inline in the notebook simply by supplying the show=True argument to the method. The server app will be killed whenever you rerun or delete the cell that contains the output. Additionally, if your Jupyter Notebook server is not running on the default address or port ( localhost:8888 ) supply the websocket origin, which should match the first part of the URL of your notebook:

In [ ]:, show=True, websocket_origin='localhost:8888')

Periodic callbacks

One of the most important features of deploying apps is the ability to attach asynchronous, periodic callbacks, which update the plot. The simplest way of achieving this is to attach a Counter stream on the plot which is incremented on each callback. As a simple demo we'll simply compute a phase offset from the counter value, animating the sine wave:

In [ ]:
def sine(counter):
    phase = counter*0.1%np.pi*2
    xs = np.linspace(0, np.pi*4)
    return hv.Curve((xs, np.sin(xs+phase)))(plot=dict(width=800))

dmap = hv.DynamicMap(sine, streams=[hv.streams.Counter()])

app =, show=True, websocket_origin='localhost:8888')

Once we have created the app we can start a periodic callback with the periodic method on the DynamicMap . The first argument to the method is the period and the second argument the number of executions to trigger (we can set this value to None to set up an indefinite callback). As soon as we start this callback you should see the Curve above become animated.

In [ ]:
dmap.periodic(0.1, 100)

Combining HoloViews and Bokeh Plots/Widgets

While HoloViews provides very convenient ways of creating an app it is not as fully featured as bokeh itself is. Therefore we often want to extend a HoloViews based app with bokeh plots and widgets created directly using the bokeh API. Using the BokehRenderer we can easily convert a HoloViews object into a bokeh model, which we can combine with other bokeh models as desired.

To see what this looks like we will use the sine example again but this time connect a Stream to a manually created bokeh slider widget and play button. To display this in the notebook we will reuse what we learned about creating a Server instance using a FunctionHandler , you can of course run this in a script by calling the modify_doc function with with the Document returned by the bokeh curdoc() function.

In [ ]:
import numpy as np
import holoviews as hv

from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from import show
from bokeh.layouts import layout
from bokeh.models import Slider, Button

renderer = hv.renderer('bokeh').instance(mode='server')

# Create the holoviews app again
def sine(phase):
    xs = np.linspace(0, np.pi*4)
    return hv.Curve((xs, np.sin(xs+phase)))(plot=dict(width=800))

stream = hv.streams.Stream.define('Phase', phase=0.)()
dmap = hv.DynamicMap(sine, streams=[stream])

# Define valid function for FunctionHandler
# when deploying as script, simply attach to curdoc
def modify_doc(doc):
    # Create HoloViews plot and attach the document
    hvplot = renderer.get_plot(dmap, doc)

    # Create a slider and play buttons
    def animate_update():
        year = slider.value + 0.2
        if year > end:
            year = start
        slider.value = year

    def slider_update(attrname, old, new):
        # Notify the HoloViews stream of the slider update 
    start, end = 0, np.pi*2
    slider = Slider(start=start, end=end, value=start, step=0.2, title="Phase")
    slider.on_change('value', slider_update)

    def animate():
        if button.label == '► Play':
            button.label = '❚❚ Pause'
            doc.add_periodic_callback(animate_update, 50)
            button.label = '► Play'
    button = Button(label='► Play', width=60)
    # Combine the holoviews plot and widgets in a layout
    plot = layout([
    [slider, button]], sizing_mode='fixed')
    return doc

# To display in the notebook
handler = FunctionHandler(modify_doc)
app = Application(handler)
show(app, notebook_url='localhost:8888')

# To display in a script
#    doc = modify_doc(curdoc()) 

If you had trouble following the last example, you will noticed how much verbose things can get when we drop down to the bokeh API. The ability to customize the plot comes at the cost of additional complexity. However when we need it the additional flexibility of composing plots manually is there.