10
10
11
11
import ipywidgets as widgets
12
12
from ipywidgets import Color
13
- from ipywidgets .widgets .trait_types import TypedTuple
13
+ from ipywidgets .widgets .trait_types import TypedTuple , ByteMemoryView
14
14
from ipywidgets .widgets .widget_link import Link
15
15
16
16
HEADER = '''# Model State
40
40
}
41
41
42
42
43
- def widget_type ( widget , widget_list ):
43
+ def trait_type ( trait , widget_list ):
44
44
attributes = {}
45
- if isinstance (widget , CaselessStrEnum ):
45
+ if isinstance (trait , CaselessStrEnum ):
46
46
w_type = 'string'
47
- attributes ['enum' ] = widget .values
48
- elif isinstance (widget , Unicode ):
47
+ attributes ['enum' ] = trait .values
48
+ elif isinstance (trait , Unicode ):
49
49
w_type = 'string'
50
- elif isinstance (widget , (Tuple , List )):
50
+ elif isinstance (trait , (Tuple , List )):
51
51
w_type = 'array'
52
- elif isinstance (widget , TypedTuple ):
52
+ elif isinstance (trait , TypedTuple ):
53
53
w_type = 'array'
54
- attributes ['items' ] = widget_type ( widget ._trait , widget_list )
55
- elif isinstance (widget , Bool ):
54
+ attributes ['items' ] = trait_type ( trait ._trait , widget_list )
55
+ elif isinstance (trait , Bool ):
56
56
w_type = 'bool'
57
- elif isinstance (widget , (CFloat , Float )):
57
+ elif isinstance (trait , (CFloat , Float )):
58
58
w_type = 'float'
59
- elif isinstance (widget , (CInt , Int )):
59
+ elif isinstance (trait , (CInt , Int )):
60
60
w_type = 'int'
61
- elif isinstance (widget , Color ):
61
+ elif isinstance (trait , Color ):
62
62
w_type = 'color'
63
- elif isinstance (widget , Dict ):
63
+ elif isinstance (trait , Dict ):
64
64
w_type = 'object'
65
- elif isinstance (widget , Bytes ):
65
+ elif isinstance (trait , Bytes ) or isinstance ( trait , ByteMemoryView ):
66
66
w_type = 'bytes'
67
- elif isinstance (widget , Instance ) and issubclass (widget .klass ,
67
+ elif isinstance (trait , Instance ) and issubclass (trait .klass ,
68
68
widgets .Widget ):
69
69
w_type = 'reference'
70
- attributes ['widget' ] = widget .klass .__name__
70
+ attributes ['widget' ] = trait .klass .__name__
71
71
# ADD the widget to this documenting list
72
- if (widget .klass not in [i [1 ] for i in widget_list ]
73
- and widget .klass is not widgets .Widget ):
74
- widget_list .append ((widget .klass .__name__ , widget .klass ))
75
- elif isinstance (widget , Any ):
72
+ if (trait .klass not in [i [1 ] for i in widget_list ]
73
+ and trait .klass is not widgets .Widget ):
74
+ widget_list .append ((trait .klass .__name__ , trait .klass ))
75
+ elif isinstance (trait , Any ):
76
76
# In our case, these all happen to be values that are converted to
77
77
# strings
78
78
w_type = 'label'
79
79
else :
80
- w_type = widget .__class__ .__name__
80
+ w_type = trait .__class__ .__name__
81
81
attributes ['type' ] = w_type
82
- if widget .allow_none :
82
+ if trait .allow_none :
83
83
attributes ['allow_none' ] = True
84
84
return attributes
85
85
@@ -91,7 +91,7 @@ def jsdefault(trait):
91
91
return 'reference to new instance'
92
92
else :
93
93
default = trait .default_value
94
- if isinstance (default , bytes ):
94
+ if isinstance (default , bytes ) or isinstance ( default , memoryview ) :
95
95
default = trait .default_value_repr ()
96
96
return default
97
97
@@ -163,7 +163,7 @@ def jsonify(identifier, widget, widget_list):
163
163
help = trait .help or '' ,
164
164
default = jsdefault (trait )
165
165
)
166
- attribute .update (widget_type (trait , widget_list ))
166
+ attribute .update (trait_type (trait , widget_list ))
167
167
attributes .append (attribute )
168
168
169
169
return dict (model = model , view = view , attributes = attributes )
0 commit comments