Exemple #1
0
def param_to_proto(param) -> widgets_pb2.Parameter:
    try:
        return param._to_proto()
    except AttributeError:
        return widgets_pb2.Parameter(
            name=param._name,
            label=getattr(param, "_label", ""),
            typespec=serialize_typespec(type(param)),
        )
Exemple #2
0
    def _to_proto(self) -> widgets_pb2.Parameter:
        typespec = serialize_typespec(type(self).__bases__[-1])
        msg = widgets_pb2.Parameter(
            name=self._name, label=self._label, typespec=typespec
        )

        widget_msg = getattr(msg, WIDGET_MSG_TYPE_TO_ONEOF_NAME[self._proto_type])
        widget_msg.SetInParent()
        # ^ ensure `widget_msg` is registered as WhichOneof, even if it's empty.
        # https://stackoverflow.com/a/60022948/10519953
        self._to_proto_set_widget_msg(widget_msg)

        return msg
    def test_proto_to_param_non_widget(self):
        name = "foo"
        type_ = List[Int]

        proto = widgets_pb2.Parameter(
            name=name,
            typespec=cereal.serialize_typespec(type_),
        )
        param = widget.proto_to_param(proto)

        assert type(param) is type_
        assert param._name == name

        assert param.params == (param,)
        assert param.graft["returns"] == name
    def test_proto_to_param(self, StrWidget):
        name = "foo"
        default = "bar"
        label = "baz"

        proto = widgets_pb2.Parameter(
            name=name,
            label=label,
            typespec=cereal.serialize_typespec(Str),
        )
        sub_msg = getattr(
            proto, widget.WIDGET_MSG_TYPE_TO_ONEOF_NAME[StrWidget._proto_type]
        )
        sub_msg.default = default

        param = widget.proto_to_param(proto)
        assert type(param) is StrWidget
        assert param._name == name
        assert param._label == label
        assert param._default == default

        assert param.graft["returns"] == name
        assert param.params == (param,)
Exemple #5
0
from descarteslabs.common.proto.widgets import widgets_pb2
from descarteslabs.common.graft import client

from ...cereal import serialize_typespec, deserialize_typespec
from ..core import Proxytype
from ..identifier import parameter

if TYPE_CHECKING:
    import ipywidgets


MSG_TO_WIDGET = {}

# OMG WTF proto. how do you figure out which oneof name corresponds to the type you want?
msg = widgets_pb2.Parameter()
WIDGET_MSG_TYPE_TO_ONEOF_NAME = {
    type(getattr(msg, oneof.name)): oneof.name
    for oneof in msg.DESCRIPTOR.oneofs_by_name["widget"].fields
}
del msg


class Widget:
    # Mixin class to create a widget parameter: class that acts like a Proxytype, but also like a widget
    # Subclasses must inherit like::
    #     class SubWidget(Widget, ImageCollection):
    #         _proto_type = widgets_pb2.SubWidgetMessage
    # Subclasses may want to override `_to_proto_set_widget_msg` and `_from_proto_init_from_widget_msg`.

    # NOTE: we don't set an actual docstring here, so subclasses will inherit whatever docstring