|
| 1 | +# |
| 2 | +# Monkey patching of register_callback |
| 3 | +# |
| 4 | + |
| 5 | +import dash._callback |
| 6 | + |
| 7 | +from dash._callback import (handle_grouped_callback_args, |
| 8 | + insert_callback, |
| 9 | + NoUpdate, |
| 10 | + ) |
| 11 | +import collections |
| 12 | +from functools import wraps |
| 13 | + |
| 14 | +from dash.dependencies import ( |
| 15 | + handle_callback_args, |
| 16 | + handle_grouped_callback_args, |
| 17 | + Output, |
| 18 | +) |
| 19 | +from dash.exceptions import PreventUpdate |
| 20 | + |
| 21 | +from dash._grouping import ( |
| 22 | + flatten_grouping, |
| 23 | + make_grouping_by_index, |
| 24 | + grouping_len, |
| 25 | +) |
| 26 | +from dash._utils import ( |
| 27 | + create_callback_id, |
| 28 | + stringify_id, |
| 29 | + to_json, |
| 30 | +) |
| 31 | + |
| 32 | +from dash import _validate |
| 33 | + |
| 34 | + |
| 35 | +def register_callback( |
| 36 | + callback_list, callback_map, config_prevent_initial_callbacks, *_args, **_kwargs |
| 37 | +): |
| 38 | + ( |
| 39 | + output, |
| 40 | + flat_inputs, |
| 41 | + flat_state, |
| 42 | + inputs_state_indices, |
| 43 | + prevent_initial_call, |
| 44 | + ) = handle_grouped_callback_args(_args, _kwargs) |
| 45 | + if isinstance(output, Output): |
| 46 | + # Insert callback with scalar (non-multi) Output |
| 47 | + insert_output = output |
| 48 | + multi = False |
| 49 | + else: |
| 50 | + # Insert callback as multi Output |
| 51 | + insert_output = flatten_grouping(output) |
| 52 | + multi = True |
| 53 | + |
| 54 | + output_indices = make_grouping_by_index(output, list(range(grouping_len(output)))) |
| 55 | + callback_id = insert_callback( |
| 56 | + callback_list, |
| 57 | + callback_map, |
| 58 | + config_prevent_initial_callbacks, |
| 59 | + insert_output, |
| 60 | + output_indices, |
| 61 | + flat_inputs, |
| 62 | + flat_state, |
| 63 | + inputs_state_indices, |
| 64 | + prevent_initial_call, |
| 65 | + ) |
| 66 | + |
| 67 | + # pylint: disable=too-many-locals |
| 68 | + def wrap_func(func): |
| 69 | + @wraps(func) |
| 70 | + def add_context(*args, **kwargs): |
| 71 | + output_spec = kwargs.pop("outputs_list") |
| 72 | + _validate.validate_output_spec(insert_output, output_spec, Output) |
| 73 | + |
| 74 | + func_args, func_kwargs = _validate.validate_and_group_input_args( |
| 75 | + args, inputs_state_indices |
| 76 | + ) |
| 77 | + |
| 78 | + func_kwargs = {**func_kwargs, |
| 79 | + **kwargs} |
| 80 | + |
| 81 | + # don't touch the comment on the next line - used by debugger |
| 82 | + output_value = func(*func_args, **func_kwargs) # %% callback invoked %% |
| 83 | + |
| 84 | + if isinstance(output_value, NoUpdate): |
| 85 | + raise PreventUpdate |
| 86 | + |
| 87 | + if not multi: |
| 88 | + output_value, output_spec = [output_value], [output_spec] |
| 89 | + flat_output_values = output_value |
| 90 | + else: |
| 91 | + if isinstance(output_value, (list, tuple)): |
| 92 | + # For multi-output, allow top-level collection to be |
| 93 | + # list or tuple |
| 94 | + output_value = list(output_value) |
| 95 | + |
| 96 | + # Flatten grouping and validate grouping structure |
| 97 | + flat_output_values = flatten_grouping(output_value, output) |
| 98 | + |
| 99 | + _validate.validate_multi_return( |
| 100 | + output_spec, flat_output_values, callback_id |
| 101 | + ) |
| 102 | + |
| 103 | + component_ids = collections.defaultdict(dict) |
| 104 | + has_update = False |
| 105 | + for val, spec in zip(flat_output_values, output_spec): |
| 106 | + if isinstance(val, NoUpdate): |
| 107 | + continue |
| 108 | + for vali, speci in ( |
| 109 | + zip(val, spec) if isinstance(spec, list) else [[val, spec]] |
| 110 | + ): |
| 111 | + if not isinstance(vali, NoUpdate): |
| 112 | + has_update = True |
| 113 | + id_str = stringify_id(speci["id"]) |
| 114 | + component_ids[id_str][speci["property"]] = vali |
| 115 | + |
| 116 | + if not has_update: |
| 117 | + raise PreventUpdate |
| 118 | + |
| 119 | + response = {"response": component_ids, "multi": True} |
| 120 | + |
| 121 | + try: |
| 122 | + jsonResponse = to_json(response) |
| 123 | + except TypeError: |
| 124 | + _validate.fail_callback_output(output_value, output) |
| 125 | + |
| 126 | + return jsonResponse |
| 127 | + |
| 128 | + callback_map[callback_id]["callback"] = add_context |
| 129 | + |
| 130 | + return func |
| 131 | + |
| 132 | + return wrap_func |
| 133 | + |
| 134 | + |
| 135 | +dash._callback.register_callback = register_callback |
0 commit comments