These libraries are foundational cornerstones of web-based data visualization and web-based data science, respectively.
However, they don't work together very well.
Even subtle changes will have side-effects on your interface—not really tenable.
py-d3 is an idempotent D3 shim for Jupyter. It monkey-patches the default
d3.selectAll()selector methods with cell-specific versions that, at runtime, only see objects created inside of the currently executed cell. This approach was suggested to me by D3.JS creator Mike Bostock himself.
At runtime, all you have to do is declare
%%d3 at the top of your code cell and you're ready to go:
%%d3 <g></g> <script>d3.select("g").text("Hello World!")</script>
py_d3 is still probably the best way to sandbox D3.JS code in a Jupyter environment, these days I recommend using Observable Notebooks instead.