Skip to content

Commit 31105cb

Browse files
authored
auto mixin class for generating add_<graphic> methods (#248)
* automixin class for generating add_<graphic> methods * generating the class and methods seems to work fine until I try to import the class elsewhere, graphic names not being defined * moving mixin class to layouts, adding mixin to subplot * requested changes, methods still not working * fix add graphic methods * revert to orig * trying to use locals() to circumvent issue * generated methods should work now * regenerate methods to align with most up-to-date fpl graphics * fix automixin generated methods * add disclaimer, change return type annotation * change return type
1 parent 19adf82 commit 31105cb

File tree

3 files changed

+637
-28
lines changed

3 files changed

+637
-28
lines changed

fastplotlib/layouts/_subplot.py

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
)
1919
from wgpu.gui.auto import WgpuCanvas
2020

21-
from .. import graphics
21+
from ._utils import make_canvas_and_renderer
22+
from ._base import PlotArea
2223
from ..graphics import TextGraphic
2324
from ._utils import make_canvas_and_renderer
2425
from ._base import PlotArea
2526
from ._defaults import create_camera, create_controller
27+
from .graphic_methods_mixin import GraphicMethodsMixin
2628

2729

28-
class Subplot(PlotArea):
30+
class Subplot(PlotArea, GraphicMethodsMixin):
2931
def __init__(
3032
self,
3133
position: Tuple[int, int] = None,
@@ -70,6 +72,8 @@ def __init__(
7072
7173
"""
7274

75+
super(GraphicMethodsMixin, self).__init__()
76+
7377
canvas, renderer = make_canvas_and_renderer(canvas, renderer)
7478

7579
if position is None:
@@ -113,36 +117,10 @@ def __init__(
113117
self.docked_viewports[pos] = dv
114118
self.children.append(dv)
115119

116-
# attach all the add_<graphic_name> methods
117-
for graphic_cls_name in graphics.__all__:
118-
cls = getattr(graphics, graphic_cls_name)
119-
120-
pfunc = partial(self._create_graphic, cls)
121-
pfunc.__signature__ = signature(cls)
122-
pfunc.__doc__ = cls.__init__.__doc__
123-
124-
# cls.type is defined in Graphic.__init_subclass__
125-
setattr(self, f"add_{cls.type}", pfunc)
126-
127120
self._title_graphic: TextGraphic = None
128121
if self.name is not None:
129122
self.set_title(self.name)
130123

131-
def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy:
132-
if "center" in kwargs.keys():
133-
center = kwargs.pop("center")
134-
else:
135-
center = False
136-
137-
if "name" in kwargs.keys():
138-
self._check_graphic_name_exists(kwargs["name"])
139-
140-
graphic = graphic_class(*args, **kwargs)
141-
self.add_graphic(graphic, center=center)
142-
143-
# only return a proxy to the real graphic
144-
return weakref.proxy(graphic)
145-
146124
@property
147125
def name(self) -> Any:
148126
return self._name

0 commit comments

Comments
 (0)