Appendix A. Setup, Tools, Resources

Setup

To work with D3, you need to run a web server, either locally or hosted, to serve pages, JavaScript files, and other resources (such as data files). In principle, it is possible to load a local page using the file: protocol and any JavaScript files referenced by it. But browsers may prevent your JavaScript code from loading other resources, such as data files, in this way, depending on the browser’s cross-origin resource sharing (CORS) policy. Browsers are inconsistent in this regard; it is probably best to sidestep the issue by always using a web server when working with D3.

Setting up a web server need not be a challenge: several minimal web servers can be run without further configuration from the command line,1 and many programming languages include ready-to-use web server modules as well. The D3 website recommends http-server, which is a Node.js package. If you have the Node runtime and the npm package manager installed, you can install and run a web server using:2

npm install -g http-server
http-server ./project -p 8080

Because they are part of its standard distribution, Python’s web server modules are ubiquitous, but can be quite slow, even for development work (the -d argument requires Python 3.7 or later):

python -m http.server -d ./project 8080	 # Python 3
python -m SimpleHTTPServer 8080          # Python 2: current dir

The busybox set of tools should be installed on all Debian-derived Linux distributions by default. Its built-in ...

Get D3 for the Impatient now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.