Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
fixing bokeh plots on reloading the notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
soumith committed Jan 8, 2015
1 parent d45d418 commit ab38f8c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
68 changes: 57 additions & 11 deletions Plot.lua
Expand Up @@ -404,18 +404,64 @@ local function encodeAllModels(m)
return s
end

local base_template =
[[
local base_template = [[
<script type="text/javascript">
$(function() {
var modelid = "${model_id}";
var modeltype = "Plot";
var all_models = ${all_models};
Bokeh.load_models(all_models);
var model = Bokeh.Collections(modeltype).get(modelid);
$("#${window_id}").html(''); // clear any previous plot in window_id
var view = new model.default_view({model: model, el: "#${window_id}"});
});
$(function() {
if (typeof (window._bokeh_onload_callbacks) === "undefined"){
window._bokeh_onload_callbacks = [];
}
function load_lib(url, callback){
window._bokeh_onload_callbacks.push(callback);
if (window._bokeh_is_loading){
console.log("Bokeh: BokehJS is being loaded, scheduling callback at", new Date());
return null;
}
console.log("Bokeh: BokehJS not loaded, scheduling load and callback at", new Date());
window._bokeh_is_loading = true;
var s = document.createElement('script');
s.src = url;
s.async = true;
s.onreadystatechange = s.onload = function(){
Bokeh.embed.inject_css("http://cdn.pydata.org/bokeh-0.7.0.min.css");
window._bokeh_onload_callbacks.forEach(function(callback){callback()});
};
s.onerror = function(){
console.warn("failed to load library " + url);
};
document.getElementsByTagName("head")[0].appendChild(s);
}
bokehjs_url = "http://cdn.pydata.org/bokeh-0.7.0.min.js"
var elt = document.getElementById("${window_id}");
if(elt==null) {
console.log("Bokeh: ERROR: autoload.js configured with elementid '${window_id}'"
+ "but no matching script tag was found. ")
return false;
}
if(typeof(Bokeh) !== "undefined") {
console.log("Bokeh: BokehJS loaded, going straight to plotting");
var modelid = "${model_id}";
var modeltype = "Plot";
var all_models = ${all_models};
Bokeh.load_models(all_models);
var model = Bokeh.Collections(modeltype).get(modelid);
$("#${window_id}").html(''); // clear any previous plot in window_id
var view = new model.default_view({model: model, el: "#${window_id}"});
} else {
load_lib(bokehjs_url, function() {
console.log("Bokeh: BokehJS plotting callback run at", new Date())
var modelid = "${model_id}";
var modeltype = "Plot";
var all_models = ${all_models};
Bokeh.load_models(all_models);
var model = Bokeh.Collections(modeltype).get(modelid);
$("#${window_id}").html(''); // clear any previous plot in window_id
var view = new model.default_view({model: model, el: "#${window_id}"});
});
}
});
</script>
]]

Expand Down
10 changes: 0 additions & 10 deletions custom.js
Expand Up @@ -12,16 +12,6 @@ $([IPython.events]).on('notebook_loaded.Notebook', function(){

// logic per page-refresh
$([IPython.events]).on("app_initialized.NotebookApp", function () {
$.ajax({
url: "http://cdn.pydata.org/bokeh-0.7.0.min.js",
dataType: "script",
async: false,
success: function () {},
error: function () {
throw new Error("Could not load bokeh.js");
}
});
$('head').append('<link rel="stylesheet" type="text/css" href="http://cdn.pydata.org/bokeh-0.7.0.min.css">');
$('head').append('<link rel="stylesheet" type="text/css" href="custom.css">');


Expand Down
Binary file added small.mp4
Binary file not shown.
5 changes: 1 addition & 4 deletions test.lua
Expand Up @@ -9,7 +9,7 @@ itorch.image(m.weight)
itorch.audio('volkswagen.mp3')

-- video
itorch.video('chase.ogv')
itorch.video('small.mp4')

-- html
itorch.html('<p><b>Hi there!</b> how are you</p>')
Expand All @@ -34,6 +34,3 @@ plot:legend(true)
plot:redraw()
-- print(plot:toHTML())
plot:save('out.html')



0 comments on commit ab38f8c

Please sign in to comment.