Skip to content

Commit

Permalink
use metaclass to instantiate a blitzui
Browse files Browse the repository at this point in the history
  • Loading branch information
mde-pach committed Feb 20, 2024
1 parent beb4c38 commit fc6e44b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
19 changes: 14 additions & 5 deletions blitz/ui/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
T = TypeVar("T", bound="BaseComponent")


class BaseComponent:
def __init__(self, *args: Any, props: str = "", classes: str = "", blitz_ui: BlitzUI = get_blitz_ui(), **kwargs: Any) -> None:
# Get the blitz_ui through a metaclass
class BaseComponentMeta(type):
def __new__(cls, name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> type:
blitz_ui = get_blitz_ui()
namespace["blitz_ui"] = blitz_ui
return super().__new__(cls, name, bases, namespace)


class BaseComponent(metaclass=BaseComponentMeta):
def __init__(self, *args: Any, props: str = "", classes: str = "", **kwargs: Any) -> None:
self.props: str
self.classes: str
self.blitz_ui = blitz_ui
self.current_project = blitz_ui.current_project
self.current_app = blitz_ui.current_app
self.blitz_ui: BlitzUI
self.current_project = self.blitz_ui.current_project
self.current_app = self.blitz_ui.current_app

if hasattr(self, "props"):
self.props = f"{self.props} {props}"
Expand All @@ -23,6 +31,7 @@ def __init__(self, *args: Any, props: str = "", classes: str = "", blitz_ui: Bli
self.classes = classes

self.blitz_ui = get_blitz_ui()
self.render()

def render(self) -> None:
raise NotImplementedError
Expand Down
3 changes: 1 addition & 2 deletions blitz/ui/pages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ class BasePage(BaseComponent):
FRAME: FrameComponent

def __init__(self) -> None:
super().__init__()
self.setup()
self.render()
super().__init__()
self.frame()

def __new__(cls, *args: Any, **kwargs: Any) -> Self:
Expand Down

0 comments on commit fc6e44b

Please sign in to comment.