diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-10-30 16:07:10 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-10-30 16:07:10 +0800 |
commit | 90b877a2c785bb7691249d31e23138e0385001ae (patch) | |
tree | 7e775e4009d97ce611bc4a8f4c83f6ea2694ae13 | |
parent | 9d7c7bb53244d2f79173b0f7fe1c6e91a656a240 (diff) | |
download | fg21sim-90b877a2c785bb7691249d31e23138e0385001ae.tar.bz2 |
webui: Build the Web UI using Tornado with WebSocket
Tornado: http://www.tornadoweb.org/
-rw-r--r-- | fg21sim/webui/__init__.py | 4 | ||||
-rw-r--r-- | fg21sim/webui/app.py | 37 | ||||
-rw-r--r-- | fg21sim/webui/websocket.py | 27 | ||||
-rw-r--r-- | requirements.txt | 1 | ||||
-rwxr-xr-x | setup.py | 1 |
5 files changed, 70 insertions, 0 deletions
diff --git a/fg21sim/webui/__init__.py b/fg21sim/webui/__init__.py new file mode 100644 index 0000000..637e653 --- /dev/null +++ b/fg21sim/webui/__init__.py @@ -0,0 +1,4 @@ +# Copyright (c) 2016 Weitian LI <liweitianux@live.com> +# MIT license + +from .app import make_application diff --git a/fg21sim/webui/app.py b/fg21sim/webui/app.py new file mode 100644 index 0000000..63d80be --- /dev/null +++ b/fg21sim/webui/app.py @@ -0,0 +1,37 @@ +# Copyright (c) 2016 Weitian LI <liweitianux@live.com> +# MIT license + +""" +Web user interface (UI) of "fg21sim" based upon Tornado_. + +.. _Tornado: http://www.tornadoweb.org/ +""" + +import os + +import tornado.web + +from .websocket import EchoWSHandler + + +class IndexHandler(tornado.web.RequestHandler): + def get(self): + self.render("index.html") + + +_settings = { + # The static files will be served from the default "/static/" URI + "static_path": os.path.join(os.path.dirname(__file__), "static"), + "template_path": os.path.join(os.path.dirname(__file__), "templates"), +} + + +def make_application(**kwargs): + settings = _settings + settings.update(kwargs) + appplication = tornado.web.Application( + handlers=[ + (r"/", IndexHandler), + (r"/ws", EchoWSHandler), + ], **settings) + return appplication diff --git a/fg21sim/webui/websocket.py b/fg21sim/webui/websocket.py new file mode 100644 index 0000000..ab846df --- /dev/null +++ b/fg21sim/webui/websocket.py @@ -0,0 +1,27 @@ +# Copyright (c) 2016 Weitian LI <liweitianux@live.com> +# MIT license + +""" +Communicate with the "fg21sim" simulation program through the Web UI using +the WebSocket_ technique, which provides full-duplex communication channels +over a single TCP connection. + +.. _WebSocket: https://en.wikipedia.org/wiki/WebSocket , + http://caniuse.com/#feat=websockets +""" + +import tornado.websocket + + +class EchoWSHandler(tornado.websocket.WebSocketHandler): + def open(self): + print("WebSocket opened") + + def on_message(self, message): + print("Message received: %s" % message) + msg_back = message[::-1] + print("Message sent back: %s" % msg_back) + self.write_message(msg_back) + + def on_close(self): + print("WebSocket closed") diff --git a/requirements.txt b/requirements.txt index 60199f0..8d240fc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ healpy configobj beautifulsoup4 requests +tornado @@ -93,6 +93,7 @@ setup( "configobj", "beautifulsoup4", "requests", + "tornado", ], tests_require=["pytest"], cmdclass={"test": PyTest}, |