Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering Bokeh widgets in django Templates

I'm new to django and bokeh. I was trying to render a simple bokeh plot supported by a few select options that essentially allow me to tweak my plot's content in a django web application.

The plots are rendered when the script and div elements obtained from the bokeh.embed.components() are passed to the template as context variables. The same didn't work when i had a widget and a plot held in a bokeh.io.vform object.

I get the output right when i perform a bokeh.io.show(), by specifying the bokeh.plotting.output_file(), but I'm trying to get this running in my web application. Am I missing anything? Or is there any other approach that serves my intent?

my code to just render a bokeh widget is as follows:

views.py

#django imports 
from bokeh.embed import components
from bokeh.plotting import figure
from bokeh.io import vform
from bokeh.models.widgets import Select
def test(request):
    s = Select(title="test", value="a", options=['a','b','c'])
    script,div = components(s)
    return render(request,'test.html',RequestContext(request,{'script':script,'div':div}))

test.html

<html>
<head>
      <link href="http://cdn.bokeh.org/bokeh/release/bokeh-0.11.1.min.css" rel="stylesheet" type="text/css">
       <script src="http://cdn.bokeh.org/bokeh/release/bokeh-0.11.1.min.js"></script>
</head>
{% load staticfiles %}
<body>
      {{ div | safe }}
      {{ script | safe }}
</body>
</html>

I would expect a select form element to be rendered when test.html is launched from test() in django. but doesn't happen.

like image 850
Skanda Avadhani Avatar asked Apr 18 '16 08:04

Skanda Avadhani


1 Answers

BokehJS was recently split up into separate pieces to provide more flexible options for usage depending on what parts of the library are actually being used. Because loading resources is normally automatically handled in many cases, it was unintentioinally neglected to mention this split prominently in the docs. However, it is important to know about for embedding. There is an issue to update the docs, but what you need to know is that if you are using widgets, you now also need to load additional scripts from CDN:

http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.11.1.min.js
http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.11.1.min.css
like image 110
bigreddot Avatar answered Oct 11 '22 14:10

bigreddot