# Jupyter notebook with Slicer Preview

**URL:** <https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276>\
**Category:** Support\
**Created:** [April 23, 2020, 11:04pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276 "2020-04-23T23:04:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [April 23, 2020, 11:04pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/1 "2020-04-23T23:04:08Z")

</div>

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:

```auto
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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/1/01ec27be930caed5566a37c2533d6cb46bc83d0a.png)

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [April 24, 2020, 12:26am UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/2 "2020-04-24T00:26:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [April 24, 2020, 8:05pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/3 "2020-04-24T20:05:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 24, 2020, 8:31pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/4 "2020-04-24T20:31:20Z")

</div>

@muratmaga what operating system did you try this on?

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [April 24, 2020, 8:47pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/5 "2020-04-24T20:47:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [April 24, 2020, 9:00pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/6 "2020-04-24T21:00:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 24, 2020, 9:01pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/7 "2020-04-24T21:01:50Z")

</div>

I could reproduce the issue and push a fix soon

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 24, 2020, 9:34pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/8 "2020-04-24T21:34:54Z")

</div>

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:

```python
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"

```

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [April 24, 2020, 9:42pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/9 "2020-04-24T21:42:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [April 27, 2020, 10:20pm UTC](https://discourse.slicer.org/t/jupyter-notebook-with-slicer-preview/11276/10 "2020-04-27T22:20:20Z")

</div>

@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.
