Debian Codegraph @

tests/plotting/jupyter/test_static.py

raw
1 from __future__ import annotations
2
3 import importlib.util
4 from typing import TYPE_CHECKING
5
6 import numpy as np
7 import pytest
8
9 import pyvista as pv
10 from pyvista.jupyter import _validate_jupyter_backend
11
12 if TYPE_CHECKING:
13 from pytest_mock import MockerFixture
14
15 has_ipython = bool(importlib.util.find_spec('IPython'))
16 if has_ipython:
17 from PIL.Image import Image
18
19
20 skip_no_ipython = pytest.mark.skipif(not has_ipython, reason='Requires IPython package')
21
22
23 def test_set_jupyter_backend_fail():
24 with pytest.raises(ValueError, match='Invalid Jupyter notebook plotting backend'):
25 pv.set_jupyter_backend('not a backend')
26
27
28 def test_validate_jupyter_backend_raises(mocker: MockerFixture):
29 from pyvista import jupyter
30
31 m = mocker.patch.object(jupyter, 'importlib')
32 m.util.find_spec.return_value = False
33 with pytest.raises(
34 ImportError, match=r'Install IPython to display with pyvista in a notebook.'
35 ):
36 _validate_jupyter_backend('foo')
37
38
39 def test_set_jupyter_backend_none():
40 pv.set_jupyter_backend('none')
41 assert pv.global_theme.jupyter_backend == 'none'
42
43
44 def test_set_jupyter_backend_none_resets_to_auto():
45 pv.set_jupyter_backend('static')
46 assert pv.global_theme.jupyter_backend == 'static'
47 pv.set_jupyter_backend(None)
48 assert pv.global_theme.jupyter_backend is None
49
50
51 @skip_no_ipython
52 def test_set_jupyter_backend_static():
53 pv.set_jupyter_backend('static')
54 assert pv.global_theme.jupyter_backend == 'static'
55 pv.set_jupyter_backend(None)
56
57
58 @skip_no_ipython
59 @pytest.mark.skip_plotting
60 @pytest.mark.parametrize('return_viewer', [True, False])
61 def test_static_from_show(sphere, return_viewer):
62 window_size = (100, 100)
63 image = sphere.plot(
64 window_size=window_size,
65 notebook=True,
66 jupyter_backend='static',
67 return_viewer=return_viewer,
68 )
69 if return_viewer:
70 assert isinstance(image, Image)
71 assert window_size == image.size
72
73
74 @skip_no_ipython
75 @pytest.mark.skip_plotting
76 def test_show_return_values(sphere: pv.PolyData):
77 # Three possible return values: (cpos, image, widget)
78 img, viewer = sphere.plot(
79 notebook=True,
80 return_viewer=True,
81 return_cpos=False,
82 return_img=True,
83 jupyter_backend='static',
84 )
85 assert isinstance(img, np.ndarray)
86 assert isinstance(viewer, Image)
87
88 cpos, img, viewer = sphere.plot(
89 notebook=True,
90 return_viewer=True,
91 return_cpos=True,
92 return_img=True,
93 jupyter_backend='static',
94 )
95 assert isinstance(cpos, pv.CameraPosition)
96 assert isinstance(img, np.ndarray)
97 assert isinstance(viewer, Image)