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 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_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_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_checkbox_str_jslink(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) 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 checkbox._models[ref][0] is cb1.args['source'] else (cb2, cb1) assert cb1.code == """ var value = source['active']; value = value.indexOf(0) >= 0; 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_checkbox(document, comm): checkbox = Checkbox(value=True, name='Checkbox') widget = checkbox._get_root(document, comm=comm) assert isinstance(widget, checkbox._widget_type) assert widget.labels == ['Checkbox'] assert widget.active == [0] widget.active = [] checkbox._comm_change({'active': []}) assert checkbox.value == False checkbox.value = True assert widget.active == [0]
def test_checkbox(document, comm): checkbox = Checkbox(value=True, name='Checkbox') box = checkbox._get_model(document, comm=comm) assert isinstance(box, WidgetBox) widget = box.children[0] assert isinstance(widget, checkbox._widget_type) assert widget.labels == ['Checkbox'] assert widget.active == [0] widget.active = [] checkbox._comm_change({'active': []}) assert checkbox.value == False checkbox.value = True assert widget.active == [0]