97 lines
2.2 KiB
Python
97 lines
2.2 KiB
Python
from weakref import WeakSet #TODO: for debugging
|
|
|
|
from bokeh.layouts import column, row
|
|
|
|
|
|
class Object:
|
|
|
|
instances = WeakSet() #TODO: for debugging
|
|
|
|
def __init__(self, layout, parent=None, name=None):
|
|
self.layout = layout
|
|
self.parent = parent
|
|
self.name = name or layout.name
|
|
self.instances.add(self) #TODO: for debugging
|
|
|
|
def delete(self):
|
|
if self.parent is not None:
|
|
self.parent.remove(self)
|
|
else:
|
|
print(self, "has no parent")
|
|
|
|
def __repr__(self):
|
|
return repr(self.layout)
|
|
|
|
|
|
|
|
class Container(Object):
|
|
|
|
factory = None #TODO: abc
|
|
|
|
def __init__(self, *children, parent=None, **kwargs):
|
|
self.children = children = [ensure_Object(obj) for obj in children]
|
|
|
|
layouts = []
|
|
for obj in children:
|
|
layouts.append(obj.layout)
|
|
obj.parent = self
|
|
|
|
factory = self.factory.__func__ # use function and not method (i.e., no self in args)
|
|
layout = factory(*layouts, **kwargs)
|
|
super().__init__(layout, parent=parent)
|
|
|
|
|
|
def append(self, obj):
|
|
obj = ensure_Object(obj)
|
|
obj.parent = self
|
|
self.children.append(obj)
|
|
self.layout.children.append(obj.layout)
|
|
|
|
def prepend(self, obj):
|
|
obj = ensure_Object(obj)
|
|
obj.parent = self
|
|
self.children.insert(0, obj)
|
|
self.layout.children.insert(0, obj.layout)
|
|
|
|
def remove(self, obj):
|
|
obj = ensure_Object(obj)
|
|
obj.parent = None
|
|
self.children.remove(obj)
|
|
self.layout.children.remove(obj.layout)
|
|
if not self.children:
|
|
print("Delete emptied container", self)
|
|
self.delete()
|
|
|
|
def delete(self):
|
|
for obj in self.children:
|
|
obj.parent = None
|
|
# self.children = []
|
|
super().delete()
|
|
|
|
def __bool__(self):
|
|
return bool(self.children)
|
|
|
|
def __iter__(self):
|
|
return iter(self.children)
|
|
|
|
def __repr__(self):
|
|
return f"{self.layout}: {self.children}"
|
|
|
|
|
|
|
|
def ensure_Object(obj):
|
|
if isinstance(obj, Object):
|
|
return obj
|
|
return Object(obj)
|
|
|
|
|
|
|
|
class Column(Container):
|
|
factory = column
|
|
|
|
class Row(Container):
|
|
factory = row
|
|
|
|
|
|
|