Demo entry 6317001

test

   

Submitted by anonymous on Nov 04, 2016 at 23:26
Language: Python 3. Code size: 1.4 kB.

from flask import Flask, render_template, jsonify, request, url_for
import serial
import datetime
import os

app = Flask(__name__)


ser = serial.Serial('/dev/ttyUSB0')
ser.readline()


@app.route('/')
def  index():
	return  render_template('index.html', menu='home.html')

@app.route('/arduino')
def arduino():
	y_temp = float(ser.readline().decode("utf-8").strip("\n"))
	i = datetime.datetime.now()
	data = "{0}/{1}/{2} {3}:{4}:{5} Temperature is : {6}".format(i.day, i.month, i.year, i.hour, i.minute, i.second, round(y_temp,2))
	return  render_template('index.html', menu='arduino.html', data=data)
	
	
	
	
#javascript functions
@app.route('/updatedata')
def updatedata():
	y_temp = float(ser.readline().decode("utf-8").strip("\n"))
	data = round(y_temp,5)
	return jsonify(result=data)
	
	
#for css update	
@app.context_processor
def override_url_for():
    return dict(url_for=dated_url_for)

def dated_url_for(endpoint, **values):
    if endpoint == 'static':
        filename = values.get('filename', None)
        if filename:
            file_path = os.path.join(app.root_path,
                                     endpoint, filename)
            values['q'] = int(os.stat(file_path).st_mtime)
    return url_for(endpoint, **values)	
	
if __name__  == "__main__":
	app.run(host='0.0.0.0',
			port=5000,
			debug = True)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).