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_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_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_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 == """