Orange的扩展插件Widgets开发(六)-OWWidget

Orange的扩展插件Widgets开发(六)

-OWWidget

The OWWidget is the main component for implementing a widget in the Orange Canvas workflow. It both defines the widget input/output capabilities and implements it’s functionality within the canvas.

Widget Meta Description

Every widget in the canvas framework needs to define it’s meta definition. This includes the widget’s name and text descriptions but more importantly also its input/output specification. This is done by defining constants in the widget’s class namespace:

class IntConstant(OWWidget):
    name = "Integer Constant"
    description "A simple integer constant"

    outputs = [("Constant", int)]

    ...

    def commit(self):
        """        Commit/send the outputs.        """
        self.send("Constant", 42)

Omitting the implementation details, this defines a simple node namedInteger Constant which outputs (on a signal called Constant) a single object of type int.

The node’s inputs are defined similarly but with and extra field naming the widget instance method which accepts the inputs at runtime:

class Adder(OWWidget):
    name = "Add two integers"
    description = "Add two numbers"

    inputs = [("A", int, "set_A"),
              ("B", int, "set_B")]

    outputs = [("A + B", int")]

    ...

    def set_A(self, a):
        """Set the `A` input."""
        self.A = a

    def set_B(self, b):
        """Set the `B` input."""
        self.B = b

    def handleNewSignals(self):
        """Coalescing update."""
        self.commit()

    def commit(self):
        """        Commit/send the outputs.        """
        sef.send("result", self.A + self.B)

See also

Getting Started Tutorial

Input/Output Signal Definitions

Widgets specify their input/output capabilities in their class definitions by means of a inputs and outputs class attributes which are lists of tuples or lists of InputSignal/OutputSignal.

  • An input is defined by a (name, type, methodname [, flags]) tuple or an InputSignal.

    Thenameis the input’s descriptive name,typethe type of objects received,methodnameastrnaming a widget member method that will receive the input, and optionalflags.

  • An output is defined by a (name, type, flags) tuple or anOutputSignal

Input/Output flags:

  • Orange.widgets.widget.Default

  • This input is the default for it’s type. When there are multiple IO signals with the same type the one with the default flag takes precedence when adding a new link in the canvas.

  • Orange.widgets.widget.Multiple

  • Multiple signal (more then one input on the channel). Input with this flag receive a second parameterid.

  • Orange.widgets.widget.Dynamic

  • Only applies to output. Specifies that the instances on the output will in general be subtypes of the declared type and that the output can be connected to any input which can accept a subtype of the declared output type.

Sending/Receiving

The widgets receive inputs at runtime with the designated handler method (specified in the OWWidget.inputs class member).

If a widget defines multiple inputs it can coalesce updates by reimplementingOWWidget.handleNewSignals() method.

def set_foo(self, foo):
   self.foo = foodef set_bar(self, bar):
   self.bar = bardef handleNewSignals(self)
   dosomething(self.foo, self.bar)

If an input is defined with the Multiple, then the input handler method also receives an connectioniduniquely identifying a connection/link on which the value was sent (see also Channels and Tokens)

The widgets publish their outputs using OWWidget.send() method.


转载于:https://my.oschina.net/u/2306127/blog/596150

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值