Embed Bokeh Plots in Jinja template by name


I am currently using Bokeh with Flask & Bootstrap to make metric dashboards. So far I have never seen a example or managed to implement a way of referring to a specific plot by name, every example I can find requires you to plot all the graphs or loop through plot_div.

Using the following method to embed plots into a flask app:


def dashboard():

js_resources = INLINE.render_js()

css_resources = INLINE.render_css()

p1, p2 = bplot.returnTest()

plot_script, plot_div = components(dict([(p1.name, p1), (p2.name, p2)]))

return encode_utf8(






Using the above, one would expect this to work without issue in the html file:

{{ plot_div[‘plot1’]|safe }}

This throws an error: (index):68 Uncaught Error: Error rendering Bokeh model: could not find #215691fc-0cbd-4485-a92b-b8f8ec064b30 HTML tag

But if we try the following:

{% for key in plot_div.keys() %}

{% if key == “plot1” %}

{{ plot_div[key]|safe }}

{% endif %}

{% endfor %}

This works without issue, even though logically they should have the same behaviour. So there is a hacky workaround for the issue. Has anyone had experience of this or seen an example of someone doing this without the hack? I can provide more code if something is not clear please ask.



Update: works but you have to have every plot on the page, it will error if the script cannot find a plot which it contains information for. Not sure if this is intended behaviour, seems like it would be simple to change this behaviour.