File: test_static.py

package info (click to toggle)
python-pyvista 0.46.3-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 177,564 kB
  • sloc: python: 94,482; sh: 129; makefile: 70
file content (91 lines) | stat: -rw-r--r-- 2,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import annotations

import importlib.util
from typing import TYPE_CHECKING

import numpy as np
import pytest

import pyvista as pv
from pyvista.jupyter import _validate_jupyter_backend

if TYPE_CHECKING:
    from pytest_mock import MockerFixture

has_ipython = bool(importlib.util.find_spec('IPython'))
if has_ipython:
    from PIL.Image import Image


skip_no_ipython = pytest.mark.skipif(not has_ipython, reason='Requires IPython package')


def test_set_jupyter_backend_fail():
    with pytest.raises(ValueError, match='Invalid Jupyter notebook plotting backend'):
        pv.set_jupyter_backend('not a backend')


def test_validate_jupyter_backend_raises(mocker: MockerFixture):
    from pyvista import jupyter

    m = mocker.patch.object(jupyter, 'importlib')
    m.util.find_spec.return_value = False
    with pytest.raises(
        ImportError, match='Install IPython to display with pyvista in a notebook.'
    ):
        _validate_jupyter_backend('foo')


@pytest.mark.parametrize('backend', [None, 'none'])
def test_set_jupyter_backend_none(backend):
    pv.set_jupyter_backend(backend)
    assert pv.global_theme.jupyter_backend == 'none'


@skip_no_ipython
def test_set_jupyter_backend_static():
    pv.set_jupyter_backend('static')
    assert pv.global_theme.jupyter_backend == 'static'
    pv.set_jupyter_backend(None)


@skip_no_ipython
@pytest.mark.skip_plotting
@pytest.mark.parametrize('return_viewer', [True, False])
def test_static_from_show(sphere, return_viewer):
    window_size = (100, 100)
    image = sphere.plot(
        window_size=window_size,
        notebook=True,
        jupyter_backend='static',
        return_viewer=return_viewer,
    )
    if return_viewer:
        assert isinstance(image, Image)
        assert window_size == image.size


@skip_no_ipython
@pytest.mark.skip_plotting
def test_show_return_values(sphere: pv.PolyData):
    # Three possible return values: (cpos, image, widget)
    img, viewer = sphere.plot(
        notebook=True,
        return_viewer=True,
        return_cpos=False,
        return_img=True,
        jupyter_backend='static',
    )
    assert isinstance(img, np.ndarray)
    assert isinstance(viewer, Image)

    cpos, img, viewer = sphere.plot(
        notebook=True,
        return_viewer=True,
        return_cpos=True,
        return_img=True,
        jupyter_backend='static',
    )
    assert isinstance(cpos, pv.CameraPosition)
    assert isinstance(img, np.ndarray)
    assert isinstance(viewer, Image)