Below is an example of how I am using d3.js with web2py controller. This is working for me.
But, I would prefer to use d3.js within an existing bootstrap page using web2py's default bootstrap layout. When I put this d3.js code into the bootstrap layout it does not cause any errors, but I am not seeing an <svg>
tag appended to <body>
.
Can someone post an example of how this can be done?
controllers/d3js.py
:
import random
def histogram():
dataset = [(random.randint(1,6) + random.randint(1,6)) for i in range(100)]
return dict(dataset=dataset, title='D3.js Histogram')
view/d3js/histogram.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{=title}}</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.min.js" ></script>
</head>
<body>
<script type="text/javascript">
{{from gluon.serializers import json}}
var dataset = {{=XML(json(dataset))}};
//Width and height
var w = 600;
var h = 600;
...
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
...
</script>
</body>
</html>
The source of this d3.js sample is Scott Murray's book, Interactive Data Visualizations for the Web.