Skip to content

Commit c50a7e2

Browse files
Update call button to default True when auto_call is False (#194)
* Update call button default in signature/docstring * Minor docstring fixes * Update type annotations * Add new call_button default to FunctionGui class * Add test for new default behavior * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add call_button to default layout test Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 61d8f4f commit c50a7e2

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

magicgui/_magicgui.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def magicgui(
1818
layout: str = "vertical",
1919
labels: bool = True,
2020
tooltips: bool = True,
21-
call_button: bool | str = False,
21+
call_button: bool | str | None = None,
2222
auto_call: bool = False,
2323
result_widget: bool = False,
2424
main_window: bool = False,
@@ -41,8 +41,10 @@ def magicgui(
4141
tooltips : bool, optional
4242
Whether tooltips are shown when hovering over widgets. by default True
4343
call_button : bool or str, optional
44-
If ``True``, create an additional button that calls the original function when
45-
clicked. If a ``str``, set the button text. by default False
44+
If ``True``, create an additional button that calls the original
45+
function when clicked. If a ``str``, set the button text. If None (the
46+
default), it defaults to True when ``auto_call`` is False, and False
47+
otherwise.
4648
auto_call : bool, optional
4749
If ``True``, changing any parameter in either the GUI or the widget attributes
4850
will call the original function with the current settings. by default False
@@ -51,13 +53,13 @@ def magicgui(
5153
by default False
5254
main_window : bool
5355
Whether this widget should be treated as the main app window, with menu bar,
54-
by default True.
56+
by default False.
5557
app : magicgui.Application or str, optional
5658
A backend to use, by default ``None`` (use the default backend.)
5759
persist : bool, optional
5860
If `True`, when parameter values change in the widget, they will be stored to
5961
disk (in `~/.config/magicgui/cache`) and restored when the widget is loaded
60-
again with ``persist = True``. By default, `False`.
62+
again with ``persist = True``. By default False.
6163
6264
**param_options : dict of dict
6365
Any additional keyword arguments will be used as parameter-specific options.
@@ -93,7 +95,7 @@ def magic_factory(
9395
layout: str = "vertical",
9496
labels: bool = True,
9597
tooltips: bool = True,
96-
call_button: bool | str = False,
98+
call_button: bool | str | None = None,
9799
auto_call: bool = False,
98100
result_widget: bool = False,
99101
main_window: bool = False,

magicgui/_magicgui.pyi

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def magicgui( # noqa
2828
layout: str = "horizontal",
2929
labels: bool = True,
3030
tooltips: bool = True,
31-
call_button: bool | str = False,
31+
call_button: bool | str | None = None,
3232
auto_call: bool = False,
3333
result_widget: bool = False,
3434
main_window: Literal[False] = False,
@@ -43,7 +43,7 @@ def magicgui( # noqa
4343
layout: str = "horizontal",
4444
labels: bool = True,
4545
tooltips: bool = True,
46-
call_button: bool | str = False,
46+
call_button: bool | str | None = None,
4747
auto_call: bool = False,
4848
result_widget: bool = False,
4949
main_window: Literal[False] = False,
@@ -58,7 +58,7 @@ def magicgui( # noqa
5858
layout: str = "horizontal",
5959
labels: bool = True,
6060
tooltips: bool = True,
61-
call_button: bool | str = False,
61+
call_button: bool | str | None = None,
6262
auto_call: bool = False,
6363
result_widget: bool = False,
6464
main_window: Literal[True],
@@ -73,7 +73,7 @@ def magicgui( # noqa
7373
layout: str = "horizontal",
7474
labels: bool = True,
7575
tooltips: bool = True,
76-
call_button: bool | str = False,
76+
call_button: bool | str | None = None,
7777
auto_call: bool = False,
7878
result_widget: bool = False,
7979
main_window: Literal[True],
@@ -88,7 +88,7 @@ def magic_factory( # noqa
8888
layout: str = "horizontal",
8989
labels: bool = True,
9090
tooltips: bool = True,
91-
call_button: bool | str = False,
91+
call_button: bool | str | None = None,
9292
auto_call: bool = False,
9393
result_widget: bool = False,
9494
main_window: Literal[False] = False,
@@ -103,7 +103,7 @@ def magic_factory( # noqa
103103
layout: str = "horizontal",
104104
labels: bool = True,
105105
tooltips: bool = True,
106-
call_button: bool | str = False,
106+
call_button: bool | str | None = None,
107107
auto_call: bool = False,
108108
result_widget: bool = False,
109109
main_window: Literal[False] = False,
@@ -118,7 +118,7 @@ def magic_factory( # noqa
118118
layout: str = "horizontal",
119119
labels: bool = True,
120120
tooltips: bool = True,
121-
call_button: bool | str = False,
121+
call_button: bool | str | None = None,
122122
auto_call: bool = False,
123123
result_widget: bool = False,
124124
main_window: Literal[True],
@@ -133,7 +133,7 @@ def magic_factory( # noqa
133133
layout: str = "horizontal",
134134
labels: bool = True,
135135
tooltips: bool = True,
136-
call_button: bool | str = False,
136+
call_button: bool | str | None = None,
137137
auto_call: bool = False,
138138
result_widget: bool = False,
139139
main_window: Literal[True],

magicgui/widgets/_function_gui.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ class FunctionGui(Container, Generic[_R]):
6767
----------
6868
function : Callable
6969
A callable to turn into a GUI
70-
call_button : bool or str, optional
70+
call_button : bool, str, or None, optional
7171
If True, create an additional button that calls the original function when
72-
clicked. If a ``str``, set the button text. by default False
72+
clicked. If a ``str``, set the button text. by default False when
73+
auto_call is True, and True otherwise.
7374
layout : str, optional
7475
The type of layout to use. Must be one of {'horizontal', 'vertical'}.
7576
by default "horizontal".
@@ -110,7 +111,7 @@ class FunctionGui(Container, Generic[_R]):
110111
def __init__(
111112
self,
112113
function: Callable[..., _R],
113-
call_button: bool | str = False,
114+
call_button: bool | str | None = None,
114115
layout: str = "vertical",
115116
labels: bool = True,
116117
tooltips: bool = True,
@@ -170,6 +171,8 @@ def __init__(
170171
# the nesting level of tqdm_mgui iterators in a given __call__
171172
self._tqdm_depth: int = 0
172173

174+
if call_button is None:
175+
call_button = not auto_call
173176
self._call_button: PushButton | None = None
174177
if call_button:
175178
text = call_button if isinstance(call_button, str) else "Run"

tests/test_magicgui.py

+28-6
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,27 @@
1212
from magicgui.signature import MagicSignature
1313

1414

15+
def func(a: str = "works", b: int = 3, c=7.1) -> str:
16+
return a + str(b)
17+
18+
1519
@pytest.fixture
1620
def magic_func():
1721
"""Test function decorated by magicgui."""
22+
decorated = magicgui(func, call_button="my_button", auto_call=True, labels=False)
23+
return decorated
1824

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

23-
return func
26+
@pytest.fixture
27+
def magic_func_defaults():
28+
decorated = magicgui(func)
29+
return decorated
30+
31+
32+
@pytest.fixture
33+
def magic_func_autocall():
34+
decorated = magicgui(func, auto_call=True)
35+
return decorated
2436

2537

2638
def test_magicgui(magic_func):
@@ -48,6 +60,16 @@ def test_magicgui(magic_func):
4860
magic_func.index(a)
4961

5062

63+
def test_default_call_button_behavior(magic_func_defaults, magic_func_autocall):
64+
assert magic_func_defaults._call_button is not None
65+
66+
assert magic_func_autocall._call_button is None
67+
prior_autocall_count = magic_func_autocall.call_count
68+
magic_func_autocall.a.value = "hello"
69+
magic_func_autocall.b.value = 7
70+
assert magic_func_autocall.call_count == prior_autocall_count + 2
71+
72+
5173
def test_overriding_widget_type():
5274
"""Test overriding the widget type of a parameter."""
5375
# a will now be a LineEdit instead of a spinbox
@@ -351,9 +373,9 @@ def get_layout_items(gui):
351373
return items
352374

353375
gui = magicgui(func, labels=labels)
354-
assert get_layout_items(gui) == ["a", "b", "c"]
376+
assert get_layout_items(gui) == ["a", "b", "c", "call_button"]
355377
gui.insert(1, widgets.create_widget(name="new"))
356-
assert get_layout_items(gui) == ["a", "new", "b", "c"]
378+
assert get_layout_items(gui) == ["a", "new", "b", "c", "call_button"]
357379

358380

359381
def test_original_function_works(magic_func):

0 commit comments

Comments
 (0)