How to replace curdoc

筅森魡賤 提交于 2019-12-11 15:18:25

问题


How can I replace the current document in a bokeh server app?

I have a previous document saved as a json_string. If I do

set_curdoc(Document.from_json_string(json_string))

this seems to properly change curdoc(), however the new document is not displayed in the browser.


回答1:


I found a workaround, other places suggest to update the children of an existing layout instead of updating the whole curdoc().

I did that but I had to expand a bit to do that from a document saved in a json string.

I had to switch the document attribute of all the models from the imported document to curdoc() instead (otherwise it complains that the models belong to another document)

assuming that the document I import and the current document both have only one root:

new_doc = Document.from_json_string(json_string)

new_grid_models = collect_models(new_doc.roots[0])

for elem in new_grid_models:
    try:
        elem.document = curdoc()
    except AttributeError:
        elem._document = curdoc()

new_children = new_doc.roots[0].children
del new_doc

grid.children = new_children

After that the python callbacks need to be re-affected to the appropriate imported models.

I put up an example app here: save_and_load app on Bitbucket



来源:https://stackoverflow.com/questions/46494811/how-to-replace-curdoc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!