Skip to content

Commit

Permalink
Register MIME handler for classic notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 30, 2018
1 parent b557b9e commit b657547
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
3 changes: 2 additions & 1 deletion holoviews/plotting/bokeh/widgets.py
Expand Up @@ -12,6 +12,7 @@
from ...core import Store, NdMapping, OrderedDict
from ...core.util import (drop_streams, unique_array, isnumeric,
wrap_tuple_streams, unicode)
from ..renderer import MIME_TYPES
from ..widgets import NdWidget, SelectionWidget, ScrubberWidget
from .util import serialize_json

Expand Down Expand Up @@ -274,7 +275,7 @@ def _get_data(self):
msg, metadata = self.renderer.components(self.plot, comm=False)
data = super(BokehWidget, self)._get_data()
return dict(data, init_html=msg['text/html'],
init_js=msg['application/javascript'],
init_js=msg[MIME_TYPES['js']],
plot_id=self.plot.state._id)

def encode_frames(self, frames):
Expand Down
1 change: 0 additions & 1 deletion holoviews/plotting/renderer.py
Expand Up @@ -324,7 +324,6 @@ def components(self, obj, fmt=None, comm=True, **kwargs):
data['text/html'] = html
if js:
data[MIME_TYPES['js']] = js
data[MIME_TYPES['jlab-hv-exec']] = js
metadata['id'] = plot_id
return (data, {MIME_TYPES['jlab-hv-exec']: metadata})

Expand Down
61 changes: 61 additions & 0 deletions holoviews/plotting/widgets/widgets.js
Expand Up @@ -539,3 +539,64 @@ for (var k in _namespace) {
window.HoloViews[k] = _namespace[k];
}
}

var JS_MIME_TYPE = 'application/javascript';
var HTML_MIME_TYPE = 'text/html';
var EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';
var CLASS_NAME = 'output';

/**
* Render data to the DOM node
*/
function render(props, node) {
var script = document.createElement("script");
node.appendChild(script);
}

/**
* Handle when a new output is added
*/
function handleAddOutput(event, handle) {
var output_area = handle.output_area;
var output = handle.output;
// limit handleAddOutput to display_data with EXEC_MIME_TYPE content only
if ((output.output_type != "display_data") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {
return
}
var toinsert = output_area.element.find("." + CLASS_NAME.split(' ')[0]);
if (output.metadata[EXEC_MIME_TYPE]["id"] !== undefined) {
toinsert[0].firstChild.textContent = output.data[JS_MIME_TYPE];
output_area._hv_plot_id = output.metadata[EXEC_MIME_TYPE]["id"];
}
}

function register_renderer(events, OutputArea) {
function append_mime(data, metadata, element) {
// create a DOM node to render to
var toinsert = this.create_output_subarea(
metadata,
CLASS_NAME,
EXEC_MIME_TYPE
);
this.keyboard_manager.register_events(toinsert);
// Render to node
var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};
render(props, toinsert[0]);
element.append(toinsert);
return toinsert
}
events.on('output_added.OutputArea', handleAddOutput);

OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {
safe: true,
index: 0
});
}

if (window.Jupyter !== undefined) {
var events = require('base/js/events');
var OutputArea = require('notebook/js/outputarea').OutputArea;
if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {
register_renderer(events, OutputArea);
}
}

0 comments on commit b657547

Please sign in to comment.