Skip to content

Commit 8b0d631

Browse files
wmvanvlietlarsoner
authored andcommitted
Integrate better with Jupyter notebook (#268)
* Integrate better with Jupyuter notebook Mayavi can render things inside a jupyter notebook as either PNG or X3D. This functionality can be enabled with `mlab.init_notebook()`. This PR adds an `_ipython_display_` hook to the `Brain` class that renders the brain accordgin to the Mayavi notebook integration settings. * Add note on Jupyter notebook integration to docs * Fix links in doc
1 parent df68527 commit 8b0d631

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

doc/install.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ notebook), you have to activate the correct GUI backend, which is probably qt::
7777
This will allow you to have an open PySurfer window while still being able to
7878
execute code in the console/notebook.
7979

80+
It is also possible to embed the PySurfer visualization into a Jupyter notebook.
81+
This is achieved by leveraging `Mayavi's notebook integration
82+
<https://docs.enthought.com/mayavi/mayavi/tips.html#using-mayavi-in-jupyter-notebooks>`_::
83+
84+
from mayavi import mlab
85+
mlab.init_notebook(backend='png')
86+
87+
The ``backend`` parameter can either be ``'png'`` to render the visualization
88+
as a static PNG image, or ``'x3d'`` to render it using
89+
`X3D <https://www.x3dom.org>`_ (still experimental).
90+
8091
If you are having trouble getting started using PySurfer, please describe the problem on the `nipy mailing list`_.
8192

8293
.. include:: links_names.txt

surfer/viz.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,23 @@ def animate(self, views, n_steps=180., fname=None, use_cache=False,
27762776
if ret:
27772777
print("\n\nError occured when exporting movie\n\n")
27782778

2779+
def __repr__(self):
2780+
return ('<Brain subject_id="%s", hemi="%s", surf="%s">' %
2781+
(self.subject_id, self._hemi, self.surf))
2782+
2783+
def _ipython_display_(self):
2784+
"""Called by Jupyter notebook to display a brain."""
2785+
from IPython.display import display as idisplay
2786+
2787+
if mlab.options.offscreen:
2788+
# Render the mayavi scenes to the notebook
2789+
for figure in self._figures:
2790+
for scene in figure:
2791+
idisplay(scene.scene)
2792+
else:
2793+
# Render string representation
2794+
print(repr(self))
2795+
27792796

27802797
def _scale_sequential_lut(lut_table, fmin, fmid, fmax):
27812798
"""Scale a sequential colormap."""

0 commit comments

Comments
 (0)