Jupyter notebook with Slicer Preview

I’m working on a Slicer Jupyter notebook using the latest preview version (Mac OS 4.11.0-2020-04-19) but am getting an error when I try to display any images. The output is a broken image icon with no errors. I get the expected display when I try the same code block using the 4.10 stable kernel. To test I used sample code:

import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
volume = sampleDataLogic.downloadMRHead()
slicer.app.layoutManager().setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpView)

display()

Are there any know issues with using Jupyter and Slicer Preview?

image

To clarify, the Slicer app managed by Jupyter is displaying the correct visualization, but it’s not showing up in the notebook when using the preview version.

I’ve also tested using the latest preview version in Windows and have the same error.

@lassoan @pieper
Any input into this? This is same for me too (output displays only on the slicer window, not in the notebook).

@muratmaga what operating system did you try this on?

I’ve replicated this on MacOS Mojave and Windows 10.

I don’t use the Jupyter interface myself, so I’m not sure what to suggest. @jcfr might also have ideas.

I could reproduce the issue and push a fix soon

1 Like

I’ve fixed the issue (it was related to byte buffer to string conversion, probably changed because of switching to UTF-8). It should work in tomorrow’s build but until then you can copy-paste this into a cell, run it, and then you can use display2() for capturing screenshot of the view layout:

def display2():
    layoutManager = slicer.app.layoutManager()
    slicer.util.setViewControllersVisible(False)
    slicer.app.processEvents()
    slicer.util.forceRenderAllViews()
    screenshot = layoutManager.viewport().grab()
    slicer.util.setViewControllersVisible(True)
    bArray = qt.QByteArray()
    buffer = qt.QBuffer(bArray)
    buffer.open(qt.QIODevice.WriteOnly)
    screenshot.save(buffer, "PNG")
    slicer.modules.jupyterkernel.executeResultDataValue = bArray.toBase64().data().decode()
    slicer.modules.jupyterkernel.executeResultDataType = "image/png"
1 Like

Thanks @lassoan! Your temporary solution is working well for me.

1 Like

@lassoan
I tested the updated SlicerJupyter extension in Windows and it is great! It fixes my display issues. It looks like the Mac build failed and is not available in the preview version.