Skip to content

common

Common generic widgets.

These basically integrate many default widgets provided by panel/bokeh into Metador.

FileWidget

Bases: Widget

Simple widget based on (a subschema of) 'core.file'.

Allows to state supported MIME types with less boilerplate.

Source code in src/metador_core/widget/common.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class FileWidget(Widget):
    """Simple widget based on (a subschema of) 'core.file'.

    Allows to state supported MIME types with less boilerplate.
    """

    class Plugin:
        # name and version must be overridden in subclasses
        supports = [FileMeta.Plugin.ref()]

    MIME_TYPES: Set[str] = set()
    """If non-empty, metadata objects must have a MIME type from this set."""

    FILE_EXTS: Set[str] = set()
    """If non-empty, filename must have an extension from this set."""

    @property
    def title(self) -> str:
        return self._meta.name or self._meta.filename or self._node.name

    @classmethod
    @overrides
    def supports_meta(cls, obj: MetadataSchema) -> bool:
        supported_mime = True
        if cls.MIME_TYPES:
            supported_mime = obj.encodingFormat in cls.MIME_TYPES
        supported_ext = False
        if cls.FILE_EXTS:
            supported_ext = obj.filename.endswith(tuple(cls.FILE_EXTS))
        # either supported mime or supported ext is enough
        return supported_mime or supported_ext

MIME_TYPES class-attribute instance-attribute

MIME_TYPES: Set[str] = set()

If non-empty, metadata objects must have a MIME type from this set.

FILE_EXTS class-attribute instance-attribute

FILE_EXTS: Set[str] = set()

If non-empty, filename must have an extension from this set.

DispatcherWidget

Bases: Widget

Meta-widget to dispatch a node+metadata object to a more specific widget.

Make sure that the dispatcher widget is probed before the widgets it can dispatch to.

This works if and only if for each widget in listed in WIDGETS: * the plugin name of the dispatcher is a prefix of the widget name, or * the widget has primary = False and thus is not considered by the dashboard.

Source code in src/metador_core/widget/common.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
class DispatcherWidget(Widget):
    """Meta-widget to dispatch a node+metadata object to a more specific widget.

    Make sure that the dispatcher widget is probed before the widgets it can
    dispatch to.

    This works if and only if for each widget in listed in `WIDGETS`:
    * the plugin name of the dispatcher is a prefix of the widget name, or
    * the widget has `primary = False` and thus is not considered by the dashboard.
    """

    WIDGETS: List[Type[Widget]]
    """Widgets in the order they should be tested."""

    def dispatch(self, w_cls: Type[Widget]) -> Widget:
        """Dispatch to another widget (used by meta-widgets)."""
        return w_cls(
            self._node,
            "",
            server=self._server,
            metadata=self._meta,
            max_width=self._w,
            max_height=self._h,
        )

    @classmethod
    @overrides
    def supports_meta(cls, obj: MetadataSchema) -> bool:
        return any(map(lambda w: w.supports_meta(obj), cls.WIDGETS))

    @overrides
    def setup(self):
        for w_cls in self.WIDGETS:
            if w_cls.supports_meta(self._meta):
                self._widget = self.dispatch(w_cls)
                break

    @overrides
    def show(self) -> Viewable:
        return self._widget.show()

WIDGETS instance-attribute

WIDGETS: List[Type[Widget]]

Widgets in the order they should be tested.

dispatch

dispatch(w_cls: Type[Widget]) -> Widget

Dispatch to another widget (used by meta-widgets).

Source code in src/metador_core/widget/common.py
161
162
163
164
165
166
167
168
169
170
def dispatch(self, w_cls: Type[Widget]) -> Widget:
    """Dispatch to another widget (used by meta-widgets)."""
    return w_cls(
        self._node,
        "",
        server=self._server,
        metadata=self._meta,
        max_width=self._w,
        max_height=self._h,
    )