File: chaining.py

package info (click to toggle)
magicgui 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,796 kB
  • sloc: python: 11,202; makefile: 11; sh: 9
file content (52 lines) | stat: -rw-r--r-- 1,423 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""# Chaining functions together

This example demonstrates chaining multiple functions together.
"""

from magicgui import magicgui, widgets


@magicgui(auto_call=True)
def func_a(x: int = 64, y: int = 64):
    """Callable function A."""
    print("calling func_a")
    return x + y


@magicgui(auto_call=True, input={"visible": False, "label": " ", "max": 100000})
def func_b(input: int, mult=1.0):
    """Callable function B."""
    print("calling func_b")
    result = input * mult
    # since these function defs live in globals(), you can update them directly
    func_c.input.value = result
    return result


# alternatively, you can the `widget.called` signal to connect a callback function
# where the result of the function being called is at `value`
@func_a.called.connect
def _on_func_a(value: str):
    func_b.input.value = value


@magicgui(
    auto_call=True,
    input={"visible": False, "max": 100000},
    result_widget=True,
    labels=False,
)
def func_c(input: int, format: str = "({} + {}) * {} is {}") -> str:
    """Callable function C."""
    print("calling func_c\n")
    return format.format(func_a.x.value, func_a.y.value, func_b.mult.value, input)


container = widgets.Container(
    widgets=[func_a, func_b, func_c], layout="vertical", labels=False
)
container.native.setMinimumWidth(500)
func_a()
container.show(run=True)

# notice which functions get called when you change each widget.