1

In views.py

from django.shortcuts import render
from django.template.loader import get_template
from django.http import HttpResponse
from bokeh.plotting import figure, output_file, show 
from bokeh.embed import components
# Create your views here.
def homepage(request):
    template = get_template('index.html')
    plot = figure()
    plot.line([1,2,3,4,5],[5,4,3,2,1])
    script, div = components(plot)
    html = template.render(locals())
    return HttpResponse(html)

In the templates/index.html

i use the bokeh to generate the following code:

<div class="bk-root"> 
    <div class="plotdiv" id="a3a4c265-dec8-4057-b9ed-d39afda33e2d"></div> 
</div> 

And when i use the {{div | safe}}

But the result show nothing

how should i do to make the graphic show?

update 'templates/index.html'

{{ div | safe }}
<script type="text/javascript" src="http://cdn.bokeh.org/bokeh/release/bokeh-0.9.0.min.js"></script>
{{ script | safe }}
bigreddot
  • 33,642
  • 5
  • 69
  • 122
Jonathan Cheng
  • 459
  • 2
  • 11
  • 26
  • Are you templating the script that is returned too? You have to include both the script, and the div, in your template. – bigreddot Dec 29 '16 at 19:52
  • this question might help: http://stackoverflow.com/questions/29508958/how-to-embed-bokeh-graphs-into-django-templates-without-using-the-bokeh-server – AMG Dec 30 '16 at 02:56
  • @bigreddot yes i returned too. i update above – Jonathan Cheng Dec 30 '16 at 07:10
  • Are you actually using Bokeh version `0.9.0`!? That's very old, if you are running a recent/current version of the python library then that mismatch is almost certainly the problem. The BokehJS version from CDN (i,e. the version in the script tag) and the installed python library version need to match. – bigreddot Dec 30 '16 at 16:59
  • thx!!it does work!!because i i copy the tutorial in the net so i ignored if the version be matchedXDD – Jonathan Cheng Dec 30 '16 at 17:29

1 Answers1

1

The BokehJS version from CDN (in the script tag) must be corresponding to the python library version

Jonathan Cheng
  • 459
  • 2
  • 11
  • 26