@app.route('/Data',methods=['GET','POST'])
def Data():
if request.method == 'POST':
try:
_x1=1.0
_x2=2.0
_x3=3.0
_x4=4.0
mydict = {"X1":_x1,"X2":_x2,"X3":_x3,"X4":_x4}
with open('resultData.json', 'w') as f:
json.dump(mydict, f)
except:
return "Unexpected error:", sys.exc_info()[0]
return jsonify(mydict)
I created a Method Data and used it for app.route. With help of timer I want to call it periodically and show value on index.html. I used render_template and called index.html where i have defined _value variable. I am able to print value of X1, X2, X3 and X4 successfully but I am not able to see any value on my html page. What is wrong I am doing that I am not able to see values on my html? My main task is to update my html periodically as well as on any click from html page( click function works). I am new to flask.
def sensor():
response = requests.post("http://localhost:5000/Data")
with open('resultData.json', 'r') as fr:
dictonary = json.load(fr)
print(dictonary["X1"])
print(dictonary["X2"])
print(dictonary["X3"])
print(dictonary["X4"])
app.config['SERVER_NAME'] = 'http://localhost:5000'
with app.app_context():
return render_template('index.html', _value='{}{}{}{}'.format(dictonary["X1"],dictonary["X2"],dictonary["X3"],dictonary["X4"]))
sched = BackgroundScheduler(daemon=True)
sched.add_job(sensor,'interval',seconds=10)
sched.start()