Orange Widget Base

raw JSON →
4.27.0 verified Sat May 09 auth: no python

Base classes for creating Orange Canvas widgets. Provides OWBaseWidget and its derived classes for building interactive data mining components. Current version 4.27.0, requires Python >=3.10. Released as part of the Orange data mining suite.

pip install orange-widget-base
error ModuleNotFoundError: No module named 'Orange.widgets.gui'
cause The gui module was moved to the orangewidget package in version 4.x.
fix
Replace from Orange.widgets import gui with from orangewidget import gui.
error AttributeError: 'MyWidget' object has no attribute 'Inputs'
cause Input and Output signals are now defined as inner classes, not as class-level attributes.
fix
Define class Inputs: and class Outputs: inside your widget class with Input and Output instances.
error TypeError: signal must be a type, got <class 'str'>
cause Passing a string instead of a class for the signal type parameter.
fix
Pass the actual class, e.g., Input("Data", Orange.data.Table, "set_data") instead of a string.
error ImportError: cannot import name 'Setting' from 'Orange.widgets.settings'
cause The settings module moved to orangewidget.settings.
fix
Change import to from orangewidget.settings import Setting.
breaking In version 4.x, the signal handling API changed from connect/disconnect style to decorator-based Input/Output classes. Old code using `self.inputs = [...]` or `self.outputs = [...]` will break.
fix Define Inputs and Outputs as inner classes with Input/Output instances.
breaking The `gui` module was removed from `Orange.widgets` and moved to the separate `orangewidget` package. Importing from `Orange.widgets.gui` will raise ImportError.
fix Use `from orangewidget import gui` instead.
breaking The `Orange.widgets.settings` module moved to `orangewidget.settings`. The `Setting` class import path changed.
fix Import from `orangewidget.settings import Setting`.
breaking The `orange-canvas-core` package now enforces the new widget API. Old-style widgets may not load or cause errors.
fix Update widgets to use OWBaseWidget and Input/Output decorators.
deprecated The use of `getattr(self, 'handler_{}'.format(signal_name))` signal handlers is deprecated in favor of explicit methods linked via `Input` decorator.
fix Define handler methods with descriptive names and link them via `Input("Name", type, "method_name")`.
gotcha The `name` attribute of OWBaseWidget must be a non-empty string, otherwise the widget may not appear in the canvas.
fix Always set `name` to a unique, descriptive string.
gotcha Signals types must be actual Python objects (classes), not strings. Using strings for type will raise a TypeError at registration.
fix Pass the class object, e.g., `Input("Data", Orange.data.Table, "set_data")`.

Minimal OWBaseWidget subclass with inputs, outputs, settings, and control area.

from orangewidget.widget import OWBaseWidget, Input, Output
from orangewidget import gui
from orangewidget.settings import Setting

class MyWidget(OWBaseWidget):
    name = "My Widget"
    description = "A custom widget"
    icon = "icons/my.svg"
    priority = 10

    class Inputs:
        data = Input("Data", object, "set_data")

    class Outputs:
        result = Output("Result", object)

    settings = {"threshold": Setting(0.5)}
    want_main_area = False

    def __init__(self):
        super().__init__()
        self.threshold = 0.5
        gui.doubleSpin(self.controlArea, self, "threshold", 0, 1, 0.1, label="Threshold:")

    def set_data(self, data):
        pass

if __name__ == "__main__":
    from orangewidget.utils.widgetpreview import WidgetPreview
    WidgetPreview(MyWidget).run()