Show More
@@ -197,9 +197,9 b' class Widget(LoggingConfigurable):' | |||||
197 | keys = self.keys if key is None else [key] |
|
197 | keys = self.keys if key is None else [key] | |
198 | state = {} |
|
198 | state = {} | |
199 | for k in keys: |
|
199 | for k in keys: | |
200 |
f = self.trait_metadata(k, ' |
|
200 | f = self.trait_metadata(k, 'to_json') | |
201 | if f is None: |
|
201 | if f is None: | |
202 |
f = self. |
|
202 | f = self._trait_to_json | |
203 | value = getattr(self, k) |
|
203 | value = getattr(self, k) | |
204 | state[k] = f(value) |
|
204 | state[k] = f(value) | |
205 | return state |
|
205 | return state | |
@@ -287,9 +287,9 b' class Widget(LoggingConfigurable):' | |||||
287 | """Called when a state is received from the front-end.""" |
|
287 | """Called when a state is received from the front-end.""" | |
288 | for name in self.keys: |
|
288 | for name in self.keys: | |
289 | if name in sync_data: |
|
289 | if name in sync_data: | |
290 |
f = self.trait_metadata(name, ' |
|
290 | f = self.trait_metadata(name, 'from_json') | |
291 | if f is None: |
|
291 | if f is None: | |
292 |
f = self. |
|
292 | f = self._trait_from_json | |
293 | value = f(sync_data[name]) |
|
293 | value = f(sync_data[name]) | |
294 | with self._lock_property(name, value): |
|
294 | with self._lock_property(name, value): | |
295 | setattr(self, name, value) |
|
295 | setattr(self, name, value) | |
@@ -309,30 +309,30 b' class Widget(LoggingConfigurable):' | |||||
309 | """Called when a view has been displayed for this widget instance""" |
|
309 | """Called when a view has been displayed for this widget instance""" | |
310 | self._display_callbacks(self, **kwargs) |
|
310 | self._display_callbacks(self, **kwargs) | |
311 |
|
311 | |||
312 |
def |
|
312 | def _trait_to_json(self, x): | |
313 |
""" |
|
313 | """Convert a trait value to json | |
314 |
|
314 | |||
315 | Traverse lists/tuples and dicts and serialize their values as well. |
|
315 | Traverse lists/tuples and dicts and serialize their values as well. | |
316 | Replace any widgets with their model_id |
|
316 | Replace any widgets with their model_id | |
317 | """ |
|
317 | """ | |
318 | if isinstance(x, dict): |
|
318 | if isinstance(x, dict): | |
319 |
return {k: self. |
|
319 | return {k: self._trait_to_json(v) for k, v in x.items()} | |
320 | elif isinstance(x, (list, tuple)): |
|
320 | elif isinstance(x, (list, tuple)): | |
321 |
return [self. |
|
321 | return [self._trait_to_json(v) for v in x] | |
322 | elif isinstance(x, Widget): |
|
322 | elif isinstance(x, Widget): | |
323 | return "IPY_MODEL_" + x.model_id |
|
323 | return "IPY_MODEL_" + x.model_id | |
324 | else: |
|
324 | else: | |
325 | return x # Value must be JSON-able |
|
325 | return x # Value must be JSON-able | |
326 |
|
326 | |||
327 |
def |
|
327 | def _trait_from_json(self, x): | |
328 | """Convert json values to objects |
|
328 | """Convert json values to objects | |
329 |
|
329 | |||
330 |
|
|
330 | Replace any strings representing valid model id values to Widget references. | |
331 | """ |
|
331 | """ | |
332 | if isinstance(x, dict): |
|
332 | if isinstance(x, dict): | |
333 |
return {k: self. |
|
333 | return {k: self._trait_from_json(v) for k, v in x.items()} | |
334 | elif isinstance(x, (list, tuple)): |
|
334 | elif isinstance(x, (list, tuple)): | |
335 |
return [self. |
|
335 | return [self._trait_from_json(v) for v in x] | |
336 | elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets: |
|
336 | elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets: | |
337 | # we want to support having child widgets at any level in a hierarchy |
|
337 | # we want to support having child widgets at any level in a hierarchy | |
338 | # trusting that a widget UUID will not appear out in the wild |
|
338 | # trusting that a widget UUID will not appear out in the wild |
General Comments 0
You need to be logged in to leave comments.
Login now