The following script (from this SO answer) generates a simpleapp Bokeh plot.
How can I save the output to a standalone html file?
import bokeh.plotting as bk
from bokeh.models import ColumnDataSource, Plot
from bokeh.models.widgets import Select, AppVBox
from bokeh.simpleapp import simpleapp
data = {"a": {"x": [1,2,3], "y": [1,2,3]},
"b": {"x": [3,2,1], "y": [1,2,3]},
"c": {"x": [2,2,2], "y": [1,2,3]},}
options = ["a", "b", "c"]
select1 = Select(name = 'ticker1', value = options[0], options = options)
@simpleapp(select1)
def test_layout(ticker1):
p = bk.figure(title = "layout test")
chart_data = data[ticker1]
df = ColumnDataSource(data = chart_data)
p.circle(x = chart_data["x"], y = chart_data["y"])
return {'plot': p}
@test_layout.layout
def layout(app):
return AppVBox(app=app, children=['ticker1', 'plot'])
test_layout.route("/bokeh/layout/")
I tried changing the layout(app) to make use of file_html. This generates the initial plot, but the combobox does not work. I think that file_html is only for single plot objects, not for something like the simpleapp that I'm using.
from bokeh.embed import file_html
from bokeh.resources import CDN
def layout(app):
# save standalone html
html = file_html(AppVBox(app=app, children=['ticker1', 'plot']), CDN, "title")
f = open("bokeh_standalone.html", "w")
f.write(html)
f.close
1 Answer 1
There isn't too much documentation surrounding Bokeh at the moment so some simple tasks may seem a little under-supported. TL;DR: Use the example provided here by extending it.
However, what you need to do in order to enable the Select box is to call on_change('value', func) on it, where func is a callable that updates the datasource. Once that has been done, you can set datasource._dirty = True. Importantly, all of these things must happen inside another method belonging to the app class called setup_events.
The way the bokeh server handles interaction is to create new python instances of objects that are stored in it's chosen internal database (redis by default). This means it is necessary to "setup events" every time.
I strongly suggest reading through the Sliders App example provided by ContinuumIO on the Bokeh Github page: https://github.com/bokeh/bokeh/tree/master/examples/app/sliders_applet
I've been pulling hair out over this for the last few weeks but we're understanding more and more everyday. I hope this helps :)