Creating interactive dashboards#

import pandas as pd
import holoviews as hv

from bokeh.sampledata import stocks
from holoviews.operation.timeseries import rolling, rolling_outlier_std


In the Data Processing Pipelines section we discovered how to declare a DynamicMap and control multiple processing steps with the use of custom streams as described in the Responding to Events guide. A DynamicMap works like a tiny web application, with widgets that select values along a dimension, and a plot that updates. Let’s start with a function that loads stock data and see what a DynamicMap can do:

def load_symbol(symbol, variable, **kwargs):
    df = pd.DataFrame(getattr(stocks, symbol))
    df['date'] ='datetime64[ns]')
    return hv.Curve(df, ('date', 'Date'), variable).opts(framewise=True)

stock_symbols = ['AAPL', 'IBM', 'FB', 'GOOG', 'MSFT']
variables = ['open', 'high', 'low', 'close', 'volume', 'adj_close']
dmap = hv.DynamicMap(load_symbol, kdims=['Symbol','Variable'])
dmap = dmap.redim.values(Symbol=stock_symbols, Variable=variables)

rolling(dmap, rolling_window=2)

Here we already have widgets for Symbol and Variable, as those are dimensions in the DynamicMap, but what if we wanted a widget to control the rolling_windowwidth value in the HoloViews operation? We could redefine the DynamicMap to include the operation and accept that parameter as another dimension, but in complex cases we would quickly find we need more flexibility in defining widgets and layouts than DynamicMap can give us directly.

Building dashboards#

For more flexibility, we can build a full-featured dashboard using the Panel library, which is what a DynamicMap is already using internally to generate widgets and layouts. We can easily declare our own custom Panel widgets and link them to HoloViews streams to get dynamic, user controllable analysis workflows.

Here, let’s start with defining various Panel widgets explicitly, choosing a RadioButtonGroup for the symbol instead of DynamicMaps’s default Select widget:

import panel as pn

symbol = pn.widgets.RadioButtonGroup(options=stock_symbols)
variable = pn.widgets.Select(options=variables)
rolling_window = pn.widgets.IntSlider(name='Rolling Window', value=10, start=1, end=365)

pn.Column(symbol, variable, rolling_window)

As you can see, these widgets can be displayed but they aren’t yet attached to anything, so they don’t do much. We can now use pn.bind to bind the symbol and variable widgets to the arguments of the DynamicMap callback function, and provide rolling_window to the rolling operation argument. (HoloViews operations accept Panel widgets or param Parameter values, and they will then update reactively to changes in those widgets.)

We can then lay it all out into a simple application that works similarly to the regular DynamicMap display but where we can add our additional widget and control every aspect of the widget configuration and the layout:

dmap = hv.DynamicMap(pn.bind(load_symbol, symbol=symbol, variable=variable))
smoothed = rolling(dmap, rolling_window=rolling_window)

app = pn.Row(pn.WidgetBox('## Stock Explorer', symbol, variable, rolling_window), 
             smoothed.opts(width=500, framewise=True)).servable()

Here we chose to lay the widgets out into a box to the left of the plot, but we could put the widgets each in different locations, add different plots, etc., to create a full-featured dashboard. See for the full set of widgets and layouts supported.

