-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * use metaclass to instantiate a blitzui * fix text is finished trigger in gpt component * fix lint and typing * wip * wip * New heritence class version * Add new mecanism in components * Add new composant and Iframe * Add ng variable * Delete .zed/settings.json * add some component and refacto * clean * continue adding component and remove js as much as possible * clean * make passing args and kwargs in component make them used in render * clean * use image components * add doc * update components * fix ruff * Update blitz/ui/components/buttons/base.py --------- Co-authored-by: Maxime de Pachtere <[email protected]>
- Loading branch information
Showing
71 changed files
with
1,531 additions
and
830 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,3 +168,4 @@ database.db | |
.nicegui/ | ||
demo-blitz-app/ | ||
.idea/ | ||
.ruff_cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import importlib.metadata | ||
|
||
from .core import BlitzCore | ||
|
||
__version__ = importlib.metadata.version("blitz") | ||
|
||
__all__ = [ | ||
"BlitzCore", | ||
] | ||
__version__ = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .base import BaseComponent as Component | ||
|
||
__all__ = [ | ||
"Component", | ||
] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from nicegui import ui | ||
from typing import Callable | ||
from blitz.ui.components.base import BaseComponent | ||
|
||
|
||
class BaseExpansion(BaseComponent[ui.expansion]): | ||
def __init__( | ||
self, | ||
text: str = "", | ||
caption: str | None = None, | ||
icon: str | None = None, | ||
group: str | None = None, | ||
value: bool = False, | ||
on_value_change: Callable[..., None] | None = None, | ||
props: str = "", | ||
classes: str = "", | ||
) -> None: | ||
self.text = text | ||
self.caption = caption | ||
self.icon = icon | ||
self.group = group | ||
self.value = value | ||
self.on_value_change = on_value_change | ||
super().__init__(props=props, classes=classes) | ||
|
||
def render(self) -> None: | ||
self.ng = ( | ||
ui.expansion( | ||
self.text, | ||
caption=self.caption, | ||
icon=self.icon, | ||
group=self.group, | ||
value=self.value, | ||
on_value_change=self.on_value_change, | ||
) | ||
.classes(self.classes) | ||
.props(self.props) | ||
) | ||
|
||
|
||
class BaseAccordion(BaseExpansion): | ||
def __init__( | ||
self, | ||
text: str = "", | ||
caption: str | None = None, | ||
icon: str | None = None, | ||
group: str | None = None, | ||
is_open: bool = False, | ||
on_change: Callable[..., None] | None = None, | ||
props: str = "", | ||
classes: str = "", | ||
) -> None: | ||
super().__init__( | ||
text=text, | ||
caption=caption, | ||
icon=icon, | ||
group=group, | ||
value=is_open, | ||
on_value_change=on_change, | ||
props=props, | ||
classes=classes, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,150 @@ | ||
import time | ||
from typing import Any, Generic, Protocol, Self, TypeVar, cast, overload | ||
|
||
from nicegui import ui | ||
from nicegui.element import Element | ||
from blitz.ui.blitz_ui import BlitzUI, get_blitz_ui | ||
|
||
|
||
class BaseComponent: | ||
def __init__(self, blitz_ui: BlitzUI = get_blitz_ui()) -> None: | ||
self.blitz_ui: BlitzUI = blitz_ui | ||
self.current_project = blitz_ui.current_project | ||
self.current_app = blitz_ui.current_app | ||
class NiceGUIComponent(Protocol): | ||
def __enter__(self) -> Any: | ||
... | ||
|
||
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None: | ||
... | ||
|
||
|
||
V = TypeVar("V", bound=Any) | ||
|
||
|
||
# Get the blitz_ui through a metaclass | ||
class BaseComponentMeta(type): | ||
def __new__( | ||
cls, | ||
name: str, | ||
bases: tuple[type, ...], | ||
namespace: dict[str, Any], | ||
*, | ||
reactive: bool = False, | ||
render: bool = True, | ||
) -> type: | ||
blitz_ui = get_blitz_ui() | ||
namespace["blitz_ui"] = blitz_ui | ||
namespace["reactive"] = reactive | ||
namespace["_render"] = render | ||
return super().__new__(cls, name, bases, namespace) | ||
|
||
|
||
class BaseComponent(Generic[V], metaclass=BaseComponentMeta): | ||
def __init__( | ||
self, | ||
*args: Any, | ||
props: str = "", | ||
classes: str = "", | ||
render: bool | None = None, | ||
**kwargs: Any, | ||
) -> None: | ||
self._ng: Element | ||
self.props = props | ||
self.classes = classes | ||
self.blitz_ui: BlitzUI | ||
self.reactive: bool | ||
self._render: bool | ||
if render is not None: | ||
self._render = render | ||
self.current_project = self.blitz_ui.current_project | ||
self.current_app = self.blitz_ui.current_app | ||
self.kwargs = kwargs | ||
|
||
self.blitz_ui = get_blitz_ui() | ||
if self.reactive: | ||
self.render = ui.refreshable(self.render) # type: ignore | ||
if self._render: | ||
self.render(*args, **kwargs) | ||
|
||
@overload | ||
def render(self) -> None: | ||
... | ||
|
||
@overload | ||
def render(self, *args: Any, **kwargs: Any) -> None: | ||
... | ||
|
||
def render(self, *args: Any, **kwargs: Any) -> None: | ||
raise NotImplementedError | ||
|
||
def __call__(self, *args: Any, **kwargs: Any) -> Any: | ||
self.render(*args, **kwargs) | ||
|
||
def refresh(self, *args: Any, **kwargs: Any) -> None: | ||
if hasattr(self.render, "refresh"): | ||
self.render.refresh(*args, **kwargs) # type: ignore | ||
|
||
@property | ||
def ng(self) -> Element: | ||
return self._ng | ||
|
||
@ng.setter | ||
def ng(self, value: Element) -> None: | ||
self._ng = value | ||
|
||
@classmethod | ||
def variant( | ||
cls, name: str = "", *, props: str = "", classes: str = "", render: bool = True, **kwargs: Any | ||
) -> type[Self]: | ||
""" | ||
Create a new type (class) based on the current component class with specified props and classes. | ||
:param props: The properties to be predefined in the new class. | ||
:param classes: The CSS classes to be predefined in the new class. | ||
:return: A new type (class) that is a variant of the current class with predefined props and classes. | ||
""" | ||
if not name: | ||
new_type_name = f'{cls.__name__}_{str(time.time()).replace(".","")}' | ||
else: | ||
new_type_name = f"{name}{cls.__name__}" | ||
|
||
if hasattr(cls, "props"): | ||
props = f"{getattr(cls, 'props')} {props}" | ||
if hasattr(cls, "classes"): | ||
classes = f"{getattr(cls, 'classes')} {classes}" | ||
|
||
return type( | ||
new_type_name, | ||
(cls,), | ||
{ | ||
"props": props, | ||
"classes": classes, | ||
"kwargs": kwargs, | ||
}, | ||
render=render, | ||
) | ||
|
||
def __enter__(self) -> V: | ||
if hasattr(self.ng, "__enter__"): | ||
return cast(V, self.ng.__enter__()) | ||
raise NotImplementedError | ||
|
||
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Any: | ||
if hasattr(self.ng, "__exit__"): | ||
self.ng.__exit__(exc_type, exc_value, traceback) | ||
else: | ||
raise NotImplementedError | ||
|
||
def __new__(cls, *args: Any, **kwargs: Any) -> Self: | ||
instance = super().__new__(cls) | ||
for parent in cls.mro(): | ||
if hasattr(parent, "classes"): | ||
setattr(instance, "classes", getattr(parent, "classes")) | ||
if hasattr(parent, "props"): | ||
setattr(instance, "props", getattr(parent, "props")) | ||
return instance | ||
|
||
def __setattr__(self, __name: str, __value: Any) -> None: | ||
"""If the attribute is classes or props, then append the new value to the existing value.""" | ||
if __name in ["props", "classes"] and hasattr(self, __name): | ||
if __value in getattr(self, __name): | ||
return | ||
__value = f"{getattr(self, __name)} {__value}" | ||
|
||
return super().__setattr__(__name, __value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .flat import FlatButton | ||
from .base import BaseButton as Button | ||
|
||
__all__ = [ | ||
"FlatButton", | ||
"Button", | ||
] |
Oops, something went wrong.