howto

Bokeh Web Apps

Applies to DSS 4.1 and above | December 08, 2017

In Dataiku DSS, web apps provide a way to create interactive visualizations that can be shared on dashboards for sophisticated reporting. You can create web apps with HTML/Javascript, R Shiny, or Python Bokeh.

In this post, we’ll create a simple Bokeh web app in Dataiku DSS. It’s a simple scatterplot on Haiku T-shirt sales data, related to the data used in the Tutorials.

Prerequisites

You will need:

  • The Orders_enriched_prepared dataset. This can be found in the project DSS Tutorials > Automation > Deployment or you can download the data and import it into a new project.
  • A Python code environment with the bokeh and mandatory Dataiku packages.

Creating a New Bokeh Webapp

Create a new empty Bokeh web app:

  1. In the top navigation bar, select Lab - Notebooks > Web apps
  2. Click + New Web App
  3. Select Bokeh
  4. Choose An empty Bokeh app and type a name for the web app

Notebook/Webapps navigation bar with arrows on: 1. Lab - notebooks universe icon, 2.webapps.

You will be redirected to the web app editor.

The Web App Editor

The web app editor is divided into two panes.

Bokeh web app editor; empty

The left pane allows you to see and edit the Python code underlying the web app.

The right pane gives you several views on the web app.

  • The Preview tab allows you to write and test your code in the left pane while having immediate visual feedback in the right pane. At any time you can save or reload your current code by clicking on the Save button or the Reload Preview button.
  • The Python tab allows you to look at different portions of the code side-by-side in the left and right panes.
  • The Log is useful for troubleshooting problems.
  • Settings allows you to set the code environment for this web app, if you want it to be different from the project default.

Coding the Web App

Let’s build the code behind the Python Bokeh web app.

Importing the Packages

Insert the following code into the Python tab so that we’ll have the necessary tools to create the web app.

from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput, Select
from bokeh.plotting import figure
import dataiku
import pandas as pd

Setting up the Data

Add the following code to the Python tab to access a Dataiku dataset as a pandas dataframe.

# Set up data
mydataset = dataiku.Dataset("Orders_enriched_prepared")
df = mydataset.get_dataframe()

Add the following code to the Python tab. This code extracts the customer age and total amount spent from the pandas dataframe to define the source data for the visualization as a ColumnDataSource.

x = df['age']
y = df['total']
source = ColumnDataSource(data=dict(x=x, y=y))

Nothing is displayed yet, because we haven’t created the visualization, but there are no errors in the log.

Defining the Visualization

Add the following code to the Python tab to define the output visualization. First we create a plot object with the desired properties. Here we have chosen static values for the limits of the x and y axes, but we could alternatively compute the minimum and maximum values of customer age and use those to define the axis limits. Next we define the visualization as a scatterplot that plots data from the source defined above.

# Set up plot
plot = figure(plot_height=400, plot_width=400, title="Order total by customer age",
              tools="crosshair,pan,reset,save,wheel_zoom",
              x_range=[0, 100], y_range=[0,500])

plot.scatter('x', 'y', source=source)

The following code defines the layout of the web app and adds it to the current “document”. For now, we’ll include an empty widgetbox that we’ll populate in a moment when we add the interactivity.

# Set up layouts and add to document
inputs = widgetbox()

curdoc().add_root(row(inputs, plot, width=800))

Save your work, and the preview should show the current (non-interactive) scatterplot.

Editor with a non-interactive Bokeh web app

Adding Interactivity

The current scatterplot includes all orders from 2013-2017, across all types of t-shirts sold. Now let’s add the ability to select a subset of years, and a specific category of t-shirt. To do this, we need to make changes to the Python code.

The code in this section should be added after the code to set up the plot, but before the code to define the layout of the web app.

In the Python tab, add the following.

# Set up widgets
text = TextInput(title="Title", value='Order total by customer age')
min_year = Slider(title="Order year start", value=2013, start=2013, end=2017, step=1)
max_year = Slider(title="Order year max", value=2017, start=2013, end=2017, step=1)
category = Select(title="T-shirt Category", value="All",
                           options=[("All","All"),("White T-Shirt M", "White T-Shirt M"), ("White T-Shirt F", "White T-Shirt F"), ("Black T-Shirt M", "Black T-Shirt M"), ("Black T-Shirt F", "Black T-Shirt F"), ("Hoodie","Hoodie"),("Tennis Shirt","Tennis Shirt")])

This defines four widgets.

  • text accepts text input to be used as the title of the visualization.
  • min_year and max_year are sliders that take values from 2013 to 2017 in integer steps. Their default values are 2013 and 2017, respectively
  • category is a selection that has an option for each t-shirt category, plus “All”. Its default value is All.

Add the following code, which contains the instructions on how to update the web app when a user interacts with it.

def update_title(attrname, old, new):
    plot.title.text = text.value

def update_data(attrname, old, new):
    category_value = category.value
    selected = df[(df.order_date_year>=min_year.value) & (df.order_date_year<=max_year.value)]
    if (category_value != "All"):
        selected = selected[selected.tshirt_category.str.contains(category_value)==True]
    # Generate the new plot
    x = selected['age']
    y = selected['total']
    source.data = dict(x=x, y=y)
  • When the title text is changed, update_title() updates plot.title.text to the new value
  • When the sliders or the select widget are changed, update_data takes the input dataframe df and uses the widget selections to filter the dataframe to only use records with the correct order year and t-shirt category. It then defines the x and y axes of the scatterplot to be the age and order total from the filtered dataframe.

Add the following code that listens for changes to the widget values using the on_change() method, which calls the functions above to update the web app.

# Set up callbacks
text.on_change('value', update_title)

for w in [min_year, max_year, category]:
    w.on_change('value', update_data)

Finally, change the definition of inputs as follows, to include the four widgets so that they are displayed in the web app.

inputs = widgetbox(text, min_year, max_year, category)

Save your work, refresh the preview, and it should now show the current interactive scatterplot.

Editor with an Interactive Bokeh web app

Publish to a Dashboard

When you are done with editing, you can easily publish your web app on a dashboard from the Actions dropdown at the top-right corner of the screen.

Webapps editor window with the actions drop-down menu opened and arrow on publish

What’s Next

Using Dataiku DSS, you have created an interactive Bokeh web app and published it to a dashboard.

You can examine this sample web app further on the Dataiku gallery.

For further inspiration on what is possible in Bokeh web apps, see the Bokeh gallery (external).