duck.html.components.extensions¶
Extensions module for components.
Usage example:
from duck.html.components.button import Button
from duck.html.components.extensions import Extension
class MyExtension(Extension):
def my_new_method(self):
# This method will now be available on component `MyButton`
# Do something
pass
def existing_method(self):
super().existing_method()
# When overriding an existing method, don't forget to call `super()`
# Do something, e.g. access component keyword arguments through `self.kwargs`
def apply_extension(self):
super().apply_extension()
# Modify something or do something
self.style["background-color"] = "red"
class MyButton(MyExtension, Button):
pass
btn = MyButton()
btn.style["background-color"] == "red" # Outputs: True
Package Contents¶
Classes¶
Basic extension for HTML components, providing common properties like |
|
Base class for all component extensions. |
|
Extension for improving CSS style compatibility between browsers. Automatically adds and (optionally) deletes vendor-prefixed versions of certain CSS properties when setting or deleting styles. |
API¶
- class duck.html.components.extensions.BasicExtension[source]¶
Bases:
duck.html.components.extensions.ExtensionBasic extension for HTML components, providing common properties like
text,id,bg_color, andcolor.- apply_extension()[source]¶
Apply the extension. Applies initial values from
kwargsfor basic properties such asid,klass,text,bg_color, andcolor.
- property bg_color: Optional[str]¶
Returns the background color of the component.
- Returns:
The background color if set, otherwise None.
- Return type:
Optional[str]
- property color: Optional[str]¶
Returns the foreground (text) color of the component.
- Returns:
The text color if set, otherwise None.
- Return type:
Optional[str]
- get_kwarg_or_raise(kwarg: str) Any[source]¶
Retrieves an argument from component
kwargsor raise an exception.- Raises:
KwargError – If a keyword argument is not provided to the component.
- get_request_or_raise() duck.http.request.HttpRequest[source]¶
Retrieves a request object from component
kwargsor raise an exception.- Raises:
RequestNotFoundError – If the request is not in kwargs or kwargs[‘context’] (if used in templates).
- property id: Optional[str]¶
Returns the ID of the component.
- Returns:
The ID if set, otherwise None.
- Return type:
Optional[str]
- property klass: Optional[str]¶
Returns the
classof the component.- Returns:
The class’ if set, otherwise None.
- Return type:
Optional[str]
- property text: str¶
Returns the inner html of the component.
… admonition:: Notes
This escapes HTML if found in text. You can disable this by setting
escape_on_text=Falseon component.- Returns:
The inner content of the component.
- Return type:
str
- Raises:
ExtensionError – If the component does not support
inner_html.
- class duck.html.components.extensions.Extension[source]¶
Base class for all component extensions.
Extensions allow reusable behaviors to be added to components via mixins. Override methods like
on_createor define new ones for extended logic.
- exception duck.html.components.extensions.ExtensionError[source]¶
Bases:
ExceptionRaised when there is an error related to a component extension.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- exception duck.html.components.extensions.KwargError[source]¶
Bases:
duck.html.components.extensions.ExtensionErrorRaised when there is no required keyword argument in component
kwargs.Initialization
Initialize self. See help(type(self)) for accurate signature.
- exception duck.html.components.extensions.RequestNotFoundError[source]¶
Bases:
duck.html.components.extensions.ExtensionErrorRaised when there is no required ‘request’ in component
kwargsorkwargs['context'](if component used in a template).Initialization
Initialize self. See help(type(self)) for accurate signature.
- class duck.html.components.extensions.StyleCompatibilityExtension(*args, **kw)[source]¶
Bases:
duck.html.components.extensions.ExtensionExtension for improving CSS style compatibility between browsers. Automatically adds and (optionally) deletes vendor-prefixed versions of certain CSS properties when setting or deleting styles.
Initialization