Skip to content

Commit

Permalink
Merge from 2.x: PR #426
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Oct 31, 2022
2 parents 67c4e68 + 6fa5520 commit 07f2715
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion requirements/posix.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cloudpickle
ipykernel>=6.15.3,<7
ipykernel>=6.16.1,<7
ipython>=7.31.1,<8
jupyter_client>=7.3.4,<8
pyzmq>=22.1.0
Expand Down
2 changes: 1 addition & 1 deletion requirements/windows.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cloudpickle
ipykernel>=6.15.3,<7
ipykernel>=6.16.1,<7
ipython>=7.31.1,<8
jupyter_client>=7.3.4,<8
pyzmq>=22.1.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_version(module='spyder_kernels'):

REQUIREMENTS = [
'cloudpickle',
'ipykernel>=6.15.3,<7',
'ipykernel>=6.16.1,<7',
'ipython>=7.31.1,<8',
'jupyter-client>=7.3.4,<8',
'packaging',
Expand Down
71 changes: 33 additions & 38 deletions spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# Third-party imports
from ipykernel.ipkernel import IPythonKernel
from ipykernel import get_connection_info
from ipykernel import eventloops, get_connection_info
from traitlets.config.loader import LazyConfigValue
import zmq
from zmq.utils.garbage import gc
Expand Down Expand Up @@ -440,48 +440,43 @@ def get_mpl_interactive_backend(self):
"""
# Mapping from frameworks to backend names.
mapping = {
'qt': 'QtAgg', # For Matplotlib 3.5+
'qt5': 'Qt5Agg',
'qt': 'QtAgg',
'tk': 'TkAgg',
'macosx': 'MacOSX'
}

try:
# --- Get interactive framework
framework = None

# This is necessary because _get_running_interactive_framework
# can't detect Tk in a Jupyter kernel.
if hasattr(self, 'app_wrapper'):
if hasattr(self.app_wrapper, 'app'):
import tkinter
if isinstance(self.app_wrapper.app, tkinter.Tk):
framework = 'tk'

if framework is None:
try:
# This is necessary for Matplotlib 3.3.0+
from matplotlib import cbook
framework = cbook._get_running_interactive_framework()
except AttributeError:
# For older versions
from matplotlib import backends
framework = backends._get_running_interactive_framework()

# --- Return backend according to framework
if framework is None:
# Since no interactive backend has been set yet, this is
# equivalent to having the inline one.
return 0
elif framework in mapping:
return MPL_BACKENDS_TO_SPYDER[mapping[framework]]
# --- Get interactive framework
framework = None

# Detect if there is a graphical framework running by checking the
# eventloop function attached to the kernel.eventloop attribute (see
# `ipykernel.eventloops.enable_gui` for context).
from IPython.core.getipython import get_ipython
loop_func = get_ipython().kernel.eventloop

if loop_func is not None:
if loop_func == eventloops.loop_tk:
framework = 'tk'
elif loop_func == eventloops.loop_qt5:
framework = 'qt'
elif loop_func == eventloops.loop_cocoa:
framework = 'macosx'
else:
# This covers the case of other backends (e.g. Wx or Gtk)
# which users can set interactively with the %matplotlib
# magic but not through our Preferences.
return -1
except Exception:
return None
# Spyder doesn't handle other backends
framework = 'other'

# --- Return backend according to framework
if framework is None:
# Since no interactive backend has been set yet, this is
# equivalent to having the inline one.
return 0
elif framework in mapping:
return MPL_BACKENDS_TO_SPYDER[mapping[framework]]
else:
# This covers the case of other backends (e.g. Wx or Gtk)
# which users can set interactively with the %matplotlib
# magic but not through our Preferences.
return -1

def set_matplotlib_backend(self, backend, pylab=False):
"""Set matplotlib backend given a Spyder backend option."""
Expand Down

0 comments on commit 07f2715

Please sign in to comment.