Quick Start

In magicgui, you can convert functions into widgets. For instance,

from magicgui import magicgui

@magicgui
def print_text(text: str):
    print(text)

print_text.show()

will create a widget that is composed of a line edit (for the input argument text) and a call button.

Similarly, with magicclass decorator, you can convert a Python class into a magicgui's Container widget and its methods appear as push buttons. When a button is clicked, the corresponding magicgui will be popped up.

from magicclass import magicclass

@magicclass
class MyClass:
    def set_value(self, value: str):
        self.value = value

    def print_value(self):
        print(self.value)

ui = MyClass()
ui.show()
_images/fig_1-1.png

Note

Methods whose names start with "_" are considered as inner functions so that they will not be converted into widgets, except for __call__ method.

Use Other Widgets in magic-class

Magic classes can also detect other magicgui's widgets.

from magicgui.widgets import LineEdit, Slider
from magicclass import magicclass

@magicclass
class MyClass:
    s = LineEdit(label="Name:")
    i = Slider(label="Age:", max=100)
    def print(self):
        print(f"{self.s.value} ({self.i.value})")

ui = MyClass()
ui.show()
_images/fig_1-2.png

Note

I highly recommend using field function to create widgets in magic classes. See Use Fields in magic-class.

If a method is decorated with @magicgui, it will directly added in the container widget, in place of a push button. This is natural because decorated methods are no longer functions, but FunctionGui widgets.

from magicgui import magicgui
from magicclass import magicclass

@magicclass
class MyClass:
    @magicgui
    def input_parameters(self, s: str, i: int):
        self.s = s
        self.i = i

    def print(self):
        print(f"{self.s} ({self.i})")

ui = MyClass()
ui.show()
_images/fig_1-3.png

Macro Recording

Another outstanding feature of magic class is its macro recorder functionalities. Function calls and value changes in child widgets are all recorded and you can generate executable Python script at any time.

You can generate Python script as string using create_macro method.

macro = ui.create_macro()
print(macro)

or in a text editor window.

ui.create_macro(show=True)

Occasionally, you may want some functions not to record macro (such as a function that only shows a help window). You can prevent macro recording with do_not_record decorator.

from magicclass import magicclass, do_not_record

@magicclass
class Main:
    @do_not_record
    def f(self):
        """this function will never be recorded"""

ui = Main()
ui.show()

Parameter Options

In magicgui you can define parameter options with keyword arguments:

@magicgui(a={"widget_type": "Slider", "step": 10})
def f(a: int): ...

However, magic classes need another way to do this because magicgui will never be called by users. magicclass uses set_options decorator instead.

from magicclass import magicclass, set_options

@magicclass
class Main:
    @set_options(a={"widget_type": "Slider", "step": 10})
    def f(self, a: int): ...

ui = Main()
ui.show()
_images/fig_1-4.png

Change Button Designs

Aside from those options of magicgui popup widget to run functions, you may also want to change the design of button itself. magicclass uses set_design decorator to do this.

from magicclass import magicclass, set_design

@magicclass
class Main:
    @set_design(text="Click (if you want)", min_height=120)
    def f(self): ...

ui = Main()
ui.show()

set_design can take properties of PushButton as arguments.