1 7
import numpy as np
2 7
import pytest
3

4 7
try:
5 7
    import pydeck
6 7
except Exception:
7 7
    pydeck = None
8

9 7
pydeck_available = pytest.mark.skipif(pydeck is None, reason="requires pydeck")
10

11 7
from panel.models.deckgl import DeckGLPlot
12 7
from panel.pane import Pane, PaneBase, DeckGL
13

14

15 7
@pydeck_available
16 2
def test_get_pydeck_pane_type_from_deck():
17 0
    deck = pydeck.Deck()
18 0
    assert PaneBase.get_pane_type(deck) is DeckGL
19

20

21 7
@pydeck_available
22 2
def test_pydeck_pane_deck(document, comm):
23 0
    deck = pydeck.Deck(tooltip=True, mapbox_key='ABC')
24 0
    pane = Pane(deck)
25

26
    # Create pane
27 0
    model = pane.get_root(document, comm=comm)
28 0
    assert isinstance(model, DeckGLPlot)
29 0
    assert pane._models[model.ref["id"]][0] is model
30 0
    assert model.data == {
31
        'description': None,
32
        'mapStyle': 'mapbox://styles/mapbox/dark-v9',
33
        'views': [{'@@type': 'MapView', 'controller': True}]
34
    }
35 0
    assert model.mapbox_api_key == deck.mapbox_key
36 0
    assert model.tooltip == deck.deck_widget.tooltip
37

38
    # Replace Pane.object
39 0
    new_deck = pydeck.Deck(tooltip=False)
40 0
    pane.object = new_deck
41

42 0
    assert pane._models[model.ref["id"]][0] is model
43 0
    assert model.tooltip == new_deck.deck_widget.tooltip
44

45
    # Cleanup
46 0
    pane._cleanup(model)
47 0
    assert pane._models == {}
48

49

50 7
def test_deckgl_empty_constructor(document, comm):
51 7
    pane = DeckGL()
52

53 7
    model = pane.get_root(document, comm)
54

55 7
    assert model.layers == []
56 7
    assert model.initialViewState == {}
57 7
    assert model.data == {}
58 7
    assert model.data_sources == []
59

60
    # Cleanup
61 7
    pane._cleanup(model)
62 7
    assert pane._models == {}
63

64

65 7
def test_deckgl_construct_layer(document, comm):
66 7
    pane = DeckGL({'layers': [{'data': [{'a': 1, 'b': 2}, {'a': 3, 'b': 7}]}]})
67

68 7
    model = pane.get_root(document, comm)
69

70 7
    assert model.layers == [{'data': 0}]
71 7
    assert len(model.data_sources) == 1
72 7
    data = model.data_sources[0].data
73 7
    assert np.array_equal(data['a'], np.array([1, 3]))
74 7
    assert np.array_equal(data['b'], np.array([2, 7]))
75

76

77 7
def test_deckgl_update_layer(document, comm):
78 7
    layer = {'data': [{'a': 1, 'b': 2}, {'a': 3, 'b': 7}]}
79 7
    pane = DeckGL({'layers': [layer]})
80

81 7
    model = pane.get_root(document, comm)
82

83 7
    cds = model.data_sources[0]
84 7
    old_data = cds.data
85 7
    a_vals = cds.data['a']
86 7
    layer['data'] = [{'a': 1, 'b': 3}, {'a': 3, 'b': 9}]
87 7
    pane.param.trigger('object')
88

89 7
    assert cds.data['a'] is a_vals
90 7
    assert cds.data is old_data
91 7
    assert np.array_equal(cds.data['b'], np.array([3, 9]))
92

93

94 7
def test_deckgl_update_layer_columns(document, comm):
95 7
    layer = {'data': [{'a': 1, 'b': 2}, {'a': 3, 'b': 7}]}
96 7
    pane = DeckGL({'layers': [layer]})
97

98 7
    model = pane.get_root(document, comm)
99

100 7
    cds = model.data_sources[0]
101 7
    old_data = cds.data
102 7
    layer['data'] = [{'c': 1, 'b': 3}, {'c': 3, 'b': 9}]
103 7
    pane.param.trigger('object')
104

105 7
    assert 'a' not in cds.data
106 7
    assert cds.data is not old_data
107 7
    assert np.array_equal(cds.data['b'], np.array([3, 9]))
108 7
    assert np.array_equal(cds.data['c'], np.array([1, 3]))
109

110

111 7
def test_deckgl_append_layer(document, comm):
112 7
    layer = {'data': [{'a': 1, 'b': 2}, {'a': 3, 'b': 7}]}
113 7
    pane = DeckGL({'layers': [layer]})
114

115 7
    model = pane.get_root(document, comm)
116

117 7
    pane.object['layers'].append({'data': [{'c': 1, 'b': 3}, {'c': 3, 'b': 9}]})
118 7
    pane.param.trigger('object')
119

120 7
    assert len(model.layers) == 2
121 7
    assert len(model.data_sources) == 2
122 7
    cds1, cds2 = model.data_sources
123 7
    old_data = cds1.data
124 7
    a_vals, b_vals = old_data['a'], old_data['b']
125 7
    layer1, layer2 = model.layers
126 7
    assert layer1['data'] == 0
127 7
    assert layer2['data'] == 1
128

129 7
    assert cds1.data is old_data
130 7
    assert cds1.data['a'] is a_vals
131 7
    assert cds1.data['b'] is b_vals
132 7
    assert np.array_equal(cds2.data['b'], np.array([3, 9]))
133 7
    assert np.array_equal(cds2.data['c'], np.array([1, 3]))
134

135

136 7
def test_deckgl_insert_layer(document, comm):
137 7
    layer = {'data': [{'a': 1, 'b': 2}, {'a': 3, 'b': 7}]}
138 7
    pane = DeckGL({'layers': [layer]})
139

140 7
    model = pane.get_root(document, comm)
141

142 7
    pane.object['layers'].insert(0, {'data': [{'c': 1, 'b': 3}, {'c': 3, 'b': 9}]})
143 7
    pane.param.trigger('object')
144

145 7
    assert len(model.layers) == 2
146 7
    assert len(model.data_sources) == 2
147 7
    cds1, cds2 = model.data_sources
148 7
    old_data = cds1.data
149 7
    a_vals, b_vals = old_data['a'], old_data['b']
150 7
    layer1, layer2 = model.layers
151 7
    assert layer1['data'] == 1
152 7
    assert layer2['data'] == 0
153

154 7
    assert cds1.data is old_data
155 7
    assert cds1.data['a'] is a_vals
156 7
    assert cds1.data['b'] is b_vals
157 7
    assert np.array_equal(cds2.data['b'], np.array([3, 9]))
158 7
    assert np.array_equal(cds2.data['c'], np.array([1, 3]))

Read our documentation on viewing source code .

Loading