Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh Bokeh Document

Tags:

bokeh

I would like to refresh a bokeh document so I can replace old plots with new ones. However, right now I just get the new plots appended to the document so the old ones don't go away.

#myfile.py
from bokeh.plotting import curdoc, figure
doc = curdoc()
p1 = figure(width=1500, height=230, active_scroll="wheel_zoom")
doc.add_root(p1)
doc.clear()
p2 = figure(width=1500, height=500, active_scroll="wheel_zoom")
doc.add_root(p2)

This results in the second plot being displayed after the first plot, though the expected behavior I am looking for is the second plot replacing the first plot. How can I resolve this? I am running this on a bokeh server via bokeh serve --show myfile.py

like image 602
Caleb Klaus Avatar asked Aug 16 '16 18:08

Caleb Klaus


People also ask

How do you start a bokeh server?

To run a Bokeh server instance, use commands similar to the following: serve myapp.py --port 5100 serve myapp.py --port 5101 ... Next, in the location stanza for the Bokeh server, change the proxy_pass value to refer to the upstream stanza above. The code below uses proxy_pass http://myapp; .

How do I save HTML in bokeh?

Bokeh creates the HTML file when you call the show() function. This function also automatically opens a web browser to display the HTML file. If you want Bokeh to only generate the file but not open it in a web browser, use the save() function instead.

What is bokeh io?

Bokeh is an interactive visualization library that targets modern web browsers for presentation. It is good for: Interactive visualization in modern browsers. Standalone HTML documents, or server-backed apps. Large, dynamic or streaming data.


2 Answers

The best way to accomplish something like this is to have a top level layout of some kind (e.g. row or column) that has the content you want to replace inside it. Then when you want to replace things, keep the layout container, but change the value of its children property:

from bokeh.plotting import curdoc, figure
from bokeh.layouts import row

doc = curdoc()

p1 = figure(width=1500, height=230, active_scroll="wheel_zoom")

layout = row(p1)
doc.add_root(layout)

p2 = figure(width=1500, height=500, active_scroll="wheel_zoom")

layout.children[0] = p2

You can see a similar technique in the Crossfilter example.

like image 122
bigreddot Avatar answered Oct 19 '22 20:10

bigreddot


Just in case anyone is struggling on how to set the children for layouts when there are multiple elements (say, widgets, more figures, rows etc), you can do so by wrapping the elements in a layout and assigning the children property directly:

p2 = figure(width=1500, height=500, active_scroll="wheel_zoom")
p3 = figure(width=1500, height=500, active_scroll="wheel_zoom")
new_layout = row(p2, p3)
layout.children = new_layout.children
like image 24
Gabriel Avatar answered Oct 19 '22 21:10

Gabriel