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
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 anInputSignal
.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.
来源:oschina
链接:https://my.oschina.net/u/2306127/blog/596150