def render_model(model, comm=None): if not isinstance(model, Model): raise ValueError("notebook_content expects a single Model instance") target = model.ref['id'] (docs_json, [render_item]) = standalone_docs_json_and_render_items([model]) div = div_for_render_item(render_item) render_item = render_item.to_json() script = DOC_NB_JS.render( docs_json=serialize_json(docs_json), render_items=serialize_json([render_item]), ) bokeh_script, bokeh_div = encode_utf8(script), encode_utf8(div) html = "<div id='{id}'>{html}</div>".format(id=target, html=bokeh_div) # Publish bokeh plot JS msg_handler = bokeh_msg_handler.format(plot_id=target) if comm: comm_js = comm.js_template.format(plot_id=target, comm_id=comm.id, msg_handler=msg_handler) bokeh_js = '\n'.join([comm_js, bokeh_script]) else: bokeh_js = bokeh_script data = {'text/html': html, 'application/javascript': bokeh_js} return ({'text/html': mimebundle_to_html(data), EXEC_MIME: ''}, {EXEC_MIME: {'id': target}})
def test_notebook_content(self, mock_sdjari, test_plot): (docs_json, render_items) = ("DOC_JSON", [dict(docid="foo", elementid="bar", modelid="bat")]) mock_sdjari.return_value = (docs_json, render_items) expected_script = DOC_NB_JS.render(docs_json=serialize_json(docs_json), render_items=serialize_json(render_items)) expected_div = PLOT_DIV.render(elementid=render_items[0]['elementid']) (script, div, _) = ben.notebook_content(test_plot) assert script == expected_script assert div == expected_div
def test_notebook_content_with_notebook_comms_target(self, mock_sdjari, test_plot): (docs_json, render_items) = ("DOC_JSON", [dict(docid="foo", elementid="bar", modelid="bat")]) mock_sdjari.return_value = (docs_json, render_items) comms_target = "NOTEBOOK_COMMS_TARGET" ## assert that NOTEBOOK_COMMS_TARGET is added to render_items bundle assert 'notebook_comms_target' not in render_items[0] (script, _, _) = ben.notebook_content(test_plot, notebook_comms_target=comms_target) assert 'notebook_comms_target' in render_items[0] ## assert that NOTEBOOK_COMMS_TARGET ends up in generated script expected_script = DOC_NB_JS.render(docs_json=serialize_json(docs_json), render_items=serialize_json(render_items)) assert script == expected_script
def test_notebook_content(self, mock_sdjari, test_plot): (docs_json, render_items) = ("DOC_JSON", [dict(docid="foo", elementid="bar", modelid="bat")]) mock_sdjari.return_value = (docs_json, render_items) expected_script = DOC_NB_JS.render( docs_json=serialize_json(docs_json), render_items=serialize_json(render_items)) expected_div = PLOT_DIV.render(elementid=render_items[0]['elementid']) (script, div, _) = ben.notebook_content(test_plot) assert script == expected_script assert div == expected_div
def test_notebook_content_with_notebook_comms_target( self, mock_sdjari, test_plot): (docs_json, render_items) = ("DOC_JSON", [dict(docid="foo", elementid="bar", modelid="bat")]) mock_sdjari.return_value = (docs_json, render_items) comms_target = "NOTEBOOK_COMMS_TARGET" ## assert that NOTEBOOK_COMMS_TARGET is added to render_items bundle assert 'notebook_comms_target' not in render_items[0] (script, _, _) = ben.notebook_content(test_plot, notebook_comms_target=comms_target) assert 'notebook_comms_target' in render_items[0] ## assert that NOTEBOOK_COMMS_TARGET ends up in generated script expected_script = DOC_NB_JS.render( docs_json=serialize_json(docs_json), render_items=serialize_json(render_items)) assert script == expected_script