Suppose I want to save a Bokeh model as json for later use. Following the fact that “the smallest unit of serialization is the document” I do the following:
s = curdoc().to_json_string() with open("file.json","w") as f: f.write(s)
To recover the model, I can do:
with open('document.json') as f: d = f.read() D = Document().from_json_string(d) f = D.roots D.clear() curdoc().add_root(f)
D.clear() is needed because otherwise you get the error that a model is associated to more than one document.
Is there a more elegant way to do this? The question has been raised before, for example in this stackoverflow post where the answer proposes iterating over all submodels of
D.roots and changing the
_document property, but
clear() seems to work for me.