File size: 1,976 Bytes
ba2f5d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from contextlib import contextmanager

import pytest

import altair.vegalite.v3 as alt


@contextmanager
def check_render_options(**options):
    """
    Context manager that will assert that alt.renderers.options are equivalent
    to the given options in the IPython.display.display call
    """
    import IPython.display

    def check_options(obj):
        assert alt.renderers.options == options

    _display = IPython.display.display
    IPython.display.display = check_options
    try:
        yield
    finally:
        IPython.display.display = _display


def test_check_renderer_options():
    # this test should pass
    with check_render_options():
        from IPython.display import display

        display(None)

    # check that an error is appropriately raised if the test fails
    with pytest.raises(AssertionError):
        with check_render_options(foo="bar"):
            from IPython.display import display

            display(None)


def test_display_options():
    chart = alt.Chart("data.csv").mark_point().encode(x="foo:Q")

    # check that there are no options by default
    with check_render_options():
        chart.display()

    # check that display options are passed
    with check_render_options(embed_options={"tooltip": False, "renderer": "canvas"}):
        chart.display("canvas", tooltip=False)

    # check that above options do not persist
    with check_render_options():
        chart.display()

    # check that display options augment rather than overwrite pre-set options
    with alt.renderers.enable(embed_options={"tooltip": True, "renderer": "svg"}):
        with check_render_options(embed_options={"tooltip": True, "renderer": "svg"}):
            chart.display()

        with check_render_options(
            embed_options={"tooltip": True, "renderer": "canvas"}
        ):
            chart.display("canvas")

    # check that above options do not persist
    with check_render_options():
        chart.display()