jquery - How to use Ajax with Flask -


i'm visualizing live charts example here: highcharts. basically, ajax request data php script live-server-data.php generates random data :

/**  * request data server, add graph , set timeout   * request again  */ function requestdata() {     $.ajax({         url: '/',         success: function(point) {             var series = chart.series[0],                 shift = series.data.length > 20; // shift if series                                                   // longer 20              // add point             chart.series[0].addpoint(point, true, shift);              // call again after 1 second             settimeout(requestdata, 1000);             },         cache: false     }); }   //---live-server-data.php <?php  $x = time() * 1000;   $y = rand(0, 100);    $ret = array($x, $y); echo json_encode($ret); ?> 

how similar thing in flask i.e. request randomly generated numbers flask?

from flask import flask, render_template, request, jsonify import random, datetime, json import time  app = flask(__name__)  @app.route('/') def index():      x = int(round(time.time() * 1000))     y = random.randint(0, 100)     return json.dumps([x, y])   if __name__ == '__main__':     app.run(debug=true) 

its simpler in flask:

@app.route('/') def index():     x = datetime.timestamp(datetime.now()) * 1000     y = random.randint(0, 100)     return json.dumps([x, y]) 

but note you'll have add following imports @ top in order work:

import random, datetime, json datetime import datetime 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -