问题
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