0

I'm starting to learn Django and one of the projects I have chosen for myself is one that has an HTML front page, in which the user inputs some search text and then that calls to an API using Python and Pytrends, then its saves that information as a CSV file.

I'm having a hard time with the next part which is importing that CSV file into another HTML page, where I would like to use my chart.js file to turn it into a graph. I can't figure out how to open up the file and run it inside of javascript. I have tried a static load but it's not opening up the file, just the path to the file. Please let me know if you need to see any other files or know anything else.

async function getData() {
   const data = '{% static "interest.csv" %}';
   console.log(data.text());

   const table = data.split("\n").slice(1);
   table.forEach((row) => {
     const columns = row.split(",");
     const year = columns[0];
     const interest = columns[1];
     xlabel.push(year);
     console.log(year, interest);
});
OldWizard007
  • 370
  • 1
  • 8
Aaron Cloud
  • 315
  • 4
  • 15
  • 1
    Does this answer your question? [How to read data From \*.CSV file using javascript?](https://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript) – Iain Shelvington Jun 18 '21 at 03:48
  • No unfortunately not. I have seen that post and tried that, however all I get is a null on the console log. – Aaron Cloud Jun 18 '21 at 03:57
  • 1
    What is `{% static "interest.csv" %}`? Assuming it's replaced on the server somehow, what is `data` actually set to in source code used on the front end? – traktor Jun 18 '21 at 04:40
  • @traktor {% static "interest.csv" %} django loading in a static file such as a csv, at least thats how I understood it when I read it. I was attempting to load it using python since I'm getting nowhere when I try to load it with javascript. – Aaron Cloud Jun 18 '21 at 04:56
  • 1
    The chance of CSV text meeting the syntax requirements of a string literal in JavaScript is zero. JavaScript can download a file from a server and process it after the response has been received, but doesn't have support for decoding CSV text. I suggest learning about the [`fetch` API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and reading up on others' efforts to decode CSV on the front end. A slightly different option is to convert the CSV to JSON text on the server first. – traktor Jun 18 '21 at 05:33
  • @traktor My problem with the fetch api is that it gets the local host url plus the name of the json file instead of just pulling the local json file. And I'm sure this is all my fault because I don't fully understand how to use it but I'm reading and learning any help would be appreciated tho. – Aaron Cloud Jun 18 '21 at 20:40
  • 1
    If by "local file" you mean a file on the PC, web pages can only access the local file system (using rthe `file://` protocol) if the page itself was loaded from the local file system. To access local files in a page served from a localhost server, you will need to create an end point on the server to respond to HTTP GET requests for local files. In a Node/Express server you could simply set up a static file server path - not sure about Python/Django. – traktor Jun 18 '21 at 22:52

0 Answers0