0

I want to serialize a bokeh document to JSON, load it again using from_json and from_json_string [3] and serve it.

However, most examples out there either create their server by modifying curdoc [1] or by creating an application that receives and modifies a document [2].

The fact that I am using holoviews to generate the plot should not change the question, so this is not in anyway a holoviews question ;).

import pandas as pd
import holoviews as hv
from bokeh.document import Document
from bokeh.io.doc import curdoc, set_curdoc
import json
hv.extension('matplotlib')
hv.extension('bokeh')

print("- create dummy data")
df = pd.DataFrame(dict(x=[1, 2], y=[0, 1]))
print(df)

print("- plot as holoviews curve")
curve = hv.Curve(df, 'x', 'y', label='curve1').opts(ylabel='y')

print("- render in bokeh")
plot = hv.render(curve, backend='bokeh')

print("- serialize as bokeh document to JSON")
doc = Document()
doc.add_root(plot)
doc_json = doc.to_json()
json_string = json.dumps(doc_json)

print("- deserialize as bokeh document")
doc2 = Document.from_json_string(json_string)
print(doc2.roots[0])

Now I do want to create something like the following:

print("- embed document in bokeh application")
from bokeh.application.application import Application
app = Application()
app.initialize_document(doc)

print("- start bokeh server")
from bokeh.server.server import Server
server = Server(
    {'/fubar':app},  # list of Bokeh applications
    port=5010,
    num_procs=1
)

print("- starting server on 'http://localhost:5010/fubar'")
server.start()
server.io_loop.start()

This does actually run, but does not show anything in my browser. The reason is, that the Application actually requires a Handler and not a Document. But I found no documentation on how I can translate the one to the other.

Tl;Dr How can I configure the bokeh server to serve a deserialized document?

[1] https://docs.bokeh.org/en/latest/docs/user_guide/server.html
[2] https://docs.bokeh.org/en/latest/docs/reference/application/handlers/function.html
[3] https://docs.bokeh.org/en/latest/docs/reference/document.html

3
  • 1
    Could you have a look at this post if it can help you?
    – Tony
    Apr 18, 2019 at 19:19
  • Hi Tony, sorry I missed your comment before. Your method has the disadvantage, that it only supports the static content of the plot. I also need the dynamic content, which in turn requires a Bokeh server.
    – magum
    Apr 23, 2019 at 8:39
  • After not getting any answers here, I also asked the question in bokeh's google group: groups.google.com/a/continuum.io/forum/#!msg/bokeh/U-nt8nUqlvY/…
    – magum
    Apr 26, 2019 at 11:56

1 Answer 1

1

Thanks to Bryan Van de Ven, I finally got it. The trick is to use replace_with_json instead!

import pandas as pd
import holoviews as hv
from bokeh.document import Document
from bokeh.io.doc import curdoc, set_curdoc
import json
hv.extension('matplotlib')
hv.extension('bokeh')

print("- create dummy data")
df = pd.DataFrame(dict(x=[1, 2], y=[0, 1]))
print(df)

print("- plot as holoviews curve")
curve = hv.Curve(df, 'x', 'y', label='curve1').opts(ylabel='y')

print("- render in bokeh")
plot = hv.render(curve, backend='bokeh')

print("- serialize as bokeh document to JSON")
doc = Document()
doc.add_root(plot)
doc_json = doc.to_json()
json_string = json.dumps(doc_json)

print("- deserialize as bokeh document")
curdoc().replace_with_json(json.loads(json_string))

Unfortunately this does not work nicely with dynamic content created with holoviews. In a more complicated example, only the static content was preserved. It seems that for these cases, there is currently really no other method than really executing the analysis on the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.