Arslán
Arslán

Reputation: 1773

Passing data from a file to a variable in Javascript

I am a Python person and dont understand Javascript. However i am stuck at a situation where I am generating a data stream from a python script that needs to be consumed by a javascript to display a wordcloud. I am using wordcloud2.js

I need to be able to pass the data to the variable 'list' (as below) from an external file (url). I tried several things from the internet but nothing seems to work. Can someone please help me fix it.

<script>
var div = document.getElementById("sourrounding_div");
var canvas = document.getElementById("canvas_cloud");
canvas.height = div.offsetHeight;
canvas.width  = div.offsetWidth;
var options = 
{
  list : [['A1', 20.0],['A2',30],['A3',40],],

  gridSize: Math.round(0.21 * document.getElementById('canvas_cloud').offsetWidth / 1024),
  weightFactor: function (size) {
    return Math.pow(size, 1.4) * document.getElementById('canvas_cloud').offsetWidth / 1024;
  }
}
WordCloud(document.getElementById('canvas_cloud'), options); 
</script>

Upvotes: 0

Views: 79

Answers (1)

cesarcosta99
cesarcosta99

Reputation: 11

You can use AJAX (JSON returned example):

var request = new XMLHttpRequest();
request.open('GET', 'url', true);

request.onload = function() {
  if (request.status == 200) {
    var list = JSON.parse(request.responseText);
  }
};

request.send();

Remember that a request is asynchronous.

Upvotes: 1

Related Questions