pythonbokehholoviewsdatashader

How to connect holoviews in backend with bokehjs frontend


I'm developing a webpage that needs to plot millions of points, so I'm using HoloViews in my backend to generate a plot and send it as a Bokeh model to my frontend which uses Bokehjs.

So in the API I call a function that does this

hv.extension("bokeh")
points = hv.Points(df)
datashaded = hd.datashade(points, aggregator=ds.count_cat('cat')).redim.range(x=(-5,5),y=(-5,5))
plot = hv.render(datashaded)
return json.dumps(json_item(plot))

and returns a Bokeh model that is sent in JSON format to the frontend.

The function hd.datashade renders a Bokeh plot and internally calls datashader to create the images as you control zoom. But the problem is that as I call this function just once through the API, zoom controls don't create a new image, instead it just make pixels bigger.

I need a way to have a "live python process running" as documentation states so I can have zoom controls and tooltips. But I have no idea how to achieve that.


Solution

  • Once you dump things into JSON, there's no longer any connection to the Python code. Instead, you can do something like in http://pyviz.org/tutorial/13_Deploying_Bokeh_Apps.html:

    hv.extension("bokeh")
    points = hv.Points(df)
    datashaded = hd.datashade(points, aggregator=ds.count_cat('cat')).redim.range(x=(-5,5),y=(-5,5))
    doc = hv.renderer('bokeh').server_doc(datashaded)
    doc.title = 'HoloViews Bokeh App'
    

    And then run bokeh serve --show file.py to launch Bokeh Server on your file. Bokeh server will ensure that a Python process is running, provide a webserver for displaying your HTML/JS, and set up connections between them.