Skip to content

Commit

Permalink
Make MatplotlibViewer.plot() animate in jupyter notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
guyer committed Sep 17, 2018
1 parent 75cc4f5 commit 10bb2f8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions fipy/viewers/matplotlibViewer/matplotlibViewer.py
Expand Up @@ -40,6 +40,24 @@

from fipy.viewers.viewer import AbstractViewer

def _isnotebook():
"""return True if running in a jupyter notebook
https://stackoverflow.com/a/39662359/2019542
"""
try:
import IPython

shell = IPython.get_ipython().__class__.__name__
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
return False # Other type (?)
except (ImportError, NameError):
return False # Probably standard Python interpreter

class AbstractMatplotlibViewer(AbstractViewer):
"""
.. attention:: This class is abstract. Always create one of its subclasses.
Expand Down Expand Up @@ -158,6 +176,14 @@ def plot(self, filename = None):
if filename is not None:
pylab.savefig(filename)

if _isnotebook():
# plots don't animate in the notebook unless we
# explicitly clear_output and display
from IPython.display import display, clear_output

clear_output(wait=True)
display(self)

def _validFileExtensions(self):
import pylab
return ["""
Expand Down

0 comments on commit 10bb2f8

Please sign in to comment.