Skip to content

Commit

Permalink
Add configure_inline_support and call it in the shell
Browse files Browse the repository at this point in the history
This removes a circular dependency between ipykernel and IPython.
  • Loading branch information
martinRenou committed Feb 9, 2021
1 parent aba2179 commit 23a5966
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
49 changes: 49 additions & 0 deletions 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 @@ -196,3 +197,51 @@ def _is_transparent(color):
"""Determine transparency from alpha."""
rgba = colors.to_rgba(color)
return rgba[3] < .5


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
9 changes: 9 additions & 0 deletions ipykernel/zmqshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
# Deprecated since ipykernel 4.3.0
from ipykernel.datapub import ZMQDataPublisher

from ipykernel.pylab.backend_inline import configure_inline_support

#-----------------------------------------------------------------------------
# Functions and classes
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -597,6 +599,13 @@ 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)

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 23a5966

Please sign in to comment.