We’re trying to get familiar with D3 (http://d3js.org/), in particular samples such as http://bl.ocks.org/mbostock/3306362 and http://bl.ocks.org/mbostock/2206590. It seems all these samples use local file IO to load geolocation info. The following code snippets are common:
queue() .defer(d3.json, "/mbostock/raw/4090846/us.json") .defer(d3.tsv, "unemployment.tsv") .await(ready)
while other samples often use this signature to load data:
d3.json("someJSONFile.json", function(error, uk) {
console.log(uk);
});
We’ve created several local html files to test out the samples, but we’re running into security issues. It’s apparent the script is accessing a local file, which is really giving us problems in a Microsoft stack (Apple or Linux isn’t an option at this time, though we have tried Chrome, with no success). How can we enable the html file or refactor the script to have access to the local files?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to host the files through a web server since web browsers restrict what types of files can be accessed locally. The simplest way to do this on a windows machine:
- Install python
-
Navigate to the directory holding your example with
cmd.exe. Holding down shift, right clicking on the folder with the example and selectingOpen Command Window Hereis the easiest way to do this. -
On the command prompt, enter
python -m SimpleHTTPServer 8000, orpython -m http.server 8000, on newer versions to start a web server. -
Open a web browser (I would really suggest chrome, the dev tools are way ahead of ff and ie), go to
127.0.0.1:8000. The example should show up.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0