Skip to content

Instantly share code, notes, and snippets.

@ruoyu0088
Created December 10, 2017 22:24
Show Gist options
  • Save ruoyu0088/61318bcb86a93a98521d8e3c1c8d2b2c to your computer and use it in GitHub Desktop.
Save ruoyu0088/61318bcb86a93a98521d8e3c1c8d2b2c to your computer and use it in GitHub Desktop.
from bokeh.embed import components
from bokeh.embed.standalone import _ModelInDocument, standalone_docs_json_and_render_items
from bokeh.embed.util import check_models_or_docs
from bokeh.models import Model
from bokeh.document import Document
def bokeh_to_json(models):
was_single_object = isinstance(models, Model) or isinstance(models, Document)
models = check_models_or_docs(models, allow_dict=True)
model_keys = None
if isinstance(models, dict):
model_keys = models.keys()
values = []
for k in model_keys:
values.append(models[k])
models = values
with _ModelInDocument(models):
(docs_json, render_items) = standalone_docs_json_and_render_items(models)
return dict(doc=docs_json, items=render_items)
with open("/home/chronos/user/sd/test_fig.json", "w") as f:
data = bokeh_to_json(fig)
json.dump(data, f)
if(typeof document !== 'undefined'){
(function(){
var cssId = 'bokeh_css';
if (!document.getElementById(cssId))
{
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css';
link.media = 'all';
head.appendChild(link);
}
var scriptId = 'bokeh_js';
if(!document.getElementById(scriptId)){
var bokeh_script = document.createElement('script');
bokeh_script.setAttribute('src', 'https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.js');
bokeh_script.setAttribute('id', scriptId);
document.head.appendChild(bokeh_script);
}
console.log("load bokeh completed");
})();
}
(function(){
"use strict";
exports.name = "bokehplot";
exports.params = [
{name: "tiddler"}
];
exports.run = function(tiddler) {
var text = $tw.wiki.getTiddler(tiddler).fields.text;
var data = JSON.parse(text);
var doc = data["doc"];
var items = data["items"];
var plot_id = items[0]["elementid"];
console.log(doc);
console.log(items);
(function() {
var fn = function() {
Bokeh.safely(function() {
(function(root) {
function embed_document(root) {
root.Bokeh.embed.embed_items(doc, items);
}
if (root.Bokeh !== undefined) {
embed_document(root);
} else {
var attempts = 0;
var timer = setInterval(function(root) {
if (root.Bokeh !== undefined) {
embed_document(root);
clearInterval(timer);
}
attempts++;
if (attempts > 100) {
console.log("Bokeh: ERROR: Unable to embed document because BokehJS library is missing")
clearInterval(timer);
}
}, 10, root)
}
})(window);
});
};
setTimeout(fn, 500);
})();
return '<div class="bk-root"><div class="bk-plotdiv" id="' + plot_id + '"></div></div>';
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment