def test_bind_bound_function_to_kwarg(): widget = IntSlider(value=1) def add1(value): return value + 1 def divide(divisor=2, value=0): return value / divisor bound_function = bind(divide, value=bind(add1, widget.param.value)) assert bound_function() == 1 widget.value = 3 assert bound_function() == 2
def test_bind_constant_to_arg(): def add1(value): return value + 1 bound_function = bind(add1, 1) assert bound_function() == 2 with pytest.raises(TypeError): bound_function(1)
def test_bind_two_widget_arg_with_remaining_arg(): widget = IntSlider(value=0) def add(value, value2): return value + value2 bound_function = bind(add, widget) assert bound_function(1) == 1 widget.value = 1 assert bound_function(2) == 3 assert bound_function(value2=3) == 4
def test_bind_widget_to_kwarg(): widget = IntSlider(value=0) def add1(value): return value + 1 bound_function = bind(add1, value=widget) assert bound_function() == 1 widget.value = 1 assert bound_function() == 2 with pytest.raises(TypeError): bound_function(1)
def test_tabulator_function_filter(document, comm): df = makeMixedDataFrame() table = Tabulator(df) model = table.get_root(document, comm) widget = TextInput(value='foo3') def filter_c(df, value): return df[df.C.str.contains(value)] table.add_filter(bind(filter_c, value=widget), 'C') expected = { 'index': np.array([2]), 'A': np.array([2]), 'B': np.array([0]), 'C': np.array(['foo3']), 'D': np.array(['2009-01-05T00:00:00.000000000'], dtype='datetime64[ns]').astype(np.int64) / 10e5 } for col, values in model.source.data.items(): np.testing.assert_array_equal(values, expected[col]) widget.value = 'foo1' expected = { 'index': np.array([0]), 'A': np.array([0]), 'B': np.array([0]), 'C': np.array(['foo1']), 'D': np.array(['2009-01-01T00:00:00.000000000'], dtype='datetime64[ns]').astype(np.int64) / 10e5 } for col, values in model.source.data.items(): np.testing.assert_array_equal(values, expected[col])
def test_bind_two_widgets_as_kwargs(): widget = IntSlider(value=0) widget2 = IntSlider(value=1) def add(value, value2): return value + value2 bound_function = bind(add, value=widget, value2=widget2) assert bound_function() == 1 widget.value = 1 assert bound_function() == 2 widget2.value = 2 assert bound_function() == 3 with pytest.raises(TypeError): bound_function(1, 2) assert bound_function(value2=5) == 6