Trying to include javascript in a notebook
Trying to include javascript in a notebook¶
Following http://nbviewer.ipython.org/github/ipython/ipython/blob/1.x/examples/notebooks/Part%205%20-%20Rich%20Display%20System.ipynb and http://nbviewer.ipython.org/gist/fonnesbeck/ad091b81bffda28fd657 , let's try to integrate an interactive D3 plot within a notebook.
In [2]:
from IPython.display import IFrame, HTML
from IPython.core.display import display
In [11]:
s = """
<style>
rect {
fill: none;
pointer-events: all;
}
circle {
fill: none;
stroke: #000;
}
</style>
<body>
<center><table border=none width=100%% height=100%%>
<tr>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var w = 960,
h = 500;
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
svg.append("rect")
.attr("width", w)
.attr("height", h);
svg.append("g").selectAll("circle")
.data(d3.range(110))
.enter().append("circle")
.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")")
.attr("r", function(d) { return d * 5; });
var circle = svg.append("g").selectAll("circle")
.data(d3.range(60))
.enter().append("circle")
.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")")
.attr("r", function(d) { return d * 3; });
svg.on("mousemove", function() {
var mouse = d3.mouse(this),
r = (Math.sqrt(mouse[0]) + 10) / 10;
circle
.attr("transform", "translate(" + mouse + ")")
.attr("r", function(d) { return d * r; });
});
</script>
</tr>
</table></center>
"""
In [12]:
HTML(s)
Out[12]:
In [13]:
display(IFrame("http://demographics.coopercenter.org/DotMap/index.html", '800px', '600px'))
In [14]:
display(HTML(s))
In [ ]: