javascriptd3.jsflaskdynatable

no data appears in javascript


I have the following script:

var dataSet;
d3.json("/TableData", function(error, json) {
  if (error) return console.warn(error);
  dataSet = json;
});

$('#data-table').dynatable({
  dataset: {
    records: dataSet
  }
});

/TableData is a sqlalchemy call to a db which returns:

{
  "data": [
{
  "Type": "Turbine", 
  "Seconds": 170, 
}, 
{
  "Type": "Turbine", 
  "Seconds": 75435, 
}]}

I don't get any errors with the console but nothing shows up in the flask website? if I go to http://127.0.0.1/TableData I can see the table data.


Solution

  • This is why because d3.json function is asynchronous and dataSet variable may be set only after the execution of dynatable code.

    So try this way.

    d3.json("/TableData", function(error, json) {
      if (error) return console.warn(error);
      var dataSet = json;
      $('#data-table').dynatable({
        dataset: {
           records: dataSet
        }
      });
    });
    

    You can confirm this by console logging the dataSet variable value from inside the d3.json callback function and outside the function.