0

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>
3
  • Can you please help me understand what your problem is exactly? Is it that you do not know how to request data from the server? Commented Oct 8, 2016 at 1:08
  • Are you able to get it working using static data? Commented Oct 8, 2016 at 1:08
  • yes, the above mentioned script works correctly, I just want to be able to read the list from a text file. Commented Oct 8, 2016 at 1:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.