From f35470d9f4235fe4eaed42ccb71e0a96a796e55a Mon Sep 17 00:00:00 2001 From: nopnop2002 Date: Wed, 16 Nov 2022 19:43:30 +0900 Subject: [PATCH] Added auto refresh --- flask/can.py | 37 +++++++++++++++++++++++++++--------- flask/templates/index.html | 35 ++++++++++++++++++++++++++++++++++ tornado/can.py | 34 +++++++++++++++++++++++++-------- tornado/templates/index.html | 35 ++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 flask/templates/index.html create mode 100644 tornado/templates/index.html diff --git a/flask/can.py b/flask/can.py index 5b4c652..2343465 100644 --- a/flask/can.py +++ b/flask/can.py @@ -2,10 +2,12 @@ # -*- coding: utf-8 -*- # Simple REST Server +import os import sys import json import datetime -from flask import Flask, request +import argparse +from flask import Flask, request, render_template app = Flask(__name__) database = [] @@ -13,13 +15,21 @@ database = [] @app.route("/") def root(): global database - res = "" + items = [] for data in database: - print("{}".format(data)) - #print("{}".format(len(data[2]))) - res = res + "{} {:x} {} {}".format(data[0],data[1],data[2],data[3]) + "
" - #return "Hello World!" - return res + #print("{}".format(data)) + datetime = data[0].split() + #print("data={} datetime={}".format(data[0], datetime)) + + items.append({ + "date": datetime[0], + "time": datetime[1], + "id": data[1], + "frame": data[2], + "value": data[3] + }) + #print("items={}".format(items)) + return render_template("index.html", lines=app.config['lines'], items=items) #curl -X POST -H "Content-Type: application/json" -d '{"canid":101, "frame":"standard" , "data": [1,2,3,4,5,6,7,8]}' http://192.168.10.43:8000/post @app.route("/post", methods=["POST"]) @@ -48,7 +58,9 @@ def post(): global database #print("{} {} {}".format(type(database), len(database), database)) - if(len(database) >= 20): + print("lines={}".format(app.config['lines'])) + #if(len(database) >= 20): + if(len(database) >= app.config['lines']): database.pop(0) database.append(items) @@ -56,6 +68,13 @@ def post(): return data if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('-port', type=int, default=8000) + parser.add_argument('-lines', type=int, default=20) + args = parser.parse_args() + print("port={}".format(args.port)) + print("lines={}".format(args.lines)) + app.config['lines'] = args.lines #app.run() - app.run(host='0.0.0.0', port=8000, debug=True) + app.run(host='0.0.0.0', port=args.port, debug=True) diff --git a/flask/templates/index.html b/flask/templates/index.html new file mode 100644 index 0000000..f11c22f --- /dev/null +++ b/flask/templates/index.html @@ -0,0 +1,35 @@ + + + + + CAN DATA + + + + Display the latest {{ lines }} records.
+ If you want to see more, specify the number of lines at startup.
+ + + + + + + + + + + + {% for i in items %} + + + + + + + + {% endfor %} + +
DateTimeIDFrameValue
{{ i.date }}{{ i.time }}{{ i.id }}{{ i.frame }}{{ i.value }}
+ + + diff --git a/tornado/can.py b/tornado/can.py index 69e477f..1604946 100644 --- a/tornado/can.py +++ b/tornado/can.py @@ -2,9 +2,11 @@ # -*- coding: utf-8 -*- # Simple REST Server -import json +import os import sys +import json import datetime +import werkzeug import tornado.httpserver import tornado.ioloop import tornado.options @@ -12,18 +14,28 @@ import tornado.web from tornado.options import define, options define("port", default=8000, help="run on the given port", type=int) +define("lines", default=20, help="number of lines displayed", type=int) database = [] class IndexHandler(tornado.web.RequestHandler): def get(self): global database - res = "" + items = [] for data in database: - print("{}".format(data)) - #print("{}".format(len(data[2]))) - res = res + "{} {:x} {} {}".format(data[0],data[1],data[2],data[3]) + "
" - self.write(res) + #print("{}".format(data)) + datetime = data[0].split() + #print("data={} datetime={}".format(data[0], datetime)) + + items.append({ + "date": datetime[0], + "time": datetime[1], + "id": data[1], + "frame": data[2], + "value": data[3] + }) + #print("items={}".format(items)) + self.render("index.html", lines=options.lines, items=items) #curl -X POST -H "Content-Type: application/json" -d '{"canid":101, "frame":"standard" , "data": [1,2,3,4,5,6,7,8]}' http://192.168.10.43:8000/post class PostHandler(tornado.web.RequestHandler): @@ -51,7 +63,9 @@ class PostHandler(tornado.web.RequestHandler): global database #print("{} {} {}".format(type(database), len(database), database)) - if(len(database) >= 20): + print("lines={}".format(options.lines)) + #if(len(database) >= 20): + if(len(database) >= options.lines): database.pop(0) database.append(items) @@ -59,11 +73,15 @@ class PostHandler(tornado.web.RequestHandler): if __name__ == "__main__": tornado.options.parse_command_line() + print("port={}".format(options.port)) + print("lines={}".format(options.lines)) app = tornado.web.Application( handlers=[ (r"/", IndexHandler), (r"/post", PostHandler), - ],debug=True + ], + template_path=os.path.join(os.getcwd(), "templates"), + debug=True ) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) diff --git a/tornado/templates/index.html b/tornado/templates/index.html new file mode 100644 index 0000000..a888123 --- /dev/null +++ b/tornado/templates/index.html @@ -0,0 +1,35 @@ + + + + + CAN DATA + + + + Display the latest {{ lines }} records.
+ If you want to see more, specify the number of lines at startup.
+ + + + + + + + + + + + {% for i in items %} + + + + + + + + {% end %} + +
DateTimeIDFrameValue
{{ i['date'] }}{{ i['time'] }}{{ i['id'] }}{{ i['frame'] }}{{ i['value'] }}
+ + +