From ccfb4a97c0f620e6a93d656fe7fe3cec5e9fc909 Mon Sep 17 00:00:00 2001 From: Ryan Clary <9618975+mrclary@users.noreply.github.com> Date: Wed, 29 May 2024 13:06:54 -0700 Subject: [PATCH] Remove hack to apply rcParams while in inline mode. 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. --- spyder_kernels/console/kernel.py | 43 +++++++++++++++++++------------- spyder_kernels/console/shell.py | 4 +++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/spyder_kernels/console/kernel.py b/spyder_kernels/console/kernel.py index b7683dde..7d75fac5 100644 --- a/spyder_kernels/console/kernel.py +++ b/spyder_kernels/console/kernel.py @@ -582,27 +582,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 @@ -988,6 +973,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 Exception: + 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": diff --git a/spyder_kernels/console/shell.py b/spyder_kernels/console/shell.py index 5a9c1a29..ad93ad7e 100644 --- a/spyder_kernels/console/shell.py +++ b/spyder_kernels/console/shell.py @@ -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