Skip to content

Commit 51e2c7a

Browse files
committed
Do not include ndarray data in BufferGeometry repr
There was an tendency for the numpy array data to unreasonably pad the notebook size. This change prevents that.
1 parent 811d9fb commit 51e2c7a

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

pythreejs/core/BufferGeometry.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
from ipywidgets import register, widget_serialization
3-
from traitlets import validate, TraitError
4-
from ipydatawidgets import NDArrayWidget
3+
from traitlets import validate, TraitError, Undefined
4+
from ipydatawidgets import NDArrayWidget, get_union_array
55

66
from .BufferGeometry_autogen import BufferGeometry as BufferGeometryBase
77

@@ -30,3 +30,43 @@ def validate(self, proposal):
3030
raise TraitError('Index attribute must be a flat array. Consider using array.ravel().')
3131

3232
return value
33+
34+
35+
def _gen_repr_from_keys(self, keys):
36+
# Hide data in repr to avoid overly large datasets
37+
# Replace with uuids of buffer attributes
38+
class_name = self.__class__.__name__
39+
signature_parts = [
40+
'%s=%r' % (key, getattr(self, key))
41+
for key in keys if key not in ('attributes', 'morphAttributes')
42+
]
43+
for name in ('attributes', 'morphAttributes'):
44+
if not _dict_is_default(self, name):
45+
signature_parts.append('%s=%s' % (name, _attr_dict_repr(getattr(self, name))))
46+
return '%s(%s)' % (class_name, ', '.join(signature_parts))
47+
48+
49+
def _dict_is_default(ht, name):
50+
value = getattr(ht, name)
51+
return (
52+
getattr(ht.__class__, name).default_value == Undefined and
53+
(value is None or len(value) == 0)
54+
)
55+
56+
def _attr_value_repr(v):
57+
array = get_union_array(v.array)
58+
# Return full repr if array size is small:
59+
if array.size < 50:
60+
return repr(v)
61+
# Otherwise, return a summary:
62+
return '<%s shape=%r, dtype=%s>' % (v.__class__.__name__, array.shape, array.dtype)
63+
64+
def _attr_dict_repr(d):
65+
parts = []
66+
for key, value in d.items():
67+
if isinstance(value, tuple):
68+
value_parts = [_attr_value_repr(v) for v in value]
69+
else:
70+
value_parts = [_attr_value_repr(value)]
71+
parts.append('%r: %s' % (key, ', '.join(value_parts)))
72+
return '{%s}' % (', '.join(parts),)

0 commit comments

Comments
 (0)