how to embed standalone bokeh graphs into django templates

Using the Embedding Bokeh Plots documentation example as suggested by Fabio Pliger, one can do this in Django:

in the file, we put:

from django.shortcuts import render
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import components

def simple_chart(request):
    plot = figure()[1,2], [3,4])

    script, div = components(plot, CDN)

    return render(request, "simple_chart.html", {"the_script": script, "the_div": div})

in the file we can put :

from myapp.views import simple_chart 
url(r'^simple_chart/$', simple_chart, name="simple_chart"),

in the template file simple_chart.html we’ll have :

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Experiment with Bokeh</title>
    <script src=""></script>
    <link rel="stylesheet" href="">

    {{ the_div|safe }}

    {{ the_script|safe }}

And it works.

Leave a Comment