def test_save_embed_json(tmpdir): checkbox = Checkbox() string = Str() checkbox.link(string, value='object') panel = Row(checkbox, string) filename = os.path.join(str(tmpdir), 'test.html') panel.save(filename, embed=True, embed_json=True, save_path=str(tmpdir)) assert os.path.isfile(filename) paths = glob.glob(os.path.join(str(tmpdir), '*')) paths.remove(filename) assert len(paths) == 1 json_files = sorted(glob.glob(os.path.join(paths[0], '*.json'))) assert len(json_files) == 2 for jf, v in zip(json_files, ('False', 'True')): with open(jf) as f: state = json.load(f) assert 'content' in state assert 'events' in state['content'] events = json.loads(state['content'])['events'] assert len(events) == 1 event = events[0] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['new'] == '<pre>%s</pre>' % v
def panel_row(self): if self._panel_row is None: if self._panel_row_active: self._panel_row = Row(self.view, self.param_control) else: self._panel_row = Row() return self._panel_row
def test_embed_select_str_link_two_steps(document, comm): select = Select(options=['A', 'B', 'C']) string1 = Str() select.link(string1, value='object') string2 = Str() string1.link(string2, object='object') panel = Row(select, string1, string2) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document) _, state = document.roots assert set(state.state) == {'A', 'B', 'C'} for k, v in state.state.items(): content = json.loads(v['content']) assert 'events' in content events = content['events'] assert len(events) == 2 event = events[0] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['model'] == model.children[1].ref assert event['new'] == '<pre>%s</pre>' % k event = events[1] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['model'] == model.children[2].ref assert event['new'] == '<pre>%s</pre>' % k
def test_embed_slider_str_link(document, comm): slider = FloatSlider(start=0, end=10) string = Str() def link(target, event): target.object = event.new slider.link(string, callbacks={'value': link}) panel = Row(slider, string) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document) _, state = document.roots assert set(state.state) == {0, 1, 2} values = [0, 5, 10] for k, v in state.state.items(): content = json.loads(v['content']) assert 'events' in content events = content['events'] assert len(events) == 1 event = events[0] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['model'] == model.children[1].ref assert event['new'] == '<pre>%.1f</pre>' % values[k]
def test_embed_param_jslink(document, comm): select = Select(options=['A', 'B', 'C']) params = Param(select, parameters=['disabled']).layout panel = Row(select, params) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document) assert len(document.roots) == 1 ref = model.ref['id'] cbs = list(model.select({'type': CustomJS})) assert len(cbs) == 2 cb1, cb2 = cbs cb1, cb2 = (cb1, cb2) if select._models[ref][0] is cb1.args['target'] else (cb2, cb1) assert cb1.code == """ var value = source['active']; value = value.indexOf(0) >= 0; value = value; try { var property = target.properties['disabled']; if (property !== undefined) { property.validate(value); } } catch(err) { console.log('WARNING: Could not set disabled on target, raised error: ' + err); return; } try { target['disabled'] = value; } catch(err) { console.log(err) } """ assert cb2.code == """
def test_embed_float_slider_explicit_values(document, comm): select = FloatSlider() string = Str() def link(target, event): target.object = event.new select.link(string, callbacks={'value': link}) panel = Row(select, string) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document, states={select: [0.1, 0.7, 1]}) _, state = document.roots assert set(state.state) == {0, 1, 2} states = {0: 0.1, 1: 0.7, 2: 1} for (k, v) in state.state.items(): content = json.loads(v['content']) assert 'events' in content events = content['events'] assert len(events) == 1 event = events[0] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['model'] == model.children[1].ref assert event['new'] == '<pre>%s</pre>' % states[k]
def test_save_embed(tmpdir): checkbox = Checkbox() string = Str() checkbox.link(string, value='object') panel = Row(checkbox, string) filename = os.path.join(str(tmpdir), 'test.html') panel.save(filename, embed=True) assert os.path.isfile(filename)
def test_save_embed_bytesio(): checkbox = Checkbox() string = Str() checkbox.link(string, value='object') panel = Row(checkbox, string) stringio = StringIO() panel.save(stringio, embed=True) stringio.seek(0) utf = stringio.read() assert "<pre>False<" in utf assert "<pre>True<" in utf
def test_embed_float_slider_explicit_values_out_of_bounds(document, comm): select = FloatSlider() string = Str() def link(target, event): target.object = event.new select.link(string, callbacks={'value': link}) panel = Row(select, string) with config.set(embed=True): model = panel.get_root(document, comm) with pytest.raises(ValueError): embed_state(panel, model, document, states={select: [0.1, 0.7, 2]})
def test_embed_select_str_explicit_values_not_found(document, comm): select = Select(options=['A', 'B', 'C']) string = Str() def link(target, event): target.object = event.new select.link(string, callbacks={'value': link}) panel = Row(select, string) with config.set(embed=True): model = panel.get_root(document, comm) with pytest.raises(ValueError): embed_state(panel, model, document, states={select: ['A', 'D']})
def test_save_embed_bytesio(): checkbox = Checkbox() string = Str() def link(target, event): target.object = event.new checkbox.link(string, callbacks={'value': link}) panel = Row(checkbox, string) stringio = StringIO() panel.save(stringio, embed=True) stringio.seek(0) utf = stringio.read() assert "&lt;pre&gt;False&lt;/pre&gt;" in utf assert "&lt;pre&gt;True&lt;/pre&gt;" in utf
def test_app(): data = {"x": [1, 2, 3, 4, 5], "y": [3800, 3700, 3800, 3900, 4000]} trend = Trend( title="Panel Users", value=4000, value_change=0.51, data=data, height=200, width=200, ) def update_datasource(): new_x = max(data["x"]) + 1 old_y = data["y"][-1] new_y = random.uniform(-old_y * 0.05, old_y * 0.05) + old_y * 1.01 trend.stream({"x": [new_x], "y": [new_y]}, rollover=50) y_series = data["y"] trend.value = y_series[-1] change = y_series[-1] / y_series[-2] - 1 trend.value_change = change settings_panel = Param( trend, parameters=[ "height", "width", "sizing_mode", "layout", "title", "plot_color", "plot_type", "value_change_pos_color", "value_change_neg_color", ], widgets={ "height": {"widget_type": IntSlider, "start": 0, "end": 800, "step": 1}, "width": {"widget_type": IntSlider, "start": 0, "end": 800, "step": 1}, }, sizing_mode="fixed", width=400, ) app = Column( HTML( "<h1>Panel - Streaming to TrendIndicator<h1>", sizing_mode="stretch_width", background="black", style={"color": "white", "padding": "15px"}, ), Row(WidgetBox(settings_panel), trend, sizing_mode="stretch_both"), sizing_mode="stretch_both", ) state.add_periodic_callback(update_datasource, period=50) return app
def test_embed_discrete(document, comm): select = Select(options=['A', 'B', 'C']) string = Str() select.link(string, value='object') panel = Row(select, string) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document) _, state = document.roots assert set(state.state) == {'A', 'B', 'C'} for k, v in state.state.items(): content = json.loads(v['content']) assert 'events' in content events = content['events'] assert len(events) == 1 event = events[0] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['model'] == model.children[1].ref assert event['new'] == '<pre>%s</pre>' % k
def test_embed_checkbox(document, comm): checkbox = Checkbox() string = Str() checkbox.link(string, value='object') panel = Row(checkbox, string) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document) _, state = document.roots assert set(state.state) == {'false', 'true'} for k, v in state.state.items(): content = json.loads(v['content']) assert 'events' in content events = content['events'] assert len(events) == 1 event = events[0] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['model'] == model.children[1].ref assert event['new'] == '<pre>%s</pre>' % k.title()
def test_embed_continuous(document, comm): select = FloatSlider(start=0, end=10) string = Str() select.link(string, value='object') panel = Row(select, string) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document) _, state = document.roots assert set(state.state) == {0, 1, 2} values = [0, 5, 10] for k, v in state.state.items(): content = json.loads(v['content']) assert 'events' in content events = content['events'] assert len(events) == 1 event = events[0] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['model'] == model.children[1].ref assert event['new'] == '<pre>%.1f</pre>' % values[k]
def test_embed_slider_str_jslink(document, comm): slider = FloatSlider(start=0, end=10) string = Str() slider.link(string, value='object') panel = Row(slider, string) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document) assert len(document.roots) == 1 assert model is document.roots[0] ref = model.ref['id'] cbs = list(model.select({'type': CustomJS})) assert len(cbs) == 2 cb1, cb2 = cbs cb1, cb2 = (cb1, cb2) if slider._models[ref][0] is cb1.args['source'] else (cb2, cb1) assert cb1.code == """ var value = source['value']; value = value; value = JSON.stringify(value).replace(/,/g, ", ").replace(/:/g, ": "); try { var property = target.properties['text']; if (property !== undefined) { property.validate(value); } } catch(err) { console.log('WARNING: Could not set text on target, raised error: ' + err); return; } try { target['text'] = value; } catch(err) { console.log(err) } """ assert cb2.code == """
def test_embed_select_str_link(document, comm): select = Select(options=['A', 'B', 'C']) string = Str() def link(target, event): target.object = event.new select.link(string, callbacks={'value': link}) panel = Row(select, string) with config.set(embed=True): model = panel.get_root(document, comm) embed_state(panel, model, document) _, state = document.roots assert set(state.state) == {'A', 'B', 'C'} for k, v in state.state.items(): content = json.loads(v['content']) assert 'events' in content events = content['events'] assert len(events) == 1 event = events[0] assert event['kind'] == 'ModelChanged' assert event['attr'] == 'text' assert event['model'] == model.children[1].ref assert event['new'] == '<pre>%s</pre>' % k
def test_embed_merged_sliders(document, comm): s1 = IntSlider(name='A', start=1, end=10, value=1) t1 = StaticText() s1.param.watch(lambda event: setattr(t1, 'value', event.new), 'value') s2 = IntSlider(name='A', start=1, end=10, value=1) t2 = StaticText() s2.param.watch(lambda event: setattr(t2, 'value', event.new), 'value') panel = Row(s1, s2, t1, t2) with config.set(embed=True): model = panel.get_root(document, comm) state_model = embed_state(panel, model, document) assert len(document.roots) == 2 assert model is document.roots[0] cbs = list(model.select({'type': CustomJS})) assert len(cbs) == 5 ref1, ref2 = model.children[2].ref['id'], model.children[3].ref['id'] state0 = json.loads(state_model.state[0]['content'])['events'] assert state0 == [{ "attr": "text", "kind": "ModelChanged", "model": { "id": ref1 }, "new": "1" }, { "attr": "text", "kind": "ModelChanged", "model": { "id": ref2 }, "new": "1" }] state1 = json.loads(state_model.state[1]['content'])['events'] assert state1 == [{ "attr": "text", "kind": "ModelChanged", "model": { "id": ref1 }, "new": "5" }, { "attr": "text", "kind": "ModelChanged", "model": { "id": ref2 }, "new": "5" }] state2 = json.loads(state_model.state[2]['content'])['events'] assert state2 == [{ "attr": "text", "kind": "ModelChanged", "model": { "id": ref1 }, "new": "9" }, { "attr": "text", "kind": "ModelChanged", "model": { "id": ref2 }, "new": "9" }]
def view(self): if self.Screen1 == "radial": set_screen1("radial", sid) populate_radial_diagram(df, sid, datashaded=self.datashaded) if self.Screen1 == "force": set_screen1("force", sid) populate_force_diagram(df, sid, datashaded=self.datashaded) if self.Screen1 == "hierarchical": set_screen1("hierarchical", sid) populate_hierarchical_diagram(df, sid, datashaded=self.datashaded) if self.Screen1 == "3d": set_screen1("3d", sid) populate_3d_diagram(df, sid) # print(s1[1]) screen1 = get_custom_key(get_screen1(sid), sid) if self.Screen1 == "3d": zero_zero = Column(get_custom_key(get_screen1(sid), sid), css_classes=['screen-1', 'col-s-6']) else: screen1.color_palette = self.Color_palette screen1.node_size = self.Node_size screen1.node_color = self.Node_color set_custom_key(get_screen1(sid), screen1, sid) if self.Screen2 == "matrix": set_screen2("matrix", sid) populate_matrix(get_matrix_df(sid), sid) screen2 = get_custom_key(get_screen2(sid), sid) screen2.reordering = self.Ordering screen2.metric = self.Metric screen2.color_palette = self.Color_palette set_custom_key(get_screen2(sid), screen2, sid) matrix = screen2.view() edge_table = Table(matrix.data).opts(height=int(get_window_height(sid)/3), width=290) SelectedDataLink(matrix, edge_table) zero_one = Column(matrix, css_classes=['screen-2', 'col-s-6']) if self.Screen1 != "3d": # Setting up the linking, generateDiagram functions return two-tuple (graph, points). Points is the selection layer # makeMatrix returns matrix_dropdown object. matrix.view returns the heatmap object SelectMatrixToNodeLink.register_callback('bokeh', SelectMatrixToNodeCallback) SelectNodeToMatrixLink.register_callback('bokeh', SelectNodeToMatrixCallback) SelectNodeToTableLink.register_callback('bokeh', SelectNodeToTableCallback) graph, points = screen1.view() node_table = Table(points.data[['index', 'indegree', 'outdegree']]).opts(height=int(get_window_height(sid)/3), width=290) # Link matrix to the nodelink (both graph and points) SelectMatrixToNodeLink(matrix, points) SelectNodeToTableLink(points, node_table) # Link nodelink to matrix (points only) SelectNodeToMatrixLink(points, matrix) # For highlighting edges when tapping on node, but doesn't work # if not self.datashaded: # PointToGraphLink.register_callback('bokeh', PointToGraphCallback) # PointToGraphLink(points, graph) zero_zero = Column(graph * points, css_classes=['screen-1', 'col-s-6']) if not self.Ran: zero_two = Row(Column(node_table, css_classes=['node-table', 'invisible']), Column(edge_table, css_classes=['edge-table', 'invisible']), css_classes=['trash']) else: if not self.Ran: zero_two = Row(Column(edge_table, css_classes=['edge-table', 'invisible']), css_classes=['trash']) # SelectedDataLink(matrix, points) # renderer = renderer('bokeh') # print(renderer.get_plot(points).handles) if not self.Ran: self.Ran = True return Row(zero_zero, zero_one, zero_two, css_classes=['good-width']) return Row(zero_zero, zero_one, css_classes=['good-width'])