9
9
import inspect
10
10
from importlib import import_module
11
11
12
- OBJECT_REF = ' objectRef'
13
- FUNCTION_REF = ' functionRef'
12
+ OBJECT_REF = " objectRef"
13
+ FUNCTION_REF = " functionRef"
14
14
15
15
16
16
class Events (object ):
@@ -23,94 +23,98 @@ def resolve_ref(value):
23
23
if isinstance (value , dict ):
24
24
if OBJECT_REF in value .keys ():
25
25
obj = getattr (self , value [OBJECT_REF ])
26
- for path_item in value .get (' path' , []):
26
+ for path_item in value .get (" path" , []):
27
27
obj = obj [path_item ]
28
28
return obj
29
29
if FUNCTION_REF in value .keys ():
30
30
fn = getattr (self , value [FUNCTION_REF ])
31
- args = value .get (' args' , [])
32
- kwargs = value .get (' kwargs' , {})
31
+ args = value .get (" args" , [])
32
+ kwargs = value .get (" kwargs" , {})
33
33
return fn (* args , ** kwargs )
34
34
return value
35
35
36
- if ' create_widget' in content .keys ():
37
- module_name = content [' create_widget' ][0 ]
38
- class_name = content [' create_widget' ][1 ]
39
- props = {k : resolve_ref (v ) for k , v in content [' props' ].items ()}
36
+ if " create_widget" in content .keys ():
37
+ module_name = content [" create_widget" ][0 ]
38
+ class_name = content [" create_widget" ][1 ]
39
+ props = {k : resolve_ref (v ) for k , v in content [" props" ].items ()}
40
40
module = import_module (module_name )
41
- widget = getattr (module , class_name )(** props , model_id = content ['id' ])
41
+ widget = getattr (module , class_name )(** props , model_id = content ["id" ])
42
42
self ._component_instances = [* self ._component_instances , widget ]
43
- elif ' update_ref' in content .keys ():
44
- widget = DOMWidget .widgets [content ['id' ]]
45
- prop = content [' prop' ]
46
- obj = resolve_ref (content [' update_ref' ])
43
+ elif " update_ref" in content .keys ():
44
+ widget = DOMWidget .widgets [content ["id" ]]
45
+ prop = content [" prop" ]
46
+ obj = resolve_ref (content [" update_ref" ])
47
47
setattr (widget , prop , obj )
48
- elif 'destroy_widget' in content .keys ():
49
- self ._component_instances = [w for w in self ._component_instances
50
- if w .model_id != content ['destroy_widget' ]]
51
- elif 'event' in content .keys ():
48
+ elif "destroy_widget" in content .keys ():
49
+ self ._component_instances = [
50
+ w
51
+ for w in self ._component_instances
52
+ if w .model_id != content ["destroy_widget" ]
53
+ ]
54
+ elif "event" in content .keys ():
52
55
event = content .get ("event" , "" )
53
56
data = content .get ("data" , {})
54
57
if buffers :
55
- getattr (self , ' vue_' + event )(data , buffers )
58
+ getattr (self , " vue_" + event )(data , buffers )
56
59
else :
57
- getattr (self , ' vue_' + event )(data )
60
+ getattr (self , " vue_" + event )(data )
58
61
59
62
60
63
def _value_to_json (x , obj ):
61
64
if inspect .isclass (x ):
62
- return {
63
- 'class' : [x .__module__ , x .__name__ ],
64
- 'props' : x .class_trait_names ()
65
- }
66
- return widget_serialization ['to_json' ](x , obj )
65
+ return {"class" : [x .__module__ , x .__name__ ], "props" : x .class_trait_names ()}
66
+ return widget_serialization ["to_json" ](x , obj )
67
67
68
68
69
69
def _class_to_json (x , obj ):
70
70
if not x :
71
- return widget_serialization [' to_json' ](x , obj )
71
+ return widget_serialization [" to_json" ](x , obj )
72
72
return {k : _value_to_json (v , obj ) for k , v in x .items ()}
73
73
74
74
75
75
def as_refs (name , data ):
76
76
def to_ref_structure (obj , path ):
77
77
if isinstance (obj , list ):
78
- return [to_ref_structure (item , [* path , index ]) for index , item in enumerate (obj )]
78
+ return [
79
+ to_ref_structure (item , [* path , index ]) for index , item in enumerate (obj )
80
+ ]
79
81
if isinstance (obj , dict ):
80
82
return {k : to_ref_structure (v , [* path , k ]) for k , v in obj .items ()}
81
83
82
84
# add object id to detect a new object in the same structure
83
- return {OBJECT_REF : name , ' path' : path , 'id' : id (obj )}
85
+ return {OBJECT_REF : name , " path" : path , "id" : id (obj )}
84
86
85
87
return to_ref_structure (data , [])
86
88
87
89
88
90
class VueTemplate (DOMWidget , Events ):
89
91
90
92
class_component_serialization = {
91
- ' from_json' : widget_serialization [' to_json' ],
92
- ' to_json' : _class_to_json
93
+ " from_json" : widget_serialization [" to_json" ],
94
+ " to_json" : _class_to_json ,
93
95
}
94
96
95
- # Force the loading of jupyter-vue before dependent extensions when in a static context (embed,
96
- # voila)
97
- _jupyter_vue = Any (force_load_instance , read_only = True ).tag (sync = True , ** widget_serialization )
97
+ # Force the loading of jupyter-vue before dependent extensions when in a static
98
+ # context (embed, voila)
99
+ _jupyter_vue = Any (force_load_instance , read_only = True ).tag (
100
+ sync = True , ** widget_serialization
101
+ )
98
102
99
- _model_name = Unicode (' VueTemplateModel' ).tag (sync = True )
103
+ _model_name = Unicode (" VueTemplateModel" ).tag (sync = True )
100
104
101
- _view_name = Unicode (' VueView' ).tag (sync = True )
105
+ _view_name = Unicode (" VueView" ).tag (sync = True )
102
106
103
- _view_module = Unicode (' jupyter-vue' ).tag (sync = True )
107
+ _view_module = Unicode (" jupyter-vue" ).tag (sync = True )
104
108
105
- _model_module = Unicode (' jupyter-vue' ).tag (sync = True )
109
+ _model_module = Unicode (" jupyter-vue" ).tag (sync = True )
106
110
107
111
_view_module_version = Unicode (semver ).tag (sync = True )
108
112
109
113
_model_module_version = Unicode (semver ).tag (sync = True )
110
114
111
- template = Union ([
112
- Instance ( Template ),
113
- Unicode ()]). tag ( sync = True , ** widget_serialization )
115
+ template = Union ([Instance ( Template ), Unicode ()]). tag (
116
+ sync = True , ** widget_serialization
117
+ )
114
118
115
119
css = Unicode (None , allow_none = True ).tag (sync = True )
116
120
@@ -121,15 +125,16 @@ class VueTemplate(DOMWidget, Events):
121
125
events = List (Unicode (), allow_none = True ).tag (sync = True )
122
126
123
127
components = Dict (default_value = None , allow_none = True ).tag (
124
- sync = True , ** class_component_serialization )
128
+ sync = True , ** class_component_serialization
129
+ )
125
130
126
131
_component_instances = List ().tag (sync = True , ** widget_serialization )
127
132
128
133
template_file = None
129
134
130
135
def __init__ (self , * args , ** kwargs ):
131
136
if self .template_file :
132
- abs_path = ''
137
+ abs_path = ""
133
138
if type (self .template_file ) == str :
134
139
abs_path = os .path .abspath (self .template_file )
135
140
elif type (self .template_file ) == tuple :
@@ -140,21 +145,24 @@ def __init__(self, *args, **kwargs):
140
145
141
146
super ().__init__ (* args , ** kwargs )
142
147
143
- sync_ref_traitlets = [v for k , v in self .traits ().items ()
144
- if 'sync_ref' in v .metadata .keys ()]
148
+ sync_ref_traitlets = [
149
+ v for k , v in self .traits ().items () if "sync_ref" in v .metadata .keys ()
150
+ ]
145
151
146
152
def create_ref_and_observe (traitlet ):
147
153
data = traitlet .get (self )
148
- ref_name = traitlet .name + '_ref'
149
- self .add_traits (** {ref_name : Any (as_refs (traitlet .name , data )).tag (sync = True )})
154
+ ref_name = traitlet .name + "_ref"
155
+ self .add_traits (
156
+ ** {ref_name : Any (as_refs (traitlet .name , data )).tag (sync = True )}
157
+ )
150
158
151
159
def on_ref_source_change (change ):
152
- setattr (self , ref_name , as_refs (traitlet .name , change [' new' ]))
160
+ setattr (self , ref_name , as_refs (traitlet .name , change [" new" ]))
153
161
154
162
self .observe (on_ref_source_change , traitlet .name )
155
163
156
164
for traitlet in sync_ref_traitlets :
157
165
create_ref_and_observe (traitlet )
158
166
159
167
160
- __all__ = [' VueTemplate' ]
168
+ __all__ = [" VueTemplate" ]
0 commit comments