Skip to content

Commit

Permalink
Remove hack to apply rcParams while in inline mode.
Browse files Browse the repository at this point in the history
Apply rcParams explicitly while in inline mode.
Always reset rcParams to file defaults before applying backend. This will ensure correct rcParams in interactive backends if rcParams are explicitly set in inline mode. When changing to inline mode, InlineBackend settings will be applied to rcParams.
  • Loading branch information
mrclary committed May 29, 2024
1 parent 6b88c6d commit 89dc88b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
43 changes: 25 additions & 18 deletions spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,27 +580,12 @@ def set_matplotlib_conf(self, conf):
'print_figure_kwargs', {'bbox_inches': bbox_inches}
)

# To update rcParams in inline mode we can either do so directly or
# re-assert inline mode. However, either will prevent restoring
# rcParams when toggling to interactive mode. The workaround is to
# first toggle to interactive mode, then back to inline. This updates
# the rcParams and restores rcParams when switching to interactive.
interactive_backend = self.get_mpl_interactive_backend()
current_backend = self.get_matplotlib_backend()
# Only update backend if it has changed or if autoloading pylab.
pylab_autoload_o = conf.get(pylab_autoload_n, False)
current_backend = self.get_matplotlib_backend()
pylab_backend_o = conf.get(pylab_backend_n, current_backend)
backend_changed = current_backend != pylab_backend_o
if (
current_backend == 'inline' and inline_rc
and not backend_changed
and interactive_backend not in ('inline', -1)
):
self._set_mpl_backend(interactive_backend)
if (
(current_backend == 'inline' and inline_rc) # toggle back to inline
or pylab_autoload_o
or backend_changed
):
if pylab_autoload_o or backend_changed:
self._set_mpl_backend(pylab_backend_o, pylab_autoload_o)

# -- For completions
Expand Down Expand Up @@ -986,6 +971,28 @@ def _set_inline_config_option(self, option, value):

self._set_config_option(f'InlineBackend.{option}', value)

if option == 'rc' and self.get_matplotlib_backend() == 'inline':
# Explicitly update rcParams if already in inline mode so that
# new settings are effective immediately.
import matplotlib
matplotlib.rcParams.update(value)

def _restore_rc_file_defaults(self):
"""Restore inline rcParams to file defaults"""
try:
import matplotlib
except:
return

if (
'InlineBackend' in self.config
and 'rc' in self.config['InlineBackend']
):
# Only restore keys that may have been set explicitly by
# _set_inline_config_option
for k in self.config['InlineBackend']['rc'].keys():
matplotlib.rcParams[k] = matplotlib.rcParamsOrig[k]

def set_sympy_forecolor(self, background_color='dark'):
"""Set SymPy forecolor depending on console background."""
if self.shell.special != "sympy":
Expand Down
4 changes: 4 additions & 0 deletions spyder_kernels/console/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def enable_matplotlib(self, gui=None):
if gui is None or gui.lower() == "auto":
gui = automatic_backend()

# Before activating the backend, restore to file default those
# InlineBackend settings that may have been set explicitly.
self.kernel._restore_rc_file_defaults()

enabled_gui, backend = super().enable_matplotlib(gui)

# This is necessary for IPython 8.24+, which returns None after
Expand Down

0 comments on commit 89dc88b

Please sign in to comment.