Joe Doliner Joe Doliner - 1 month ago 6
Python Question

Why can't I call Bokeh file_html on Bar chart?

So I have the following code which works great:

from bokeh.plotting import circle
from bokeh.resources import CDN
from bokeh.embed import file_html

plot = circle([1,2], [3,4])

html = file_html(plot, CDN, "my plot")


html
winds up containing an html string which draws a circle. However I can't do the same thing with a
Bar
object.

from bokeh.plotting import circle
from bokeh.resources import CDN
from bokeh.embed import file_html

plot = Bar(OrderedDict(tuples))

html = file_html(plot, CDN, "my plot")


This fails with the error:

Traceback (most recent call last):
File "./viz.py", line 66, in <module>
html = file_html(b, CDN, "my plot")
File "/usr/local/lib/python2.7/dist-packages/bokeh/embed.py", line 120, in file_html
script, div = components(plot_object, resources)
File "/usr/local/lib/python2.7/dist-packages/bokeh/embed.py", line 41, in components
ref = plot_object.ref
AttributeError: 'Bar' object has no attribute 'ref'


I'm assuming that
Bar
object is not an plot object. How can I turn it in to one?

Answer

This question (with the current Bokeh version 0.7.1) has been answered both here and here.

Considering your code, the following should work:

from bokeh.charts import Bar
from bokeh.resources import CDN 
from bokeh.embed import file_html

plot = Bar(OrderedDict(tuples))
plot.show()
html = file_html(plot.chart.plot, CDN, "my plot")
Comments