Skip to content

Commit

Permalink
Merge pull request #590 from martinRenou/add_configure_inline_support
Browse files Browse the repository at this point in the history
Add configure_inline_support and call it in the shell
  • Loading branch information
blink1073 authored Feb 9, 2021
2 parents aba2179 + 3e69ce7 commit da44c7c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
52 changes: 51 additions & 1 deletion ipykernel/pylab/backend_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from matplotlib._pylab_helpers import Gcf

from IPython.core.getipython import get_ipython
from IPython.core.pylabtools import select_figure_formats
from IPython.display import display

from .config import InlineBackend
Expand Down Expand Up @@ -150,13 +151,62 @@ def flush_figures():
# See https://github.com/matplotlib/matplotlib/pull/1125
FigureCanvas = FigureCanvasAgg


def configure_inline_support(shell, backend):
"""Configure an IPython shell object for matplotlib use.
Parameters
----------
shell : InteractiveShell instance
backend : matplotlib backend
"""
# If using our svg payload backend, register the post-execution
# function that will pick up the results for display. This can only be
# done with access to the real shell object.

cfg = InlineBackend.instance(parent=shell)
cfg.shell = shell
if cfg not in shell.configurables:
shell.configurables.append(cfg)

if backend == 'module://ipykernel.pylab.backend_inline':
shell.events.register('post_execute', flush_figures)

# Save rcParams that will be overwrittern
shell._saved_rcParams = {}
for k in cfg.rc:
shell._saved_rcParams[k] = matplotlib.rcParams[k]
# load inline_rc
matplotlib.rcParams.update(cfg.rc)
new_backend_name = "inline"
else:
try:
shell.events.unregister('post_execute', flush_figures)
except ValueError:
pass
if hasattr(shell, '_saved_rcParams'):
matplotlib.rcParams.update(shell._saved_rcParams)
del shell._saved_rcParams
new_backend_name = "other"

# only enable the formats once -> don't change the enabled formats (which the user may
# has changed) when getting another "%matplotlib inline" call.
# See https://github.com/ipython/ipykernel/issues/29
cur_backend = getattr(configure_inline_support, "current_backend", "unset")
if new_backend_name != cur_backend:
# Setup the default figure format
select_figure_formats(shell, cfg.figure_formats, **cfg.print_figure_kwargs)
configure_inline_support.current_backend = new_backend_name


def _enable_matplotlib_integration():
"""Enable extra IPython matplotlib integration when we are loaded as the matplotlib backend."""
from matplotlib import get_backend
ip = get_ipython()
backend = get_backend()
if ip and backend == 'module://%s' % __name__:
from IPython.core.pylabtools import configure_inline_support, activate_matplotlib
from IPython.core.pylabtools import activate_matplotlib
try:
activate_matplotlib(backend)
configure_inline_support(ip, backend)
Expand Down
9 changes: 9 additions & 0 deletions ipykernel/zmqshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,15 @@ def init_magics(self):
self.register_magics(KernelMagics)
self.magics_manager.register_alias('ed', 'edit')

def enable_matplotlib(self, gui=None):
gui, backend = super(ZMQInteractiveShell, self).enable_matplotlib(gui)

from ipykernel.pylab.backend_inline import configure_inline_support

configure_inline_support(self, backend)

return gui, backend

def init_virtualenv(self):
# Overridden not to do virtualenv detection, because it's probably
# not appropriate in a kernel. To use a kernel in a virtualenv, install
Expand Down

0 comments on commit da44c7c

Please sign in to comment.