magicclass package

Subpackages

Submodules

magicclass.core module

magicclass.core.Bound(value: Any) _AnnotatedAlias[source]

Make Annotated type from a MagicField or a method, such as:

from magicclass import magicclass, field

@magicclass
class MyClass:
    i = field(int)
    def func(self, v: Bound(i)):
        ...

Bound(value) is identical to Annotated[Any, {"bind": value}].

class magicclass.core.MagicClassFactory(name: str, layout: str = 'vertical', labels: bool = True, close_on_run: bool = None, popup_mode: str | PopUpMode = None, error_mode: str | ErrorMode = None, widget_type: str | WidgetType = WidgetType.none, icon_path: str = None, parent=None, attrs: dict[str] = None)[source]

Bases: object

Factory class that can make any magic-class.

as_magicclass() magicclass.gui.class_gui.ClassGuiBase[source]
as_magiccontext() magicclass.gui.menu_gui.ContextMenuGui[source]
as_magicmenu() magicclass.gui.menu_gui.MenuGui[source]
as_magictoolbar() magicclass.gui.toolbar.ToolBarGui[source]
class magicclass.core.Parameters[source]

Bases: magicclass.core._CallableClass

as_dict() dict[str, typing.Any][source]

Convert parameter fields into a dictionary.

class params(Parameters):
    i = 1
    j = 2

p = params()
p.as_dict() # {"i": 1, "j": 2}
class magicclass.core.WidgetType(value)[source]

Bases: enum.Enum

An enumeration.

button = 'button'
collapsible = 'collapsible'
draggable = 'draggable'
groupbox = 'groupbox'
hcollapsible = 'hcollapsible'
list = 'list'
mainwindow = 'mainwindow'
none = 'none'
scrollable = 'scrollable'
split = 'split'
stacked = 'stacked'
subwindows = 'subwindows'
tabbed = 'tabbed'
toolbox = 'toolbox'
magicclass.core.build_help(ui: magicclass.gui._base.MagicTemplate, parent=None)[source]

Build a widget for user guide. Once it is built, widget will be cached.

Parameters

ui (MagicTemplate) -- Magic class UI object.

Returns

Help of the input UI.

Return type

HelpWidget

magicclass.core.find_myname(gui: magicclass.gui._base.MagicTemplate)[source]

This function is the essential part of macro recording

magicclass.core.magicclass(class_: type | None = None, *, layout: str = 'vertical', labels: bool = True, name: str = None, close_on_run: bool = None, popup_mode: PopUpModeStr | PopUpMode = None, error_mode: ErrorModeStr | ErrorMode = None, widget_type: WidgetTypeStr | WidgetType = WidgetType.none, icon_path: str = None, parent=None)[source]

Decorator that can convert a Python class into a widget.

@magicclass
class C:
    ...
c = C(a=0)
c.show()
Parameters
  • class (type, optional) -- Class to be decorated.

  • layout (str, "vertical" or "horizontal", default is "vertical") -- Layout of the main widget.

  • labels (bool, default is True) -- If true, magicgui labels are shown.

  • name (str) -- Name of GUI.

  • close_on_run (bool, default is True) -- If True, magicgui created by every method will be deleted after the method is completed without exceptions, i.e. magicgui is more like a dialog.

  • popup (bool, default is True) -- Deprecated.

  • popup_mode (str or PopUpMode, default is PopUpMode.popup) -- Option of how to popup FunctionGui widget when a button is clicked.

  • error_mode (str or ErrorMode, default is ErrorMode.msgbox) -- Option of how to raise errors during function calls.

  • widget_type (WidgetType or str, optional) -- Widget type of container.

  • icon_path (str, optional) -- Path to the icon image.

  • parent (magicgui.widgets._base.Widget, optional) -- Parent widget if exists.

Returns

Return type

Decorated class or decorator.

magicclass.core.magiccontext(class_: type = None, *, close_on_run: bool = None, popup_mode: str | PopUpMode = None, error_mode: str | ErrorMode = None, labels: bool = True, icon_path: str = None, parent=None)[source]

