Skip to article frontmatterSkip to article content

Visualizer Widgets

Visualizer widgets allow you to interactively explore a session as you would in a Vapor GUI visualizer. This notebook shows how to use visualizer widgets and how to add additional dynamic parameter inputs.

Note: Widgets require an active kernel to operate. To try it out, run this notebook on your local machine.

import example_utils
from vapor import session, renderer, dataset, camera, widget
import ipywidgets as widgets

ses = session.Session()
data = example_utils.OpenExampleDataset(ses)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import example_utils
      2 from vapor import session, renderer, dataset, camera, widget
      3 import ipywidgets as widgets

File ~/work/vapor-python-cookbook/vapor-python-cookbook/notebooks/example_utils.py:20
     16     sys.path.append('..')
     19 from inspect import signature
---> 20 import numpy as np
     21 from math import sin
     23 def SampleFunctionOnRegularGrid(f, ext=None, shape=None):

ModuleNotFoundError: No module named 'numpy'

Render an Iso Surface

ren = data.NewRenderer(renderer.VolumeIsoRenderer)
ren.SetVariableName(data.GetDataVarNames(3)[0]) # Set to first 2D data variable
ren.SetIsoValues([ren.GetIsoValues()[0]+0.1])

ses.GetCamera().ViewAll()
ses.Show()

Create a visualizer to explore the scene

Try dragging the image to rotate the view. Hover over the visualizer to see the full controls.

viz = widget.VaporVisualizerWidget(ses)
viz

Add an interactive iso value slider using ipywidgets

tf = ren.GetPrimaryTransferFunction()
dataRange = tf.GetMinMaxMapValue()

def sliderChanged(change):
    ren.SetIsoValues([change.new])
    viz.Render(fast=True)

slider = widgets.FloatSlider(value=ren.GetIsoValues()[0], min=dataRange[0], max=dataRange[1], step=(dataRange[1]-dataRange[0])/100)
slider.observe(sliderChanged, names='value')

widgets.VBox([
    viz,
    widgets.HBox([widgets.Label("Iso value:"), slider])
])