Now that we have an app, we can launch it in a separate server if we wish (using, run it as an entirely separate process (panel serve <thisfilename>.ipynb, to serve the object marked servable above), or export it to a static HTML file (sampling the space of parameter values using “embed”):"dashboard.html", embed=True)
  0%|          | 0/90 [00:00<?, ?it/s]
  1%|          | 1/90 [00:00<00:15,  5.72it/s]
  3%|▎         | 3/90 [00:00<00:07, 11.76it/s]
  6%|▌         | 5/90 [00:00<00:06, 12.17it/s]
  8%|▊         | 7/90 [00:00<00:06, 12.09it/s]
 10%|█         | 9/90 [00:00<00:05, 13.95it/s]
 12%|█▏        | 11/90 [00:00<00:05, 13.41it/s]
 14%|█▍        | 13/90 [00:01<00:05, 12.98it/s]
 17%|█▋        | 15/90 [00:01<00:05, 14.23it/s]
 19%|█▉        | 17/90 [00:01<00:05, 13.55it/s]
 21%|██        | 19/90 [00:01<00:05, 12.90it/s]
 24%|██▍       | 22/90 [00:01<00:04, 14.96it/s]
 28%|██▊       | 25/90 [00:01<00:03, 16.36it/s]
 31%|███       | 28/90 [00:01<00:03, 17.26it/s]
 34%|███▍      | 31/90 [00:02<00:03, 17.97it/s]
 38%|███▊      | 34/90 [00:02<00:03, 18.38it/s]
 41%|████      | 37/90 [00:02<00:02, 20.26it/s]
 49%|████▉     | 44/90 [00:02<00:01, 31.25it/s]
 57%|█████▋    | 51/90 [00:02<00:00, 39.73it/s]
 62%|██████▏   | 56/90 [00:02<00:01, 29.80it/s]
 67%|██████▋   | 60/90 [00:03<00:01, 23.69it/s]
 70%|███████   | 63/90 [00:03<00:01, 20.59it/s]
 73%|███████▎  | 66/90 [00:03<00:01, 18.58it/s]
 77%|███████▋  | 69/90 [00:03<00:01, 17.14it/s]
 79%|███████▉  | 71/90 [00:03<00:01, 15.76it/s]
 81%|████████  | 73/90 [00:04<00:01, 13.63it/s]
 83%|████████▎ | 75/90 [00:04<00:01, 14.54it/s]
 86%|████████▌ | 77/90 [00:04<00:00, 13.84it/s]
 88%|████████▊ | 79/90 [00:04<00:00, 13.38it/s]
 90%|█████████ | 81/90 [00:04<00:00, 14.50it/s]
 92%|█████████▏| 83/90 [00:04<00:00, 13.77it/s]
 94%|█████████▍| 85/90 [00:05<00:00, 13.18it/s]
 97%|█████████▋| 87/90 [00:05<00:00, 14.43it/s]
 99%|█████████▉| 89/90 [00:05<00:00, 13.80it/s]

Declarative dashboards#

What if we want our analysis code usable both as a dashboard and also in “headless” contexts such as batch jobs or remote execution? Both Panel and HoloViews are built on the param library, which lets you capture the definitions and allowable values for your widgets in a way that’s not attached to any GUI. That way you can declare all of your attributes and allowed values once, presenting a GUI if you want to explore them interactively or else simply provide specific values if you want batch operation.

With this approach, we declare a StockExplorer class subclassing Parameterized and defining three parameters, namely the rolling window, the symbol, and the variable to show for that symbol:

import param

class StockExplorer(param.Parameterized):

    rolling_window = param.Integer(default=10, bounds=(1, 365))
    symbol = param.ObjectSelector(default='AAPL', objects=stock_symbols)
    variable = param.ObjectSelector(default='adj_close', objects=variables)

    @param.depends('symbol', 'variable')
    def load_symbol(self):
        df = pd.DataFrame(getattr(stocks, self.symbol))
        df['date'] ='datetime64[ns]')
        return hv.Curve(df, ('date', 'Date'), self.variable).opts(framewise=True)

Here the StockExplorer class will look similar to the Panel code above, defining most of the same information that’s in the Panel widgets, but without any dependency on Panel or other GUI libraries; it’s simply declaring that this code accepts certain parameter values of the specified types and ranges. These declarations are useful even outside a GUI context, because they allow type and range checking for detecting user errors, but they are also sufficient for creating a GUI later.

Instead of using pn.bind to bind widget values to functions, here we are declaring that each method depends on the specified parameters, which can be expressed independently of whether there is a widget controlling those parameters; it simply declares (in a way that Panel can utilize) that the given method needs re-running when any of the parameters in that list changes.

Now let’s use the load_symbol method, which already declares which parameters it depends on, as the callback of a DynamicMap and create widgets out of those parameters to build a little GUI:

explorer = StockExplorer()
stock_dmap = hv.DynamicMap(explorer.load_symbol)
pn.Row(explorer.param, stock_dmap)

Here you’ll notice that the rolling_window widget doesn’t do anything, because it’s not connected to anything (e.g., nothing @param.depends on it). As we saw in the Data Processing Pipelines section, the rolling and rolling_outlier_std operations both accept a rolling_window parameter, so lets provide that to the operations and display the output of those operations. Finally we compose everything into a panel Row:

# Apply rolling mean
smoothed = rolling(stock_dmap, rolling_window=explorer.param.rolling_window)

# Find outliers
outliers = rolling_outlier_std(stock_dmap, rolling_window=explorer.param.rolling_window).opts(
    color='red', marker='triangle')

pn.Row(explorer.param, (smoothed * outliers).opts(width=600))