使用内部制作的所有数据保存Bokeh dashdoard(独立) [英] Saving Bokeh dashdoard (standalone) with all the data made inside

查看:126
本文介绍了使用内部制作的所有数据保存Bokeh dashdoard(独立)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑后是否有任何方法可以保存Bokeh仪表板?

Is there any method to save Bokeh dashboard after editing it?

例如,我已经加载了仪表板,创建了一些图表并保存了它们(最后一个选项卡).然后,我想将进度"保存到.html文件中,这样就不必在初始化脚本后每次都重新执行所有这些操作.

For example, I've loaded my dashboard, created some plots and saved them (last tab). And then I want to save my "progress" to .html-file so that I wouldn't have to do all of this again every time after initializing my script.

这是我的仪表板的屏幕截图:

This is the screenshot of my dashboard:

谢谢!

推荐答案

所以解决方案是:
1
将您的bokeh文档(整个仪表板)转换为.json并下载为文件.

So the solution is:
1
Convert your bokeh-document (your whole dashboard) to .json and download it as a file.

例如,您可以通过单击按钮来完成.

For example, you do it by clicking a button.

#  creating button
download_json = Button(label="Download json", width=70)
# callback
download_json_func = CustomJS(args=dict(source=source_fill_groupby),code="""
function saveText(text, filename){
var a = document.createElement('a');
a.setAttribute('href', 'data:text/plain;charset=utf-8,'+encodeURIComponent(text));
a.setAttribute('download', filename);
a.click()
}
var obj = Bokeh.documents[0].to_json_string();
saveText( JSON.stringify(obj), "filename.json" );
""")
# assigning callback to the button
download_json.js_on_event(ButtonClick, download_json_func)

2
下载文件后,需要在Jupyter笔记本的下一个单元格中将其还原.假设应该有FileInput-widget,以便我们可以上传文件并将其显示在div块中.

2
After you've downloaded your file, you need to restore it in the next cell of your Jupyter notebook. Let's say there should be FileInput-widget so that we could upload our file and it'd appear in the div-block.

from bokeh.models.widgets import FileInput
# creating div where our saved dashboard will be shown
div = Div(text='<div id="document-container"></div>', width=500, height=500)
# adding widget
l = FileInput(accept='.json')
# callback
l.js_on_change('value', CustomJS(code="""\
const {Document} = Bokeh.require('document/document');
// uploaded .json-file  
const data = JSON.parse(atob(cb_obj.value));            
const doc = Document.from_json_string(data);
// dashboard to show
Bokeh.embed.add_document_standalone(doc, document.getElementById('document-container'), [], true);                                     
cb_obj.disabled = true;
"""))

show(column(l, div))

它仍然存在一些问题-它不能正确显示圆弧,并且如果您进行新的查询并创建新的绘图,则从上一个文档开始保存之后,绘图将不会更新其范围.将图保存到最后一个选项卡上也存在一些问题-它无法像在Bokeh中那样工作.但是至少您可以保存研究的进展".

It still has some issues - it doesn't show ciryllic correctly and plot won't update their ranges from the last one after which doc was saved if you make new queries and creating new plots. Also there's some problem with saving plots to the last tab - it doesn't work like it works in Bokeh. But at least you can save your "progress" in your researches.

Upd1.为了正确显示西里尔字母,您应该对FileInput使用下一个回调:

Upd1. To show cyrillic correctly you should use next callback for FileInput:

    l.js_on_change('value', CustomJS(code="""
function b64DecodeUnicode(str) {
    // Going backwards: from bytestream, to percent-encoding, to original string.
    return decodeURIComponent(atob(str).split('').map(function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}

const {Document} = Bokeh.require('document/document');
const data = JSON.parse(b64DecodeUnicode(cb_obj.value));
const doc = Document.from_json_string(data);
Bokeh.embed.add_document_standalone(doc, document.getElementById('document-container1'), [], true);
cb_obj.disabled = true;
"""))

这篇关于使用内部制作的所有数据保存Bokeh dashdoard(独立)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