magicgui icon indicating copy to clipboard operation
magicgui copied to clipboard

Construct `Dialog` from a function

Open hanjinliu opened this issue 2 years ago • 0 comments

Hi @tlambert03 ,

I just found the Dialog widget and it's very useful! I think practically Dialog will usually be constructed with request_values inside a callback function of such as button widgets.

@button.changed.connect
def callback():
    values = request_values(a=int, b=str)
    do_something(**values)

def do_something(a: int, b: str): ...

It seems a little bit unnatural for me that Dialog cannot be made from the signature of do_something directly like below

@button.changed.connect
def callback():
    values = request_values_from_callable(do_something)  # signature -> dialog -> values
    do_something(**values)

or even more simply

@button.changed.connect
def callback():
    run_dialog_from_callable(do_something)  # signature -> dialog -> values -> run function

For the second one, run_dialog_from_callable is something like this:

from magicgui.signature import magic_signature

def run_dialog_from_callable(f: Callable):
    dlg = Dialog(
        widgets=list(magic_signature(f).widgets().values())
    )
    if dlg.exec():
        out = f(**dlg.asdict())
    else:
        out = None
    return out

How about implement some of these functions, perhaps in magicgui/widgets? Or maybe implementing DialogFunctionGui and adding its support to @magicgui will be a better solution.

hanjinliu avatar Jun 14 '22 15:06 hanjinliu