Vega plot not displaying within a holoviz panel in jupyter notebook

482 views Asked by At

I find the holoviz panel a very interesting solution to building data visualisation dashboards. Unfortunately, I have some issues getting a vega plot of a node-link diagram to work within a panel in a jupyter notebook.

The relevant imports etc:

  • import panel
  • pn.extension()
  • from vega import Vega

My findings:

  • The vega import works nicely when used outside of a panel: the Vega specification copy/pasted from https://vega.github.io/editor/#/examples/vega/force-directed-layout is visualised as it should be using Vega(spec) (see screenshot 1).
  • When using pn.pane.Vega(spec) I get an empty space. Running the visualisation externally using pn.pane.Vega(spec).show() and looking at the source code, I see that the div is empty (see screenshot 2).

Any help with getting this working much appreciated...

Thank you, jan.

Here is a minimal script to show the issue:

#!/usr/bin/env python
import panel as pn
from bokeh.plotting import output_notebook
from vega import Vega
pn.extension('vega')
output_notebook()

spec = {
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 400,
  "height": 200,

  "data": [
    {
      "name": "table",
      "values": [
        {"category": "A", "amount": 28},
        {"category": "B", "amount": 55},
        {"category": "C", "amount": 43}
      ]
    }
  ],

  "scales": [
    {
      "name": "xscale",
      "type": "band",
      "domain": {"data": "table", "field": "category"},
      "range": "width"
    },
    {
      "name": "yscale",
      "domain": {"data": "table", "field": "amount"},
      "range": "height"
    }
  ],

  "marks": [
    {
      "type": "rect",
      "from": {"data":"table"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "category"},
          "width": {"scale": "xscale", "band": 1},
          "y": {"scale": "yscale", "field": "amount"},
          "y2": {"scale": "yscale", "value": 0}
        },
        "update": {
          "fill": {"value": "steelblue"}
        }
      }
    }
  ]
}

Vega(spec) # => shows barchart => OK

pn.Column(pn.panel("## Vega test"),
          pn.pane.Vega(spec),
          pn.panel("_end of test_"))
# => shows "Vega test", then empty space, the "end of test"

pn.Column(pn.panel("## Vega test"),
          pn.panel(spec),
          pn.panel("_end of test_"))
# => shows "Vega test", then empty space, the "end of test"
1

There are 1 answers

0
Sander van den Oord On

So the conclusion is that this was a bug in panel and quickly fixed by @philippjfr: https://github.com/holoviz/panel/issues/872

To make this work you need panel 0.7.1 for this.

Since this version is not available yet, you can install the latest version of panel with:

pip install git+https://github.com/holoviz/panel.git

The vega plot can be shown with the latest version of panel as follows:

pn.Column(pn.pane.Vega(spec))

See also this discussion on discourse.holoviz.org:
https://discourse.holoviz.org/t/vega-plot-not-displaying-within-a-holoviz-panel-in-jupyter-notebook/49/5