简体   繁体   中英

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:

在此处输入图片说明

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>

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM