widget_container.py
56 lines
| 2.1 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r14283 | """ContainerWidget class. | ||
Represents a container that can be used to group other widgets. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (c) 2013, the IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r14540 | from .widget import DOMWidget | ||
Jason Grout
|
r14486 | from IPython.utils.traitlets import Unicode, Bool, List, Instance | ||
Jonathan Frederic
|
r14239 | |||
Jonathan Frederic
|
r14283 | #----------------------------------------------------------------------------- | ||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r14540 | class ContainerWidget(DOMWidget): | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('ContainerView', sync=True) | ||
Jonathan Frederic
|
r14268 | |||
Jonathan Frederic
|
r14283 | # Keys, all private and managed by helper methods. Flexible box model | ||
# classes... | ||||
Jonathan Frederic
|
r14608 | children = List(Instance(DOMWidget)) | ||
_children = List(Instance(DOMWidget), sync=True) | ||||
def __init__(self, *pargs, **kwargs): | ||||
"""Constructor""" | ||||
DOMWidget.__init__(self, *pargs, **kwargs) | ||||
self.on_trait_change(self._validate, ['children']) | ||||
def _validate(self, name, old, new): | ||||
"""Validate children list. | ||||
Makes sure only one instance of any given model can exist in the | ||||
Jonathan Frederic
|
r14699 | children list. | ||
An excellent post on uniqifiers is available at | ||||
http://www.peterbe.com/plog/uniqifiers-benchmark | ||||
which provides the inspiration for using this implementation. Below | ||||
I've implemented the `f5` algorithm using Python comprehensions.""" | ||||
Jonathan Frederic
|
r14608 | if new is not None and isinstance(new, list): | ||
Jonathan Frederic
|
r14699 | seen = {} | ||
def add_item(i): | ||||
seen[i.model_id] = True | ||||
return i | ||||
return [add_item(i) for i in new if not i.model_id in seen] | ||||
Jonathan Frederic
|
r14592 | |||
Jonathan Frederic
|
r14607 | |||
Jonathan Frederic
|
r14676 | class PopupWidget(ContainerWidget): | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('PopupView', sync=True) | ||
Jonathan Frederic
|
r14608 | |||
description = Unicode(sync=True) | ||||
button_text = Unicode(sync=True) | ||||