r"""Input group. Request a set of inputs from the user at once. :param str label: Label of input group. :param list inputs: Input items. The item of the list is the call to the single input function, and the ``name`` parameter need to be passed in the single input function. :para
(label: str = '', inputs: List = None, validate: Callable[[Dict], Optional[Tuple[str, str]]] = None,
cancelable: bool = False)
| 676 | |
| 677 | |
| 678 | def input_group(label: str = '', inputs: List = None, validate: Callable[[Dict], Optional[Tuple[str, str]]] = None, |
| 679 | cancelable: bool = False): |
| 680 | r"""Input group. Request a set of inputs from the user at once. |
| 681 | |
| 682 | :param str label: Label of input group. |
| 683 | :param list inputs: Input items. |
| 684 | The item of the list is the call to the single input function, and the ``name`` parameter need to be passed in the single input function. |
| 685 | :param callable validate: validation function for the group. If provided, the validation function will be called when the user submits the form. |
| 686 | |
| 687 | Function signature: ``callback(data) -> (name, error_msg)``. |
| 688 | ``validate`` receives the value of the entire group as a parameter. When the form value is valid, it returns ``None``. |
| 689 | When an input item's value is invalid, it returns the ``name`` value of the item and an error message. |
| 690 | For example: |
| 691 | |
| 692 | .. exportable-codeblock:: |
| 693 | :name: input_group-valid_func |
| 694 | :summary: `input_group()` form validation |
| 695 | |
| 696 | def check_form(data): |
| 697 | if len(data['name']) > 6: |
| 698 | return ('name', 'Name to long!') |
| 699 | if data['age'] <= 0: |
| 700 | return ('age', 'Age cannot be negative!') |
| 701 | |
| 702 | data = input_group("Basic info",[ |
| 703 | input('Input your name', name='name'), |
| 704 | input('Repeat your age', name='age', type=NUMBER) |
| 705 | ], validate=check_form) |
| 706 | |
| 707 | put_text(data['name'], data['age']) |
| 708 | |
| 709 | :param bool cancelable: Whether the form can be cancelled. Default is ``False``. |
| 710 | If ``cancelable=True``, a "Cancel" button will be displayed at the bottom of the form. |
| 711 | |
| 712 | Note: If the last input item in the group is `actions()`, ``cancelable`` will be ignored. |
| 713 | |
| 714 | :return: If the user cancels the form, return ``None``, otherwise a ``dict`` is returned, |
| 715 | whose key is the ``name`` of the input item, and whose value is the value of the input item. |
| 716 | """ |
| 717 | assert inputs is not None, 'Required `inputs` parameter in input_group()' |
| 718 | |
| 719 | spec_inputs = [] |
| 720 | preprocess_funcs = {} |
| 721 | item_valid_funcs = {} |
| 722 | onchange_funcs = {} |
| 723 | for single_input_return in inputs: |
| 724 | input_kwargs = single_input_kwargs(single_input_return) |
| 725 | |
| 726 | assert all( |
| 727 | k in (input_kwargs or {}) |
| 728 | for k in ('item_spec', 'preprocess_func', 'valid_func', 'onchange_func') |
| 729 | ), "`inputs` value error in `input_group`. Did you forget to add `name` parameter in input function?" |
| 730 | |
| 731 | input_name = input_kwargs['item_spec']['name'] |
| 732 | assert input_name, "`name` can not be empty!" |
| 733 | if input_name in preprocess_funcs: |
| 734 | raise ValueError('Duplicated input item name "%s" in same input group!' % input_name) |
| 735 | preprocess_funcs[input_name] = input_kwargs['preprocess_func'] |
no test coverage detected
searching dependent graphs…