def test_data_link_idempotent(self): table1 = Table([], 'A', 'B') table2 = Table([], 'C', 'D') link1 = DataLink(table1, table2) DataLink(table1, table2) self.assertEqual(len(Link.registry[table1]), 1) self.assertIn(link1, Link.registry[table1])
def test_data_link_poly_table_on_unlinked_clone(self): arr1 = np.random.rand(10, 2) arr2 = np.random.rand(10, 2) polys = Polygons([arr1, arr2]) table = Table([('A', 1), ('B', 2)], 'A', 'B') DataLink(polys, table) layout = polys.clone() + table.clone(link=False) plot = bokeh_renderer.get_plot(layout) cds = list(plot.state.select({'type': ColumnDataSource})) self.assertEqual(len(cds), 2)
def test_table_plot(self): table = Table(([1, 2, 3], [1., 2., 3.], ['A', 'B', 'C']), ['x', 'y'], 'z') plot = bokeh_renderer.get_plot(table) dims = table.dimensions() formatters = (NumberFormatter, NumberFormatter, StringFormatter) editors = (IntEditor, NumberEditor, StringEditor) for dim, fmt, edit, column in zip(dims, formatters, editors, plot.state.columns): self.assertEqual(column.title, dim.pprint_label) self.assertIsInstance(column.formatter, fmt) self.assertIsInstance(column.editor, edit)
def test_replace_object(self): annotator = PathAnnotator(Path([]), annotations=['Label'], vertex_annotations=['Value']) annotator.object = Path([(1, 2), (2, 3), (0, 0)]) self.assertIn('Label', annotator.object) expected = Table([('')], kdims=['Label'], label='PathAnnotator') self.assertEqual(annotator._table, expected) expected = Table([], ['x', 'y'], 'Value', label='PathAnnotator Vertices') self.assertEqual(annotator._vertex_table, expected) self.assertIs(annotator._link.target, annotator._table) self.assertIs(annotator._vertex_link.target, annotator._vertex_table)
def test_table_plot_escaped_dimension(self): table = Table([1, 2, 3], ['A Dimension']) plot = bokeh_renderer.get_plot(table) source = plot.handles['source'] renderer = plot.handles['glyph_renderer'] self.assertEqual( list(source.data.keys())[0], renderer.columns[0].field)
def test_replace_object(self): annotator = PointAnnotator(Points([]), annotations=['Label']) annotator.object = Points([(1, 2)]) self.assertIn('Label', annotator.object) expected = Table([(1, 2, '')], ['x', 'y'], vdims=['Label'], label='PointAnnotator') self.assertEqual(annotator._table, expected) self.assertIs(annotator._link.target, annotator._table)
def test_table_plot_datetimes(self): table = Table([dt.now(), dt.now()], 'Date') plot = bokeh_renderer.get_plot(table) column = plot.state.columns[0] self.assertEqual(column.title, 'Date') self.assertIsInstance(column.formatter, DateFormatter) self.assertIsInstance(column.editor, DateEditor)
def test_table_plot_callback(self): table = Table(([1, 2, 3], [1., 2., 3.], ['A', 'B', 'C']), ['x', 'y'], 'z') CDSStream(source=table) plot = bokeh_renderer.get_plot(table) self.assertEqual(len(plot.callbacks), 1) self.assertIsInstance(plot.callbacks[0], CDSCallback)
def test_table_state(self): table = Table([(0, 1), (1, 2), (2, 3)], 'x', 'y') state = self._get_plot_state(table) self.assertEqual(state['data'][0]['type'], 'table') self.assertEqual(state['data'][0]['header']['values'], ['x', 'y']) self.assertEqual(state['data'][0]['cells']['values'], [['0', '1', '2'], ['1', '2', '3']])
def test_data_link_mismatch(self): polys = Polygons([np.random.rand(10, 2)]) table = Table([('A', 1), ('B', 2)], 'A', 'B') DataLink(polys, table) layout = polys + table with self.assertRaises(Exception): bokeh_renderer.get_plot(layout)
def test_table_update_selected(self): stream = Stream.define('Selected', selected=[])() table = Table([(0, 0), (1, 1), (2, 2)], ['x', 'y']).apply.opts(selected=stream.param.selected) plot = bokeh_renderer.get_plot(table) cds = plot.handles['cds'] self.assertEqual(cds.selected.indices, []) stream.event(selected=[0, 2]) self.assertEqual(cds.selected.indices, [0, 2])
def test_data_link_list(self): path = Path([[(0, 0, 0), (1, 1, 1), (2, 2, 2)]], vdims='color').options(color='color') table = Table([('A', 1), ('B', 2)], 'A', 'B') DataLink(path, table) layout = path + table plot = bokeh_renderer.get_plot(layout) path_plot, table_plot = (sp.subplots['main'] for sp in plot.subplots.values()) self.assertIs(path_plot.handles['source'], table_plot.handles['source'])
def test_table_change_columns(self): lengths = {'a': 1, 'b': 2, 'c': 3} table = DynamicMap(lambda a: Table(range(lengths[a]), a), kdims=['a']).redim.values(a=['a', 'b', 'c']) plot = bokeh_renderer.get_plot(table) self.assertEqual(sorted(plot.handles['source'].data.keys()), ['a']) self.assertEqual(plot.handles['table'].columns[0].title, 'a') plot.update(('b',)) self.assertEqual(sorted(plot.handles['source'].data.keys()), ['b']) self.assertEqual(plot.handles['table'].columns[0].title, 'b')
def test_selection1d_resolves_table(self): table = Table([1, 2, 3], 'x') Selection1D(source=table) plot = bokeh_server_renderer.get_plot(table) selected = Selection(indices=[0, 2]) callback = plot.callbacks[0] spec = callback.attributes['index'] resolved = callback.resolve_attr_spec(spec, selected, model=selected) self.assertEqual(resolved, {'id': selected.ref['id'], 'value': [0, 2]})
def test_callback_on_table_is_attached(self): table = Table([1, 2, 3], 'x') selection = Selection1D(source=table) plot = bokeh_renderer.get_plot(table) self.assertEqual(len(plot.callbacks), 1) self.assertIsInstance(plot.callbacks[0], Selection1DCallback) self.assertIn(selection, plot.callbacks[0].streams) callbacks = plot.handles['selected'].js_property_callbacks self.assertIn('change:indices', callbacks) self.assertIn(plot.id, callbacks['change:indices'][0].code)
def test_point_draw_shared_datasource_callback(self): points = Points([1, 2, 3]) table = Table(points.data, ['x', 'y']) layout = (points + table).options(shared_datasource=True, clone=False) PointDraw(source=points) self.assertIs(points.data, table.data) plot = bokeh_renderer.get_plot(layout) point_plot = plot.subplots[(0, 0)].subplots['main'] table_plot = plot.subplots[(0, 1)].subplots['main'] self.assertIs(point_plot.handles['source'], table_plot.handles['source'])
def test_data_link_dynamicmap_table(self): dmap = DynamicMap(lambda X: Points([(0, X)]), kdims='X').redim.range(X=(-1, 1)) table = Table([(-1,)], vdims='y') DataLink(dmap, table) layout = dmap + table plot = bokeh_renderer.get_plot(layout) cds = list(plot.state.select({'type': ColumnDataSource})) self.assertEqual(len(cds), 1) data = {'x': np.array([0]), 'y': np.array([-1])} for k, v in cds[0].data.items(): self.assertEqual(v, data[k])
def test_data_link_poly_table(self): arr1 = np.random.rand(10, 2) arr2 = np.random.rand(10, 2) polys = Polygons([arr1, arr2]) table = Table([('A', 1), ('B', 2)], 'A', 'B') DataLink(polys, table) layout = polys + table plot = bokeh_renderer.get_plot(layout) cds = list(plot.state.select({'type': ColumnDataSource})) self.assertEqual(len(cds), 1) merged_data = {'xs': [[[arr1[:, 0]]], [[arr2[:, 0]]]], 'ys': [[[arr1[:, 1]]], [[arr2[:, 1]]]], 'A': np.array(['A', 'B']), 'B': np.array([1, 2])} for k, v in cds[0].data.items(): self.assertEqual(v, merged_data[k])
def test_layout_selection_points_table(self): points = Points(self.data) table = Table(self.data) lnk_sel = link_selections.instance(selected_color="#aa0000", unselected_color='#ff0000') linked = lnk_sel(points + table) current_obj = linked[()] # Check initial base points self.check_base_points_like(current_obj[0][()].Points.I, lnk_sel) # Check initial selection points self.check_overlay_points_like(current_obj[0][()].Points.II, lnk_sel, self.data) # Check initial table self.assertEqual(self.element_color(current_obj[1][()]), [lnk_sel.selected_color] * len(self.data)) # Select first and third point selectionxy = TestLinkSelections.get_value_with_key_type( lnk_sel._selection_expr_streams, hv.Points).input_streams[0].input_stream.input_streams[0] selectionxy.event(bounds=(0, 0, 4, 2)) current_obj = linked[()] # Check base points self.check_base_points_like(current_obj[0][()].Points.I, lnk_sel) # Check selection points self.check_overlay_points_like(current_obj[0][()].Points.II, lnk_sel, self.data.iloc[[0, 2]]) # Check selected table self.assertEqual(self.element_color(current_obj[1][()]), [ lnk_sel.selected_color, lnk_sel.unselected_color, lnk_sel.selected_color, ])
def table(self, x=None, y=None, data=None): allowed = ['width', 'height'] opts = {k: v for k, v in self._plot_opts.items() if k in allowed} data = self.data if data is None else data return Table(data, self.columns, []).opts(plot=opts)
def test_visible(self): element = Table([(0, 1), (1, 2), (2, 3)], 'x', 'y').options(visible=False) state = self._get_plot_state(element) self.assertEqual(state['data'][0]['visible'], False)
def test_annotation_type(self): annotator = PointAnnotator(Points([(1, 2)]), annotations={'Int': int}) expected = Table([(1, 2, 0)], ['x', 'y'], vdims=['Int'], label='PointAnnotator') self.assertEqual(annotator._table, expected)
def test_table_selected(self): table = Table([(0, 0), (1, 1), (2, 2)], ['x', 'y']).opts(selected=[0, 2]) plot = bokeh_renderer.get_plot(table) cds = plot.handles['cds'] self.assertEqual(cds.selected.indices, [0, 2])