Exemple #1
0
def make_grouping_attr_source(int_grouping):
    size = grouping_len(int_grouping)
    props = make_flat_values(size)
    vals = list(range(100, 100 + size))
    letter_grouping = make_grouping_by_index(int_grouping, props)
    Source = namedtuple("Source", props)
    return letter_grouping, Source(*vals)
Exemple #2
0
def test_flatten_odd_value():
    # Anything other than tuple and dict should be treated as a
    # scalar and passed through
    expected = [0, sum, Input("foo", "bar")]
    vals_collection = (0, (sum, Input("foo", "bar")))
    result = flatten_grouping(vals_collection)
    assert expected == result
    assert len(result) == grouping_len(vals_collection)
Exemple #3
0
def test_flatten_mixed(mixed_grouping_size):
    grouping, size = mixed_grouping_size
    expected = list(range(size))
    result = flatten_grouping(grouping)
    assert expected == result
    assert len(result) == grouping_len(grouping)
Exemple #4
0
def test_make_schema_mixed(mixed_grouping_size):
    grouping, size = mixed_grouping_size
    result = make_schema_with_nones(grouping)
    expected = make_grouping_by_index(
        grouping, [None for _ in range(grouping_len(grouping))])
    assert expected == result
Exemple #5
0
def test_flatten_scalar(scalar_grouping_size):
    grouping, size = scalar_grouping_size
    expected = list(range(size))
    result = flatten_grouping(grouping)
    assert expected == result
    assert len(result) == grouping_len(grouping)
Exemple #6
0
def make_numbers_grouping(grouping, offset=0):
    return make_grouping_by_index(
        grouping, list(range(offset, offset + grouping_len(grouping))))
Exemple #7
0
def make_letters_grouping(grouping, upper=False):
    return make_grouping_by_index(
        grouping, make_letters(grouping_len(grouping), upper=upper))
Exemple #8
0
def check_component_props_as_groupings(
    app,
    input_grouping,
    state_grouping,
    output_grouping,
    template,
    input_form="list",
    output_form="list",
):
    # Compute grouping sizes
    input_size = grouping_len(input_grouping)
    state_size = grouping_len(state_grouping)
    output_size = grouping_len(output_grouping)

    # Make buttons
    input_button, input_prop = button_component_prop_for_grouping(
        input_grouping)
    state_button, state_prop = button_component_prop_for_grouping(
        state_grouping)
    output_button, output_prop = button_component_prop_for_grouping(
        output_grouping)

    # Make flat dependencies
    flat_input_deps = flat_deps(input_button, input_prop, "input") + make_deps(
        Input, 1)
    flat_state_deps = flat_deps(state_button, state_prop, "state")
    flat_output_deps = make_deps(Output, 1) + flat_deps(
        output_button, output_prop, "output")

    # Build grouped dependency lists
    grouped_input_deps = [
        dl.Input(input_button, input_prop), flat_input_deps[-1]
    ]
    grouped_state_deps = [dl.State(state_button, state_prop)]
    grouped_output_deps = [
        flat_output_deps[0],
        dl.Output(output_button, output_prop)
    ]

    # Build flat input/output values (state is part of input now)
    flat_input_values = (make_letters(input_size) + ["-"] +
                         make_letters(state_size, upper=True))
    flat_output_values = [-1] + list(range(5, output_size + 5))

    # Build grouped input/output values (state is part of input)
    grouped_input_values = [
        make_letters_grouping(input_grouping),
        "-",
        make_letters_grouping(state_grouping, upper=True),
    ]
    grouped_output_values = [
        -1, make_numbers_grouping(output_grouping, offset=5)
    ]

    # Handle input data forms
    if input_form == "dict":
        only_input_keys = make_letters(len(grouped_input_deps), upper=True)
        grouped_input_deps = {
            k: v
            for k, v in zip(only_input_keys, grouped_input_deps)
        }

        state_keys = make_letters(len(grouped_state_deps), upper=False)
        grouped_state_deps = {
            k: v
            for k, v in zip(state_keys, grouped_state_deps)
        }
        input_value_keys = only_input_keys + state_keys

        grouped_input_values = {
            k: v
            for k, v in zip(input_value_keys, grouped_input_values)
        }

    if output_form == "scalar":
        # Remove first extra scalar value, leave only grouped value as scalar
        grouped_output_deps = grouped_output_deps[1]
        grouped_output_values = grouped_output_values[1]
        flat_output_values = flat_output_values[1:]
        flat_output_deps = flat_output_deps[1:]
    elif output_form == "dict":
        grouped_output_deps = {
            k: v
            for k, v in zip(make_letters(len(grouped_output_deps), upper=True),
                            grouped_output_deps)
        }
        grouped_output_values = {
            k: v
            for k, v in zip(
                make_letters(len(grouped_output_deps), upper=True),
                grouped_output_values,
            )
        }

    # Build mock function with grouped output as return values
    fn = mock_fn_with_return(grouped_output_values)

    # Wrap callback with grouped dependency specifications
    fn_wrapper = dl.callback(
        app,
        output=grouped_output_deps,
        inputs=grouped_input_deps,
        state=grouped_state_deps,
        template=template,
    )(fn)

    # call flat version (like dash.Dash.callback would)
    assert fn_wrapper._flat_fn(*flat_input_values) == flat_output_values

    # Check that mock function was called with expected grouped values
    args, kwargs = fn.call_args
    if input_form == "list":
        assert args == tuple(grouped_input_values)
        assert not kwargs
    elif input_form == "dict":
        assert kwargs == grouped_input_values
        assert not args

    # Check order of dependencies
    assert_deps_eq(fn_wrapper._flat_input_deps, flat_input_deps)
    assert_deps_eq(fn_wrapper._flat_state_deps, flat_state_deps)
    assert_deps_eq(fn_wrapper._flat_output_deps, flat_output_deps)
Exemple #9
0
def button_component_prop_for_grouping(grouping):
    props_grouping = make_grouping_by_index(
        grouping, button_props[:grouping_len(grouping)])
    return html.Button(), props_grouping