Event handling¶

This notebook demonstrates how to handle events generated by NiiVue. In this example, image details are displayed whenever a new image is loaded. Dragging and dropping a NIfTI-format volume onto the panel triggers a new report, but you can easily adapt the script to perform other actions in response to image-loading events.

In [1]:
from ipyniivue import NiiVue
from IPython.display import display
from ipywidgets import Output

from ipyniivue import SliceType

out = Output()
display(out)

nv = NiiVue(slice_type=SliceType.MULTIPLANAR)


@nv.on_image_loaded
def on_image_loaded(volume):
    """
    Event handler called when an image is loaded.

    Parameters
    ----------
    volume : ipyniivue.Volume
        The loaded image volume.
    """
    with out:
        print("Image loaded:", volume)

# Load volumes
nv.load_volumes(
    [
        {
            "path": "../images/mni152.nii.gz",
            "colormap": "gray",
            "visible": True,
            "opacity": 1.0,
        },
        {
            "path": "../images/hippo.nii.gz",
            "colormap": "red",
            "visible": True,
            "opacity": 1,
        },
    ]
)

# Display the widget
nv
/home/runner/work/ipyniivue/ipyniivue/src/ipyniivue/widget.py:1327: UserWarning: Ignored unsupported kwargs in Volume: ['visible']
  volume_objects.append(Volume(**item))
Out[1]:
In [ ]: