Unable to import data from csv file to d3

I am just learning d3 and I am trying to import data from a CSV file, but I always get the error "XMLHttpRequest cannot load file: ///Users/Laura/Desktop/SampleECG.csv. Requests are supported only for HTTP.". I searched how to fix this error and ran it on the local web server, but I did not find a solution that works for d3.v2.js. Here's a sample code:

var Time = [] ECG1 = [] d3.csv("/Desktop/d3Project/Sample.csv", function(data) { Time = data.map(function(d) {return [+d["Time"]];}); ECG1 = data.map(function(d) {return [+d["ECG1"]];}); console.log(Time) console.log(ECG1) }); 

Any help would be greatly appreciated.

+6
source share
3 answers

It also embarrassed me (I also start d3).

So, for some reason, web browsers are unhappy with loading local data, perhaps for security reasons or something else. In any case, to get around this, you need to start the local web server. It is easy.

In your terminal, after cd ing to the root site of your site (thanks @daixtr), type:

 python -m SimpleHTTPServer 8888 & 

Well, now that this terminal window is open and running, your local 8888 web server will be launched.

So, in my case, the original webpage I was working on was called

 file://localhost/Users/hills/Desktop/website/visualizing-us-bls-data-inflation-and-prices.html 

When I opened it in chrome. To open my page on my local web server, I simply typed (into the chrome search bar):

 http://localhost:8888/Desktop/website/visualizing-us-bls-data-inflation-and-prices.html 

Reading in CSV should now work. Strange, I know.

+15
source

For those who use the python built-in web server and who are still experiencing problems, use the โ€œREMEMBERโ€ function and make sure that you launch the โ€œpython -m SimpleHTTPServer 8888โ€ call on the correct path that you consider your DocumentRoot to be. That is, you cannot just run "python -m SimpleHTTPServer 8888" anywhere. You should actually "cd / to / correct / path /" containing your index.html or data.tsv, and then run "python -m SimpleHTTPServer 8888" from there.

+5
source

Use Firefox, idk, what Chrome is trying to do

0
source

All Articles