vtkdatawidgets

Version: 0.1.0.dev

Jupyter data-widgets based on VTK sources.

Quickstart

To get started with vtkdatawidgets, install with pip:

pip install vtkdatawidgets

or with conda:

conda install vtkdatawidgets

Contents

Installation

The simplest way to install vtkdatawidgets is via pip:

pip install vtkdatawidgets

or via conda:

conda install vtkdatawidgets

If you installed via pip, and notebook version < 5.3, you will also have to install / configure the front-end extension as well. If you are using classic notebook (as opposed to Jupyterlab), run:

jupyter nbextension install [--sys-prefix / --user / --system] --py vtkdatawidgets

jupyter nbextension enable [--sys-prefix / --user / --system] --py vtkdatawidgets

with the appropriate flag. If you are using Jupyterlab, install the extension with:

jupyter labextension install jupyter-vtk-datawidgets

If you are installing using conda, these commands should be unnecessary, but If you need to run them the commands should be the same (just make sure you choose the –sys-prefix flag).

Introduction

Examples

This section contains several examples generated from Jupyter notebooks. The widgets have been embedded into the page for demonstrative pruposes.

Introduction

Jupyter - VTK bridge
In [1]:
import vtk
from vtkdatawidgets.vtk_binding import VtkJupyterBridge

First we set up our bridge object:

In [2]:
bridge = VtkJupyterBridge()

This object is a VTK sink (algorithm with 1 input, 0 outputs), that translates the input data to a jupyter widget representation on RequestData (i.e. when .Update() is called).

Let’s set up some placeholder VTK source (a simple sphere in this case), and set it as the input of our bridge:

In [3]:
sphere = vtk.vtkSphereSource()
bridge.SetInputConnection(sphere.GetOutputPort())
bridge.Update()
C:\cleanconda\envs\vtk\lib\site-packages\ipydatawidgets\ndarray\serializers.py:62: UserWarning: Cannot serialize (u)int64 data, Javascript does not support it. Casting to (u)int32.
  warnings.warn('Cannot serialize (u)int64 data, Javascript does not support it. '

At the .Update() call, the data is serialized and sent to the browser. There we can use it however we want. For now, let us visualize it using a simple vtk.js based rendering. This will set up a similar bridge in the browser: from widgets to a vtk.js data source (0 inputs, 1 output):

In [4]:
from vtkdatawidgets import VtkRenderer
renderer = VtkRenderer(dataset=bridge.widget, background=(0.5, 0, 0), size=(600, 400))
renderer

The VtkRenderer object is a standard Jupyter widget that we connect up to the widget side of the bridge (bridge.widget). It does not understand native VTK objects.

Being a widget, the VtkRenderer is an interactive object. Setting its properties are immediately reflected in the frontend:

In [5]:
import ipywidgets
picker = ipywidgets.ColorPicker(value='darkred')
ipywidgets.jslink((picker, 'value'), (renderer, 'background'))
picker

Similarly, we can create a new bridge (this time from a cone source), and set that as the input of the renderer:

In [ ]:
cone = vtk.vtkConeSource()
bridge2 = VtkJupyterBridge()
bridge2.SetInputConnection(cone.GetOutputPort())
bridge2.Update()
renderer.dataset = bridge2.widget

Or, we can simply change the input of the current bridge:

In [ ]:
bridge2.SetInputConnection(sphere.GetOutputPort())
bridge2.Update()

Notice that nothing will happen until you call Update() on the bridge, as VTK only produces the data on request.

In [ ]:
bridge2.SetInputConnection(cone.GetOutputPort())
In [ ]:
bridge2.Update()

In the future, there will likely be support for front-end driven update calls, but for now this is unsupported.

Further notes:

Note that VTK.js currently only supports: - PolyData - ImageData

So if you need to transform any inputs you have to those formats before passing it to the bridge.

Developer install

To install a developer version of vtkdatawidgets, you will first need to clone the repository:

git clone https://github.com/vidartf/jupyter-vtk-datawidgets
cd jupyter-vtk-datawidgets

Next, install it with a develop install using pip:

pip install -e .

If you are planning on working on the JS/frontend code, you should also do a link installation of the extension:

jupyter nbextension install [--sys-prefix / --user / --system] --symlink --py vtkdatawidgets

jupyter nbextension enable [--sys-prefix / --user / --system] --py vtkdatawidgets

with the appropriate flag. Or, if you are using Jupyterlab:

jupyter labextension install .