##// END OF EJS Templates
ict comprehension and list comprehension in pack/unpack widgets
Jonathan Frederic -
Show More
@@ -277,48 +277,33 b' class Widget(LoggingConfigurable):'
277 for handler in self._display_callbacks:
277 for handler in self._display_callbacks:
278 handler(self, **kwargs)
278 handler(self, **kwargs)
279
279
280 def _pack_widgets(self, values):
280 def _pack_widgets(self, x):
281 """Recursively converts all widget instances to model id strings.
281 """Recursively converts all widget instances to model id strings.
282
282
283 Children widgets will be stored and transmitted to the front-end by
283 Children widgets will be stored and transmitted to the front-end by
284 their model ids."""
284 their model ids."""
285 if isinstance(values, dict):
285 if isinstance(x, dict):
286 new_dict = {}
286 return {k: self._pack_widgets(v) for k, v in x.items()}
287 for key, value in values.items():
287 elif isinstance(x, list):
288 new_dict[key] = self._pack_widgets(value)
288 return [self._pack_widgets(v) for v in x]
289 return new_dict
289 elif isinstance(x, Widget):
290 elif isinstance(values, list):
290 return x.model_id
291 new_list = []
292 for value in values:
293 new_list.append(self._pack_widgets(value))
294 return new_list
295 elif isinstance(values, Widget):
296 return values.model_id
297 else:
291 else:
298 return values
292 return x
299
293
300 def _unpack_widgets(self, values):
294 def _unpack_widgets(self, x):
301 """Recursively converts all model id strings to widget instances.
295 """Recursively converts all model id strings to widget instances.
302
296
303 Children widgets will be stored and transmitted to the front-end by
297 Children widgets will be stored and transmitted to the front-end by
304 their model ids."""
298 their model ids."""
305 if isinstance(values, dict):
299 if isinstance(x, dict):
306 new_dict = {}
300 return {k: self._unpack_widgets(v) for k, v in x.items()}
307 for key, values in values.items():
301 elif isinstance(x, list):
308 new_dict[key] = self._unpack_widgets(values[key])
302 return [self._unpack_widgets(v) for v in x]
309 return new_dict
303 elif isinstance(x, string_types):
310 elif isinstance(values, list):
304 return x if x not in Widget.widgets else Widget.widgets[x]
311 new_list = []
312 for value in values:
313 new_list.append(self._unpack_widgets(value))
314 return new_list
315 elif isinstance(values, string_types):
316 if values in Widget.widgets:
317 return Widget.widgets[values]
318 else:
319 return values
320 else:
305 else:
321 return values
306 return x
322
307
323 def _ipython_display_(self, **kwargs):
308 def _ipython_display_(self, **kwargs):
324 """Called when `IPython.display.display` is called on the widget."""
309 """Called when `IPython.display.display` is called on the widget."""
General Comments 0
You need to be logged in to leave comments. Login now