Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update call button to default True when auto_call is False #194

Merged
merged 7 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions magicgui/_magicgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def magicgui(
layout: str = "vertical",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: bool = False,
Expand All @@ -41,8 +41,10 @@ def magicgui(
tooltips : bool, optional
Whether tooltips are shown when hovering over widgets. by default True
call_button : bool or str, optional
If ``True``, create an additional button that calls the original function when
clicked. If a ``str``, set the button text. by default False
If ``True``, create an additional button that calls the original
function when clicked. If a ``str``, set the button text. If None (the
default), it defaults to True when ``auto_call`` is False, and False
otherwise.
auto_call : bool, optional
If ``True``, changing any parameter in either the GUI or the widget attributes
will call the original function with the current settings. by default False
Expand All @@ -51,13 +53,13 @@ def magicgui(
by default False
main_window : bool
Whether this widget should be treated as the main app window, with menu bar,
by default True.
by default False.
app : magicgui.Application or str, optional
A backend to use, by default ``None`` (use the default backend.)
persist : bool, optional
If `True`, when parameter values change in the widget, they will be stored to
disk (in `~/.config/magicgui/cache`) and restored when the widget is loaded
again with ``persist = True``. By default, `False`.
again with ``persist = True``. By default False.

**param_options : dict of dict
Any additional keyword arguments will be used as parameter-specific options.
Expand Down Expand Up @@ -93,7 +95,7 @@ def magic_factory(
layout: str = "vertical",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: bool = False,
Expand Down
16 changes: 8 additions & 8 deletions magicgui/_magicgui.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def magicgui( # noqa
layout: str = "horizontal",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: Literal[False] = False,
Expand All @@ -43,7 +43,7 @@ def magicgui( # noqa
layout: str = "horizontal",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: Literal[False] = False,
Expand All @@ -58,7 +58,7 @@ def magicgui( # noqa
layout: str = "horizontal",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: Literal[True],
Expand All @@ -73,7 +73,7 @@ def magicgui( # noqa
layout: str = "horizontal",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: Literal[True],
Expand All @@ -88,7 +88,7 @@ def magic_factory( # noqa
layout: str = "horizontal",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: Literal[False] = False,
Expand All @@ -103,7 +103,7 @@ def magic_factory( # noqa
layout: str = "horizontal",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: Literal[False] = False,
Expand All @@ -118,7 +118,7 @@ def magic_factory( # noqa
layout: str = "horizontal",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: Literal[True],
Expand All @@ -133,7 +133,7 @@ def magic_factory( # noqa
layout: str = "horizontal",
labels: bool = True,
tooltips: bool = True,
call_button: bool | str = False,
call_button: bool | str | None = None,
auto_call: bool = False,
result_widget: bool = False,
main_window: Literal[True],
Expand Down
9 changes: 6 additions & 3 deletions magicgui/widgets/_function_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ class FunctionGui(Container, Generic[_R]):
----------
function : Callable
A callable to turn into a GUI
call_button : bool or str, optional
call_button : bool, str, or None, optional
If True, create an additional button that calls the original function when
clicked. If a ``str``, set the button text. by default False
clicked. If a ``str``, set the button text. by default False when
auto_call is True, and True otherwise.
layout : str, optional
The type of layout to use. Must be one of {'horizontal', 'vertical'}.
by default "horizontal".
Expand Down Expand Up @@ -110,7 +111,7 @@ class FunctionGui(Container, Generic[_R]):
def __init__(
self,
function: Callable[..., _R],
call_button: bool | str = False,
call_button: bool | str | None = None,
layout: str = "vertical",
labels: bool = True,
tooltips: bool = True,
Expand Down Expand Up @@ -170,6 +171,8 @@ def __init__(
# the nesting level of tqdm_mgui iterators in a given __call__
self._tqdm_depth: int = 0

if call_button is None:
call_button = not auto_call
self._call_button: PushButton | None = None
if call_button:
text = call_button if isinstance(call_button, str) else "Run"
Expand Down
34 changes: 28 additions & 6 deletions tests/test_magicgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@
from magicgui.signature import MagicSignature


def func(a: str = "works", b: int = 3, c=7.1) -> str:
return a + str(b)


@pytest.fixture
def magic_func():
"""Test function decorated by magicgui."""
decorated = magicgui(func, call_button="my_button", auto_call=True, labels=False)
return decorated

@magicgui(call_button="my_button", auto_call=True, labels=False)
def func(a: str = "works", b: int = 3, c=7.1) -> str:
return a + str(b)

return func
@pytest.fixture
def magic_func_defaults():
decorated = magicgui(func)
return decorated


@pytest.fixture
def magic_func_autocall():
decorated = magicgui(func, auto_call=True)
return decorated


def test_magicgui(magic_func):
Expand Down Expand Up @@ -48,6 +60,16 @@ def test_magicgui(magic_func):
magic_func.index(a)


def test_default_call_button_behavior(magic_func_defaults, magic_func_autocall):
assert magic_func_defaults._call_button is not None

assert magic_func_autocall._call_button is None
prior_autocall_count = magic_func_autocall.call_count
magic_func_autocall.a.value = "hello"
magic_func_autocall.b.value = 7
assert magic_func_autocall.call_count == prior_autocall_count + 2


def test_overriding_widget_type():
"""Test overriding the widget type of a parameter."""
# a will now be a LineEdit instead of a spinbox
Expand Down Expand Up @@ -351,9 +373,9 @@ def get_layout_items(gui):
return items

gui = magicgui(func, labels=labels)
assert get_layout_items(gui) == ["a", "b", "c"]
assert get_layout_items(gui) == ["a", "b", "c", "call_button"]
gui.insert(1, widgets.create_widget(name="new"))
assert get_layout_items(gui) == ["a", "new", "b", "c"]
assert get_layout_items(gui) == ["a", "new", "b", "c", "call_button"]


def test_original_function_works(magic_func):
Expand Down