Skip to content

Commit 9cf3d54

Browse files
authored
Merge pull request #15 from martinRenou/array_widget_support
Add support for NDArray widget
2 parents 631a515 + bfa139d commit 9cf3d54

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

ipygany/ipygany.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616
import vtk
1717

18-
from .serialization import array_serialization
18+
from .serialization import array_serialization, component_array_serialization
1919
from .vtk_loader import (
2020
load_vtk, FLOAT32, UINT32,
2121
get_ugrid_vertices, get_ugrid_triangles, get_ugrid_tetrahedrons, get_ugrid_data
@@ -44,7 +44,7 @@ class Component(_GanyWidgetBase):
4444
_model_name = Unicode('ComponentModel').tag(sync=True)
4545

4646
name = Unicode().tag(sync=True)
47-
array = Array(default_value=array(FLOAT32)).tag(sync=True, **array_serialization)
47+
array = Union((Instance(Widget), Array())).tag(sync=True, **component_array_serialization)
4848

4949
min = CFloat(allow_none=True, default_value=None)
5050
max = CFloat(allow_none=True, default_value=None)

ipygany/serialization.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import numpy as np
22

3+
from ipywidgets import Widget, widget_serialization
4+
35

46
def array_to_binary(ar, obj=None, force_contiguous=True):
57
if ar is None:
@@ -19,7 +21,18 @@ def json_to_array(json, obj=None):
1921
return np.array(json)
2022

2123

24+
def component_array_to_json(value, obj=None, force_contiguous=True):
25+
if isinstance(value, Widget):
26+
return widget_serialization['to_json'](value, obj)
27+
else:
28+
return array_to_binary(value, obj, force_contiguous)
29+
30+
2231
array_serialization = dict(
2332
to_json=array_to_binary,
2433
from_json=json_to_array
2534
)
35+
36+
component_array_serialization = dict(
37+
to_json=component_array_to_json
38+
)

src/widget.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ function deserialize_uint32array (data: any, manager: any) {
3535
return new Uint32Array(data.data.buffer);
3636
}
3737

38+
function deserialize_component_array (value: any, manager: any) {
39+
if (typeof value == 'string') {
40+
return unpack_models(value, manager);
41+
} else {
42+
return deserialize_float32array(value, manager);
43+
}
44+
}
45+
3846

3947
abstract class _GanyWidgetModel extends WidgetModel {
4048

@@ -81,19 +89,29 @@ class ComponentModel extends _GanyWidgetModel {
8189
};
8290
}
8391

92+
get array () {
93+
const array = this.get('array');
94+
95+
if (array.hasOwnProperty('name') && array.name == 'NDArrayModel') {
96+
return array.getNDArray().data;
97+
} else {
98+
return array;
99+
}
100+
}
101+
84102
initialize (attributes: any, options: any) {
85103
super.initialize(attributes, options);
86104

87-
this.component = new Component(this.get('name'), this.get('array'));
105+
this.component = new Component(this.get('name'), this.array);
88106

89-
this.on('change:array', () => { this.component.array = this.get('array'); });
107+
this.on('change:array', () => { this.component.array = this.array; });
90108
}
91109

92110
component: Component;
93111

94112
static serializers: ISerializers = {
95113
..._GanyWidgetModel.serializers,
96-
array: { deserialize: deserialize_float32array },
114+
array: { deserialize: deserialize_component_array },
97115
}
98116

99117
static model_name = 'ComponentModel';

0 commit comments

Comments
 (0)