Decorator that can convert a Python class into a context menu.

Parameters
  • class (type, optional) -- Class to be decorated.

  • close_on_run (bool, default is True) -- If True, magicgui created by every method will be deleted after the method is completed without exceptions, i.e. magicgui is more like a dialog.

  • popup (bool, default is True) -- If True, magicgui created by every method will be poped up, else they will be appended as a part of the main widget.

  • parent (magicgui.widgets._base.Widget, optional) -- Parent widget if exists.

Returns

Return type

Decorated class or decorator.

magicclass.core.magicmenu(class_: type = None, *, close_on_run: bool = None, popup_mode: str | PopUpMode = None, error_mode: str | ErrorMode = None, labels: bool = True, icon_path: str = None, parent=None)[source]

Decorator that can convert a Python class into a menu bar.

Parameters
  • class (type, optional) -- Class to be decorated.

  • close_on_run (bool, default is True) -- If True, magicgui created by every method will be deleted after the method is completed without exceptions, i.e. magicgui is more like a dialog.

  • popup (bool, default is True) -- If True, magicgui created by every method will be poped up, else they will be appended as a part of the main widget.

  • parent (magicgui.widgets._base.Widget, optional) -- Parent widget if exists.

Returns

Return type

Decorated class or decorator.

magicclass.core.magictoolbar(class_: type = None, *, close_on_run: bool = None, popup_mode: str | PopUpMode = None, error_mode: str | ErrorMode = None, labels: bool = True, icon_path: str = None, parent=None)[source]

Decorator that can convert a Python class into a menu bar.

magicclass.creator module

class magicclass.creator.Layout(value)[source]

Bases: enum.Enum

An enumeration.

horizontal = 1
vertical = 0
class magicclass.creator.MagicClassCreator[source]

Bases: magicgui.widgets._bases.widget.Widget, magicgui.widgets._bases.mixins._OrientationMixin, MutableSequence[magicgui.widgets._bases.widget.Widget]

magicclass.fields module

class magicclass.fields.MagicField(default=<dataclasses._MISSING_TYPE object>, default_factory=<dataclasses._MISSING_TYPE object>, metadata: dict = {}, name: Optional[str] = None, record: bool = True)[source]

Bases: dataclasses.Field

Field class for magicgui construction. This object is compatible with dataclass.

property annotation
as_getter(obj: magicclass.fields.X)[source]

Make a function that get the value of Widget or Action.

compare
connect(func: Callable) Callable[source]

Set callback function to "ready to connect" state.

decode_string_annotation(annot: str) magicclass.fields.MagicField[source]

Convert string type annotation into field info.

default
default_factory
get_action(obj: magicclass.fields.X) magicclass.gui.mgui_ext.AbstractAction[source]

Get an action from obj. This function will be called every time MagicField is referred by obj.field.

get_widget(obj: magicclass.fields.X) magicgui.widgets._bases.widget.Widget[source]

Get a widget from obj. This function will be called every time MagicField is referred by obj.field.

hash
init
metadata
name
not_ready() bool[source]
property options: dict
ready() bool[source]
repr
to_action() Action | WidgetAction[source]

Create a menu action or a menu widget action from the field.

Returns

Object that can be added to menu.

Return type

Action or WidgetAction

Raises

ValueError -- If there is not enough information to build an action.

to_widget() magicgui.widgets._bases.widget.Widget[source]

Create a widget from the field.

Returns

Widget object that is ready to be inserted into Container.

Return type

Widget

Raises

ValueError -- If there is not enough information to build a widget.

type
property value: Any
property widget_type: str
class magicclass.fields.MagicValueField(default=<dataclasses._MISSING_TYPE object>, default_factory=<dataclasses._MISSING_TYPE object>, metadata: dict = {}, name: Optional[str] = None, record: bool = True)[source]

Bases: magicclass.fields.MagicField

