A.Youssef A.Youssef - 1 month ago 8
Python Question

How to return two arrays with jsonify in Flask?

I'm trying to return two different lists that are representing a chart values and labels to jsonify then to be passed to Javascript function to draw the chart, but it never worked with me. It only worked when i returned a single list for values without the labels list.

The following is my Flask script:

enter image description here

The following is my Javascript code containing jQuery and AJAX function:



<script>

var getValues = $.get('/data');
getValues.done(function(values,labels){

var data = {
labels: [
labels.labels
],

series: [
values.values
] };

var options = {
width : 800,
height : 400
}

var myChart = new Chartist.Bar('.ct-chart', data, options);

});

</script>

Answer

Your /data endpoint returns JSON object which you can specify in your $.get parameter.

$.get('/data', function(response){
    var data = {
        labels: response.labels,
        series: [response.values]
    };

    var options = {
      width : 800,
      height : 400
    }

    var myChart = new Chartist.Bar('.ct-chart', data, options);
}, "json");
Comments