Ejemplo n.º 1
0
def render_mimebundle(model, doc, comm):
    """
    Displays bokeh output inside a notebook using the PyViz display
    and comms machinery.
    """
    if not isinstance(model, LayoutDOM):
        raise ValueError('Can only render bokeh LayoutDOM models')

    add_to_doc(model, doc, True)

    target = model.ref['id']

    # Publish plot HTML
    bokeh_script, bokeh_div, _ = bokeh.embed.notebook.notebook_content(
        model, comm.id)
    html = encode_utf8(bokeh_div)

    # Publish bokeh plot JS
    msg_handler = bokeh_msg_handler.format(plot_id=target)
    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])
    bokeh_js = embed_js.format(widget_id=target, plot_id=target,
                               html=html) + bokeh_js

    data = {
        EXEC_MIME: '',
        'text/html': html,
        'application/javascript': bokeh_js
    }
    metadata = {EXEC_MIME: {'id': target}}
    return data, metadata
Ejemplo n.º 2
0
    def components(self, obj, fmt=None, comm=True, **kwargs):
        """
        Returns data and metadata dictionaries containing HTML and JS
        components to include render in app, notebook, or standalone
        document. Depending on the backend the fmt defines the format
        embedded in the HTML, e.g. png or svg. If comm is enabled the
        JS code will set up a Websocket comm channel using the
        currently defined CommManager.
        """
        if isinstance(obj, (Plot, NdWidget)):
            plot = obj
        else:
            plot, fmt = self._validate(obj, fmt)

        widget_id = None
        data, metadata = {}, {}
        if isinstance(plot, NdWidget):
            js, html = plot(as_script=True)
            plot_id = plot.plot_id
            widget_id = plot.id
        else:
            html, js = self._figure_data(plot, fmt, as_script=True, **kwargs)
            plot_id = plot.id
            if comm and plot.comm is not None and self.comm_msg_handler:
                msg_handler = self.comm_msg_handler.format(plot_id=plot_id)
                html = plot.comm.html_template.format(init_frame=html,
                                                      plot_id=plot_id)
                comm_js = plot.comm.js_template.format(msg_handler=msg_handler,
                                                       comm_id=plot.comm.id,
                                                       plot_id=plot_id)
                js = '\n'.join([js, comm_js])
            html = "<div id='%s' style='display: table; margin: 0 auto;'>%s</div>" % (
                plot_id, html)
        if not os.environ.get('HV_DOC_HTML', False) and js is not None:
            js = embed_js.format(
                widget_id=widget_id, plot_id=plot_id, html=html) + js

        data['text/html'] = html
        if js:
            data[MIME_TYPES['js']] = js
            data[MIME_TYPES['jlab-hv-exec']] = ''
            metadata['id'] = plot_id
            self._plots[plot_id] = plot
        return (data, {MIME_TYPES['jlab-hv-exec']: metadata})
Ejemplo n.º 3
0
    def components(self, obj, fmt=None, comm=True, **kwargs):
        """
        Returns data and metadata dictionaries containing HTML and JS
        components to include render in app, notebook, or standalone
        document. Depending on the backend the fmt defines the format
        embedded in the HTML, e.g. png or svg. If comm is enabled the
        JS code will set up a Websocket comm channel using the
        currently defined CommManager.
        """
        if isinstance(obj, (Plot, NdWidget)):
            plot = obj
        else:
            plot, fmt = self._validate(obj, fmt)

        widget_id = None
        data, metadata = {}, {}
        if isinstance(plot, NdWidget):
            js, html = plot(as_script=True)
            plot_id = plot.plot_id
            widget_id = plot.id
        else:
            html, js = self._figure_data(plot, fmt, as_script=True, **kwargs)
            plot_id = plot.id
            if comm and plot.comm is not None and self.comm_msg_handler:
                msg_handler = self.comm_msg_handler.format(plot_id=plot_id)
                html = plot.comm.html_template.format(init_frame=html,
                                                      plot_id=plot_id)
                comm_js = plot.comm.js_template.format(msg_handler=msg_handler,
                                                       comm_id=plot.comm.id,
                                                       plot_id=plot_id)
                js = '\n'.join([js, comm_js])
            html = "<div id='%s' style='display: table; margin: 0 auto;'>%s</div>" % (plot_id, html)
        if not os.environ.get('HV_DOC_HTML', False) and js is not None:
            js = embed_js.format(widget_id=widget_id, plot_id=plot_id, html=html) + js

        data['text/html'] = html
        if js:
            data[MIME_TYPES['js']] = js
            data[MIME_TYPES['jlab-hv-exec']] = ''
            metadata['id'] = plot_id
            self._plots[plot_id] = plot
        return (data, {MIME_TYPES['jlab-hv-exec']: metadata})
Ejemplo n.º 4
0
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
    bokeh_js = embed_js.format(
        widget_id=target, plot_id=target, html=bokeh_div) + bokeh_js

    data = {
        EXEC_MIME: '',
        'text/html': html,
        'application/javascript': bokeh_js
    }
    metadata = {EXEC_MIME: {'id': target}}
    return data, metadata