[IPython-dev] D3 in IPython Notebook 2?

Anders Schneiderman ASchneiderman at asha.org
Fri Jun 6 17:20:58 EDT 2014


Thanks, Nicholas and Michael!  That's very helpful.

Anders Schneiderman
Database Services Manager | ASHA | (301) 296-8651<tel:(301)%20296-8651>

On Jun 6, 2014, at 3:11 PM, "Nicholas Bollweg" <nick.bollweg at gmail.com<mailto:nick.bollweg at gmail.com>> wrote:

Because d3 is require.js-aware, it is actually a little more complicated to do simple, static d3.

However, with the widget system it is far easier to do complex, dynamic d3.

To get just a little, using a remote d3.js, you basically still use the %%html cell magic approach:

%%html
<div id="foo">foo</div>
<script>
require.config({paths: {d3: "//d3js.org/d3.v3.min<http://d3js.org/d3.v3.min>"}});
require(["d3"], function(d3){
    d3.select("#foo").style("background-color", "blue")
})
</script>

This is kinda gross, as it uses IDs, but this is better than just attaching stuff to the parent document. Also, this method will potentially work better in nbviewer than widgets.

That being said, once you take the plunge and start working with widgets, traitlets and Backbone, you can do much cooler bi-drectional stuff than before.

Here is an example of getting information back from d3 into the kernel:
http://nbviewer.ipython.org/gist/anonymous/9975962

Note that traitlets for containers (list, dict) are not instrumented in core (yet), so you'll have to do a bit of your own bookkeeping there.

One thing that example doesn't take advantage of is IPython.html.install_nbextension: this is probably the most predictable way to install scripts into a place the notebook server can find them.

import IPython
IPython.html.install_nbextension("http://d3js.org/d3.v3.min.js")

d3 can then be required:

...
require(["nbextensions/d3.v3.min"], function(d3){...})
...

Hope this helps!
_______________________________________________
IPython-dev mailing list
IPython-dev at scipy.org<mailto:IPython-dev at scipy.org>
http://mail.scipy.org/mailman/listinfo/ipython-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20140606/631cde72/attachment.html>


More information about the IPython-dev mailing list