Field class for magicgui construction. Unlike MagicField, object of this class always returns value itself.

callbacks: list[typing.Callable]
compare
default
default_factory
get_widget(obj: magicclass.fields.X) magicgui.widgets._bases.value_widget.ValueWidget[source]

Get a widget from obj. This function will be called every time MagicField is referred by obj.field.

guis: dict[int, ~X]
hash
init
metadata
name
repr
type
magicclass.fields.field(obj: Any = <dataclasses._MISSING_TYPE object>, *, name: str = '', widget_type: str | type[WidgetProtocol] | None = None, options: WidgetOptions = {}, record: bool = True) MagicField[source]

Make a MagicField object.

>>> i = field(1)
>>> i = field(widget_type="Slider")
Parameters
  • obj (Any, default is MISSING) -- Reference to determine what type of widget will be created. If Widget subclass is given, it will be used as is. If other type of class is given, it will used as type annotation. If an object (not type) is given, it will be assumed to be the default value.

  • name (str, default is "") -- Name of the widget.

  • widget_type (str, optional) -- Widget type. This argument will be sent to create_widget function.

  • options (WidgetOptions, optional) -- Widget options. This parameter will always be used in widget(**options) form.

  • record (bool, default is True) -- Record value changes as macro.

Returns

Return type

MagicField

magicclass.fields.vfield(obj: Any = <dataclasses._MISSING_TYPE object>, *, name: str = '', widget_type: str | type[WidgetProtocol] | None = None, options: WidgetOptions = {}, record: bool = True) MagicValueField[source]

Make a MagicValueField object.

>>> i = vfield(1)
>>> i = vfield(widget_type="Slider")

Unlike MagicField, value itself can be accessed.

>>> ui.i      # int is returned
>>> ui.i = 3  # set value to the widget.
Parameters
  • obj (Any, default is MISSING) -- Reference to determine what type of widget will be created. If Widget subclass is given, it will be used as is. If other type of class is given, it will used as type annotation. If an object (not type) is given, it will be assumed to be the default value.

  • name (str, default is "") -- Name of the widget.

  • widget_type (str, optional) -- Widget type. This argument will be sent to create_widget function.

  • options (WidgetOptions, optional) -- Widget options. This parameter will always be used in widget(**options) form.

Returns

Return type

MagicValueField

magicclass.help module

class magicclass.help.HelpWidget(ui=None, parent=None)[source]

Bases: magicclass.widgets.utils.FreeWidget

class magicclass.help.UiBoundTreeItem(parent, ui=None)[source]

Bases: PyQt5.QtWidgets.QTreeWidgetItem

child(self, int) QTreeWidgetItem[source]
takeChild(self, int) QTreeWidgetItem[source]
magicclass.help.get_help_info(ui: magicclass.gui._base.MagicTemplate) tuple[numpy.ndarray, dict[str, str]][source]
magicclass.help.get_keymap(ui: MagicTemplate | type[MagicTemplate])[source]

magicclass.signature module

class magicclass.signature.AdditionalOptions[source]

Bases: TypedDict

into: str
keybinding: str
record: bool
class magicclass.signature.MagicMethodSignature(parameters=None, *, return_annotation, gui_options: Optional[dict[str, dict]] = None, caller_options: Optional[dict[str]] = None, additional_options: Optional[dict[str]] = None)[source]

Bases: magicgui.signature.MagicSignature

This class also retains parameter options for PushButton itself, aside from the FunctionGui options that will be needed when the button is pushed.

classmethod from_signature(sig: inspect.Signature, gui_options=None, caller_options=None, additional_options=None) magicclass.signature.MagicMethodSignature[source]
classmethod get_gui_options(self: inspect.Signature | MagicSignature)[source]
magicclass.signature.get_additional_option(obj: Any, option: str, default=None)[source]
magicclass.signature.upgrade_signature(func, gui_options: Optional[dict] = None, caller_options: Optional[magicgui.types.WidgetOptions] = None, additional_options: Optional[magicclass.signature.AdditionalOptions] = None)[source]

