Skip to content

Commit

Permalink
Figure.show: Raise ImportError instead of GMTError if IPython is not …
Browse files Browse the repository at this point in the history
…installed but required (#3580)
  • Loading branch information
seisman committed Nov 19, 2024
1 parent 82f811e commit 44f9447
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
16 changes: 9 additions & 7 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import numpy as np
from pygmt.clib import Session
from pygmt.exceptions import GMTError, GMTInvalidInput
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import launch_external_viewer, unique_name


Expand Down Expand Up @@ -331,11 +331,12 @@ def show(
match method:
case "notebook":
if not _HAS_IPYTHON:
raise GMTError(
msg = (
"Notebook display is selected, but IPython is not available. "
"Make sure you have IPython installed, "
"or run the script in a Jupyter notebook."
)
raise ImportError(msg)
png = self._preview(
fmt="png", dpi=dpi, anti_alias=True, as_bytes=True, **kwargs
)
Expand All @@ -344,14 +345,15 @@ def show(
pdf = self._preview(
fmt="pdf", dpi=dpi, anti_alias=False, as_bytes=False, **kwargs
)
launch_external_viewer(pdf, waiting=waiting) # type: ignore[arg-type]
launch_external_viewer(pdf, waiting=waiting)
case "none":
pass # Do nothing
case _:
raise GMTInvalidInput(
f"Invalid display method '{method}'. Valid values are 'external', "
"'notebook', 'none' or None."
msg = (
f"Invalid display method '{method}'. "
"Valid values are 'external', 'notebook', 'none' or None."
)
raise GMTInvalidInput(msg)

def _preview(self, fmt: str, dpi: int, as_bytes: bool = False, **kwargs):
"""
Expand Down Expand Up @@ -400,7 +402,7 @@ def _repr_html_(self):
html = '<img src="data:image/png;base64,{image}" width="{width}px">'
return html.format(image=base64_png.decode("utf-8"), width=500)

from pygmt.src import ( # type: ignore [misc]
from pygmt.src import ( # type: ignore[misc]
basemap,
coast,
colorbar,
Expand Down
6 changes: 3 additions & 3 deletions pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy.testing as npt
import pytest
from pygmt import Figure, set_display
from pygmt.exceptions import GMTError, GMTInvalidInput
from pygmt.exceptions import GMTInvalidInput
from pygmt.figure import SHOW_CONFIG, _get_default_display_method
from pygmt.helpers import GMTTempFile

Expand Down Expand Up @@ -321,7 +321,7 @@ def test_figure_show_notebook_error_without_ipython():
"""
fig = Figure()
fig.basemap(region=[0, 1, 2, 3], frame=True)
with pytest.raises(GMTError):
with pytest.raises(ImportError):
fig.show(method="notebook")


Expand Down Expand Up @@ -361,7 +361,7 @@ def test_set_display(self):
assert mock_viewer.call_count == 0
assert mock_display.call_count == 1
else:
with pytest.raises(GMTError):
with pytest.raises(ImportError):
fig.show()

# Test the "external" display method
Expand Down

0 comments on commit 44f9447

Please sign in to comment.