Skip to content

Commit

Permalink
Merge pull request spyder-ide#22057 from ccordoba12/fix-mpl-statusbar…
Browse files Browse the repository at this point in the history
…-widget

PR: Make Matplotlib status bar widget show the right backend and fix some errors related to kernel restarts for options that require them
  • Loading branch information
ccordoba12 authored May 6, 2024
2 parents 3ce32d6 + eed7b5f commit 071e646
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 142 deletions.
4 changes: 2 additions & 2 deletions external-deps/spyder-kernels/.gitrepo

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion external-deps/spyder-kernels/spyder_kernels/comms/utils.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 13 additions & 33 deletions external-deps/spyder-kernels/spyder_kernels/console/kernel.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion external-deps/spyder-kernels/spyder_kernels/console/shell.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions spyder/plugins/ipythonconsole/confpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ def setup_page(self):
pylab_group = QGroupBox(_("Support for graphics (Matplotlib)"))
pylab_box = newcb(_("Activate support"), 'pylab')
autoload_pylab_box = newcb(
_("Automatically load Pylab and NumPy modules"),
_("Automatically load Matplotlib and NumPy modules"),
'pylab/autoload',
tip=_("This lets you load graphics support without importing\n"
"the commands to do plots. Useful to work with other\n"
"plotting libraries different to Matplotlib or to develop\n"
"GUIs with Spyder."))
tip=_(
"This lets you generate graphics and work with arrays "
"<b>without</b> importing the commands to do it. It's also "
"useful to develop GUIs with Spyder."
)
)
autoload_pylab_box.setEnabled(self.get_option('pylab'))
pylab_box.checkbox.toggled.connect(autoload_pylab_box.setEnabled)

Expand All @@ -116,19 +118,13 @@ def setup_page(self):
inline = _("Inline")
automatic = _("Automatic")
backend_group = QGroupBox(_("Graphics backend"))
bend_label = QLabel(_("Decide how graphics are going to be displayed "
"in the console. If unsure, please select "
"<b>%s</b> to put graphics inside the "
"console or <b>%s</b> to interact with "
"them (through zooming and panning) in a "
"separate window.") % (inline, automatic))
bend_label.setWordWrap(True)
bend_label = QLabel(_("Decide how Matplotlib graphics are displayed"))

backends = [
(inline, 'inline'),
(automatic, 'auto'),
("Qt5", 'qt'),
("Tkinter", 'tk')
("Qt", 'qt'),
("Tk", 'tk')
]

if sys.platform == 'darwin':
Expand All @@ -139,8 +135,12 @@ def setup_page(self):
_("Backend:") + " ",
backends,
'pylab/backend', default='inline',
tip=_("This option will be applied the next time a console is "
"opened."))
tip=_(
"If unsure, select <b>%s</b> to put graphics in the Plots "
"pane or <b>%s</b> to interact with them (through zooming and "
"panning) in a separate window."
) % (inline, automatic)
)

backend_layout = QVBoxLayout()
backend_layout.addWidget(bend_label)
Expand Down
1 change: 1 addition & 0 deletions spyder/plugins/ipythonconsole/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def get_plugin(name):
debugger.on_ipython_console_available()
console.on_initialize()
console._register()
console.get_widget().matplotlib_status.register_ipythonconsole(console)
console.create_new_client(
special=special,
given_name=given_name,
Expand Down
60 changes: 52 additions & 8 deletions spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

# Third party imports
from ipykernel._version import __version__ as ipykernel_version
import IPython
from IPython.core import release as ipy_release
from IPython.core.application import get_ipython_dir
from flaky import flaky
Expand Down Expand Up @@ -1909,18 +1908,62 @@ def test_pdb_comprehension_namespace(ipyconsole, qtbot, tmpdir):
assert "_spyderpdb" not in key


@flaky(max_runs=3)
@flaky(max_runs=10)
@pytest.mark.auto_backend
def test_restart_intertactive_backend(ipyconsole, qtbot):
@pytest.mark.skipif(os.name == 'nt', reason="Fails on windows")
def test_restart_interactive_backend(ipyconsole, qtbot):
"""
Test that we ask for a restart after switching to a different interactive
backend in preferences.
Test that we ask for a restart or not after switching to different
interactive backends.
Also, check that we show the right backend in the Matplotlib status widget.
"""
main_widget = ipyconsole.get_widget()
qtbot.wait(1000)
main_widget.change_possible_restart_and_mpl_conf('pylab/backend', 'tk')
shell = ipyconsole.get_current_shellwidget()
matplotlib_status = ipyconsole.get_widget().matplotlib_status

# This is necessary to test no spurious messages are printed to the console
shell.clear_console()
qtbot.waitUntil(lambda: '\nIn [2]: ' == shell._control.toPlainText())

# Switch to the tk backend
ipyconsole.set_conf('pylab/backend', 'tk')
assert bool(os.environ.get('BACKEND_REQUIRE_RESTART'))
assert shell.get_matplotlib_backend() == "qt"
assert matplotlib_status.value == "Qt"

# Switch to the inline backend
os.environ.pop('BACKEND_REQUIRE_RESTART')
ipyconsole.set_conf('pylab/backend', 'inline')
assert not bool(os.environ.get('BACKEND_REQUIRE_RESTART'))
qtbot.waitUntil(lambda: shell.get_matplotlib_backend() == "inline")
assert matplotlib_status.value == "Inline"

# Switch to the auto backend
ipyconsole.set_conf('pylab/backend', 'auto')
assert not bool(os.environ.get('BACKEND_REQUIRE_RESTART'))
qtbot.waitUntil(lambda: shell.get_matplotlib_backend() == "qt")
assert matplotlib_status.value == "Qt"

# Switch to the qt backend
ipyconsole.set_conf('pylab/backend', 'qt')
assert not bool(os.environ.get('BACKEND_REQUIRE_RESTART'))
assert matplotlib_status.value == "Qt"

# Switch to the tk backend again
ipyconsole.set_conf('pylab/backend', 'tk')
assert bool(os.environ.get('BACKEND_REQUIRE_RESTART'))

# Check we no spurious messages are shown before the restart below
assert "\nIn [2]: " == shell._control.toPlainText()

# Restart kernel to check if the new interactive backend is set
ipyconsole.restart_kernel()
qtbot.waitUntil(lambda: shell.spyder_kernel_ready, timeout=SHELL_TIMEOUT)
qtbot.wait(SHELL_TIMEOUT)
qtbot.waitUntil(lambda: shell.get_matplotlib_backend() == "tk")
assert shell.get_mpl_interactive_backend() == "tk"
assert matplotlib_status.value == "Tk"


def test_mpl_conf(ipyconsole, qtbot):
"""
Expand Down Expand Up @@ -2051,6 +2094,7 @@ def test_startup_run_lines_project_directory(ipyconsole, qtbot, tmpdir):
qtbot.waitUntil(
lambda: shell.spyder_kernel_ready and shell._prompt_html is not None,
timeout=SHELL_TIMEOUT)
qtbot.wait(500)
assert shell.get_value('pi')

# Reset config for the 'spyder_pythonpath' and 'startup/run_lines'
Expand Down
Loading

0 comments on commit 071e646

Please sign in to comment.