Upgrade function signature to MagicMethodSignature. The input function may have a inspect.Signature or magicgui.signature.Magicsignature.

Parameters
  • func (callable) -- Input function.

  • gui_options (dict, optional) -- Options of FunctionGui.

  • caller_options (WidgetOptions, optional) -- Options of PushButton.

  • additional_options (AdditionalOptions, optional) -- Additional options that will be used in magic class.

Returns

Same function with upgraded signature

Return type

callable

magicclass.wrappers module

magicclass.wrappers.bind_key(*key) Callable[[Callable[[magicclass.wrappers.Args], magicclass.wrappers.Returns]], Callable[[magicclass.wrappers.Args], magicclass.wrappers.Returns]][source]

Define a keybinding to a button or an action. This function accepts several styles of shortcut expression.

>>> @bind_key("Ctrl-A")         # napari style
>>> @bind_key("Ctrl", "A")      # separately
>>> @bind_key(Key.Ctrl + Key.A) # use Key class
>>> @bind_key(Key.Ctrl, Key.A)  # use Key class separately
magicclass.wrappers.click(enables: Optional[Union[str, Iterable[str]]] = None, disables: Optional[Union[str, Iterable[str]]] = None, enabled: bool = True, shows: Optional[Union[str, Iterable[str]]] = None, hides: Optional[Union[str, Iterable[str]]] = None, visible: bool = True)[source]

Set options of push buttons related to button clickability.

Parameters
  • enables (str or iterable of str, optional) -- Enables other button(s) in this list when clicked.

  • disables (str or iterable of str, optional) -- Disables other button(s) in this list when clicked.

  • enabled (bool, default is True) -- The initial clickability state of the button.

  • shows (str or iterable of str, optional) -- Make other button(s) in this list visible when clicked.

  • hides (str or iterable of str, optional) -- Make other button(s) in this list invisible when clicked.

  • visible (bool, default is True) -- The initial visibility of the button.

magicclass.wrappers.do_not_record(method: Callable[[magicclass.wrappers.Args], magicclass.wrappers.Returns]) Callable[[magicclass.wrappers.Args], magicclass.wrappers.Returns][source]

Wrapped method will not be recorded in macro.

magicclass.wrappers.set_design(width: Optional[int] = None, height: Optional[int] = None, min_width: Optional[int] = None, min_height: Optional[int] = None, max_width: Optional[int] = None, max_height: Optional[int] = None, text: Optional[str] = None, icon_path: Optional[str] = None, icon_size: Optional[tuple[int, int]] = None, font_size: Optional[int] = None, font_family: Optional[int] = None, font_color: Optional[Union[str, Iterable[float]]] = None, background_color: Optional[Union[str, Iterable[float]]] = None, visible: bool = True)[source]

Change button/action design by calling setter when the widget is created.

Parameters
  • width (int, optional) -- Button width. Call button.width = width.

  • height (int, optional) -- Button height. Call button.height = height.

  • min_width (int, optional) -- Button minimum width. Call button.min_width = min_width.

  • min_height (int, optional) -- Button minimum height. Call button.min_height = min_height.

  • max_width (int, optional) -- Button maximum width. Call button.max_width = max_width.

  • max_height (int, optional) -- Button maximum height. Call button.max_height = max_height.

  • text (str, optional) -- Button text. Call button.text = text.

  • icon_path (str, optional) -- Path to icon file. min_width and min_height will be automatically set to the icon size if not given.

  • icon_size (tuple of two int, optional) -- Icon size.

  • font_size (int, optional) -- Font size of the text.

  • visible (bool default is True) -- Button visibility.

magicclass.wrappers.set_options(**options)[source]

Set MagicSignature to functions. By decorating a function like below:

>>> @set_options(x={"value": -1})
>>> def func(x):
>>>     ...

then magicgui knows what widget it should be converted to.

Module contents