Getting Started (Continued)

After learning what an Orange Widget is and how to define them on a toy example, we will build an semi-useful widgets that can work together with the existing Orange Widgets.

We will start with a very simple one, that will receive a data set on the input and will output a data set with 10% of the data instances. We will call this widget OWDataSamplerA (OW for Orange Widget, DataSampler since this is what widget will be doing, and A since we prototype a number of this widgets in our tutorial).

A ‘Demo’ package

First in order to include our new widgets in the Orange Canvas’s toolbox we will create a dummy python project named orange-demo

The layout should be:


and the orange-demo/ should contain

from setuptools import setup

      package_data={"orangedemo": ["icons/*.svg"]},
      classifiers=["Example :: Invalid"],
      # Declare orangedemo package to contain widgets for the "Demo" category
      entry_points={"orange.widgets": "Demo = orangedemo"},

Note that we declare our orangedemo package as containing widgets from an ad hoc defined category Demo.

Following the previous examples, our module defining the OWDataSamplerA widget starts out as:

import sys
import numpy

from Orange.widgets.widget import OWWidget, Input, Output
from Orange.widgets import gui

class OWDataSamplerA(OWWidget):
    name = "Data Sampler"
    description = "Randomly selects a subset of instances from the data set"
    icon = "icons/DataSamplerA.svg"
    priority = 10

    class Inputs:
        data = Input("Data",

    class Outputs:
        sample = Output("Sampled Data",

    want_main_area = False

    def __init__(self):

        # GUI
        box = gui.widgetBox(self.controlArea, "Info")
        self.infoa = gui.widgetLabel(box, 'No data on input yet, waiting to get something.')
        self.infob = gui.widgetLabel(box, '')

The widget defines an input channel “Data” and an output channel called “Sampled Data”. Both will carry tokens of the type In the code, we will refer to the signals as and Outputs.sample.

Channels can carry tokens of arbitrary types. However, the purpose of widgets is to talk with other widgets, so as one of the main design principles we try to maximize the flexibility of widgets by minimizing the number of different channel types. Do not invent new signal types before checking whether you cannot reuse the existing.

As our widget won’t display anything apart from some info, we will place the two labels in the control area and surround it with the box “Info”.

The next four lines specify the GUI of our widget. This will be simple, and will include only two lines of text of which, if nothing will happen, the first line will report on “no data yet”, and second line will be empty.

In order to complete our widget, we now need to define a method that will handle the input data. We will call it set_data(); the name is arbitrary, but calling the method set_<the name of the input> seems like a good practice. To designate it as the method that accepts the signal defined in, we decorate it with
    def set_data(self, dataset):
        if dataset is not None:
            self.infoa.setText('%d instances in input data set' % len(dataset))
            indices = numpy.random.permutation(len(dataset))
            indices = indices[:int(numpy.ceil(len(dataset) * 0.1))]
            sample = dataset[indices]
            self.infob.setText('%d sampled instances' % len(sample))
            self.infoa.setText('No data on input yet, waiting to get something.')
            self.Outputs.sample.send("Sampled Data")

The dataset argument is the token sent through the input channel which our method needs to handle.

To handle a non-empty token, the widget updates the interface reporting on number of data items on the input, then does the data sampling using Orange’s routines for these, and updates the interface reporting on the number of sampled instances. Finally, the sampled data is sent as a token to the output channel defined as Output.sample.

Although our widget is now ready to test, for a final touch, let’s design an icon for our widget. As specified in the widget header, we will call it DataSamplerA.svg and put it in icons subdirectory of orangedemo directory.

With this we can now go ahead and install the orangedemo package. We will do this by running pip install -e . command from within the orange-demo directory.


Depending on your python installation you might need administrator/superuser privileges.

For a test, we now open Orange Canvas. There should be a new pane in a widget toolbox called Demo. If we click on this pane, it displays an icon of our widget. Try to hover on it to see if the header and channel info was processed correctly:


Now for the real test. We put the File widget on the schema (from Data pane) and load the data set. We also put our Data Sampler widget on the scheme and open it (double click on the icon, or right-click and choose Open):


Now connect the File and Data Sampler widget (click on an output connector of the File widget, and drag the line to the input connector of the Data Sampler). If everything is ok, as soon as you release the mouse, the connection is established and, the token that was waiting on the output of the file widget was sent to the Data Sampler widget, which in turn updated its window:


To see if the Data Sampler indeed sent some data to the output, connect it to the Data Table widget:


Try opening different data files (the change should propagate through your widgets and with Data Table window open, you should immediately see the result of sampling). Try also removing the connection between File and Data Sampler (right click on the connection, choose Remove). What happens to the data displayed in the Data Table?

Testing Your Widget Outside Orange Canvas

As a general rule each widget should have a simple main stub function so it can be run independently from Orange Canvas

def main(argv=None):
    from AnyQt.QtWidgets import QApplication
    # PyQt changes argv list in-place
    app = QApplication(list(argv) if argv else [])
    argv = app.arguments()
    if len(argv) > 1:
        filename = argv[1]
        filename = "iris"

    ow = OWDataSamplerA()

    dataset =
    return 0

if __name__ == "__main__":