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

BasicExtension

Basic extension for HTML components, providing common properties like text, id, bg_color, and color.

Extension

Base class for all component extensions.

StyleCompatibilityExtension

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.Extension

Basic extension for HTML components, providing common properties like text, id, bg_color, and color.

apply_extension()[source]

Apply the extension. Applies initial values from kwargs for basic properties such as id, klass, text, bg_color, and color.

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 kwargs or 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 kwargs or 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 class of 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=False on 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_create or define new ones for extended logic.

apply_extension()[source]

Overrride this method to apply the desired extensions.

Notes:

  • Don’t forget to call super().apply_extensions() inside the method.

  • Methods or property definations are applied to the component automatically.

on_create()[source]
exception duck.html.components.extensions.ExtensionError[source]

Bases: Exception

Raised 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.ExtensionError

Raised 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.ExtensionError

Raised when there is no required ‘request’ in component kwargs or kwargs['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.Extension

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.

Initialization

apply_extension()[source]