I'm trying to get a python script to regularly push the current time to an HTML page using Flask. (This is not my end goal, it's just a very simple version of the problem I'm trying to solve.) I can't seem to get the JavaScript to continuously request updated data from the Python script. What am I doing wrong? This code is largely pieced together from similar questions posted before, but I still can't figure out how to get it to work.
Python code:
from flask import Flask, render_template
from datetime import datetime
app = Flask(__name__)
@app.route("/")
def template_test():
return render_template('template.html')
@app.route('/get_time', methods= ['GET', 'POST'])
def stuff():
result = datetime.utcnow()
return jsonify(result=result)
if __name__ == '__main__':
app.run(debug=True)
HTML:
<html>
<body>
<p id="result">Time goes here</p>
<script>
setInterval(
function()
{
$.getJSON(
$SCRIPT_ROOT + '/get_time',
{},
function(data)
{
$("#result").text(data.result);
});
},
500);
</script>
</body>
</html>