Cannot import data from csv file in d3
Asked Answered
C

4

19

I'm just learning d3, and I'm attempting to import data from a CSV file, but I keep getting the error "XMLHttpRequest cannot load file:///Users/Laura/Desktop/SampleECG.csv. Cross origin requests are only supported for HTTP. ". I've searched for how to fix this error and have ran it on a local web server, but I haven't found a solution that works for d3.v2.js. Here's a sample of the 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 will be much appreciated.

Ciapha answered 8/1, 2014 at 21:13 Comment(5)
Are you running a local webserver? See e.g. this tutorial.Kemeny
What issues have you encountered with the local web server? Ajax requests won't work with the file: protocol, so a web server (whether it's local or not) is required. Ideally would would server both the script and the csv file from the same server.Gouge
I have faced the same problem. check do you gave correct file path? and to open file:/// , You should perform the request using local server. Try to open with firefox first. since it handle the request exception. And please provide more information as much as possible (i.e) your console logs and some more information.Dao
If your goal is to run a local web server with minimal hassle, consider a browser extension such as Web Server for Chrome.Charley
I posted some solutions over hereImmunotherapy
L
34

This confused me too (I am also a d3 beginner).

So, for some reason, web browsers are not happy about you loading local data, probably for security reasons or something. Anyways, to get around this, you have to run a local web server. This is easy.

In your terminal, after cd-ing to your website's document root (thanks @daixtr), type:

python -m SimpleHTTPServer 8888 &

Okay, now as long as that terminal window is open and running, your local 8888 web server will be running.

So in my case, originally the web page 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 up my page on my local web server, I just typed (into the chrome search bar):

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

Now, reading in CSVs should work. Weird, I know.

Lump answered 25/9, 2014 at 5:8 Comment(4)
I tried this and end up getting this error in console: XMLHttpRequest cannot load localhost:8888/test.csv. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. Have you ran into this as well?Bereave
Had the same issue as your comment, just request the html page the same way as the .csv so that they are of the same origin. So instead of opening the .html from the file in windows explorer, request it in the browser: http://localhost:8888/Desktop/website/mypage.htmlTaunton
SimpleHTTPServer has been renamed to http.server in python 3Plectrum
I'd also add that you can access Terminal and drag & drop the file in, hit enter, get an error, then copy in the python code.Bibliofilm
E
6

To those using built-in python webserver and who are still experiencing issues, do REMEMBER and make sure that you run the "python -m SimpleHTTPServer 8888" invocation at the correct path of which you consider to be your DocumentRoot. That is, you cannot just run 'python -m SimpleHTTPServer 8888' anywhere. You have to actually 'cd /to/correct/path/' containing your index.html or data.tsv and then from there run 'python -m SimpleHTTPServer 8888'.

Ernaline answered 9/2, 2015 at 1:5 Comment(1)
That's a great point. I added it to the main answer so people see.Lump
P
6

Also, just learning D3 for school work. I was trying to run this simple D3 example: https://gist.github.com/d3noob/b3ff6ae1c120eea654b5

I had the same problem as OP re: loading data using Chrome browser. I bet the great solution Hillary Sanders posted above was re: Python 2.X.

My answer is re: Python 3.X [OS: Ubuntu 16x]:

Open a terminal window within the root directory of your project, then run:

python3 -m http.server

It will serve HTTP on port 8000 by default unless it is already taken, in that case to open another port, e.g. 7800, run:

python3 -m http.server 7800

Then, on your Chrome browser address bar type:

localhost:8000

The above worked for me because I only had an index.html page in my root folder. In case, you have a HTML page with a different name, type the whole path to that local HTML page and it should work also. And, you should be able to see the graph created from the data set in my link (that must be in a folder like data/data.csv). I hope this helps. :-)

Parkinson answered 24/2, 2020 at 20:7 Comment(0)
A
2

Use Firefox, idk what Chrome tries to accomplish

Anode answered 26/2, 2017 at 2:26 Comment(2)
Want to know the reason why chrome wasn't able to do thisCorroborate
I faced the same CORS issue on Firefox as well.Nunnally

© 2022 - 2024 — McMap. All rights reserved.