Skip to content

Commit 1ee095d

Browse files
fix: empty lists were not serialized property, but send as empty ndarray
1 parent 6e1f088 commit 1ee095d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

ipyvolume/serialize.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ def array_sequence_to_binary_or_json(ar, obj=None):
154154
pass
155155
if isinstance(element, string_types):
156156
return array_to_json(ar)
157-
if dimension == 0: # scalars are passed as is (json)
158-
return element
157+
if dimension == 0: # scalars are passed as is (json), empty lists as well
158+
if isinstance(element, np.ndarray): # must be an empty list
159+
return []
160+
else:
161+
return element
159162
if isinstance(ar, (list, tuple, np.ndarray)): # ok, at least 1d
160163
if isinstance(ar[0], (list, tuple, np.ndarray)): # ok, 2d
161164
return [array_to_binary(ar[k]) for k in range(len(ar))]

ipyvolume/test_all.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ipyvolume.examples
55
import ipyvolume.datasets
66
import ipyvolume.utils
7+
import ipyvolume.serialize
78
import numpy as np
89
import os
910
import shutil
@@ -16,6 +17,19 @@
1617
shutil.rmtree("tmp")
1718
os.makedirs("tmp")
1819

20+
def test_serialize():
21+
assert ipyvolume.serialize.array_sequence_to_binary_or_json(1) == 1
22+
assert ipyvolume.serialize.array_sequence_to_binary_or_json([]) == []
23+
empty_array = np.array([])
24+
assert ipyvolume.serialize.array_sequence_to_binary_or_json(empty_array) == []
25+
assert type(ipyvolume.serialize.array_sequence_to_binary_or_json(empty_array)) == list
26+
27+
value = np.asarray(5)
28+
assert ipyvolume.serialize.array_sequence_to_binary_or_json(value) == 5
29+
30+
value = np.asarray(5)
31+
assert ipyvolume.serialize.array_sequence_to_binary_or_json(value) == 5
32+
1933
def test_figure():
2034
f1 = p3.figure()
2135
f2 = p3.figure(2)

0 commit comments

Comments
 (0)