|
1 |
| -from typing import Optional, Union |
| 1 | +from typing import Any, Dict, Hashable, Iterable, Mapping, Optional, Tuple, Type, Union |
2 | 2 |
|
3 | 3 | from magicgui.types import FileDialogMode
|
| 4 | +from magicgui.widgets._bases.create_widget import create_widget |
| 5 | + |
| 6 | +from ._concrete import Dialog |
4 | 7 |
|
5 | 8 |
|
6 | 9 | def show_file_dialog(
|
@@ -41,3 +44,69 @@ def show_file_dialog(
|
41 | 44 | app = use_app()
|
42 | 45 | assert app.native
|
43 | 46 | return app.get_obj("show_file_dialog")(mode, caption, start_path, filter, parent)
|
| 47 | + |
| 48 | + |
| 49 | +def request_values( |
| 50 | + values: Union[Mapping, Iterable[Tuple[Hashable, Any]]] = (), |
| 51 | + *, |
| 52 | + title: str = "", |
| 53 | + **kwargs: Union[Type, Dict] |
| 54 | +) -> Optional[Dict[str, Any]]: |
| 55 | + """Show a dialog with a set of values and request the user to enter them. |
| 56 | +
|
| 57 | + Dialog is modal and immediately blocks execution until user closes it. |
| 58 | + If the dialog is accepted, the values are returned as a dictionary, otherwise |
| 59 | + returns `None`. |
| 60 | +
|
| 61 | + See also the docstring of :func:`magicgui.widgets.create_widget` for more |
| 62 | + information. |
| 63 | +
|
| 64 | + Parameters |
| 65 | + ---------- |
| 66 | + values : Union[Mapping, Iterable[Tuple[Hashable, Any]]], optional |
| 67 | + A mapping of name to arguments to create_widget. Values can be a dict, in which |
| 68 | + case they are kwargs to `create_widget`, or a single value, in which case it is |
| 69 | + interpreted as the `annotation` in `create_widget`, by default () |
| 70 | + title : str |
| 71 | + An optional label to put at the top., by default "" |
| 72 | + **kwargs : Union[Type, Dict] |
| 73 | + Additional keyword arguments are used as name -> annotation arguments to |
| 74 | + `create_widget`. |
| 75 | +
|
| 76 | + Returns |
| 77 | + ------- |
| 78 | + Optional[Dict[str, Any]] |
| 79 | + Dictionary of values if accepted, or `None` if canceled. |
| 80 | +
|
| 81 | + Examples |
| 82 | + -------- |
| 83 | + >>> from magicgui.widgets import request_values |
| 84 | +
|
| 85 | + >>> request_values(age=int, name=str, title="Hi! Who are you?") |
| 86 | +
|
| 87 | + >>> request_values( |
| 88 | + ... age=dict(value=40), |
| 89 | + ... name=dict(annotation=str, label="Enter your name:"), |
| 90 | + ... title="Hi! Who are you?" |
| 91 | + ... ) |
| 92 | +
|
| 93 | + >>> request_values( |
| 94 | + ... values={'age': int, 'name': str}, |
| 95 | + ... title="Hi! Who are you?" |
| 96 | + ... ) |
| 97 | + """ |
| 98 | + widgets = [] |
| 99 | + if title: |
| 100 | + from . import Label |
| 101 | + |
| 102 | + widgets.append(Label(value=title)) |
| 103 | + |
| 104 | + for key, val in dict(values, **kwargs).items(): |
| 105 | + kwargs = val if isinstance(val, dict) else dict(annotation=val) |
| 106 | + kwargs.setdefault("name", key) |
| 107 | + widgets.append(create_widget(**kwargs)) # type: ignore |
| 108 | + |
| 109 | + d = Dialog(widgets=widgets) |
| 110 | + if d.exec(): |
| 111 | + return d.asdict() |
| 112 | + return None |
0 commit comments