Tornado: how to make it to catch changes done to JavaScript?
Clash Royale CLAN TAG#URR8PPP
Tornado: how to make it to catch changes done to JavaScript?
I'm trying to use Tornado and found that every time when I changed JavaScript or HTML files don't look changed.
I have an experience with Node.js and it worked well - if changes were not related with the server side everything updated itself.
How to solve this problem? Of course, I can stop the server every time but it's time-consuming and tedious.
Thanks.
2 Answers
2
In your project folder where you tornado application is, you can listen for changes using the autoreload module. e.g
from os import walk
from socket import gethostname
from tornado import autoreload
from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.httpserver import HTTPServer
from tornado.web import RequestHandler
from tornado.options import define, options, parse_command_line
class TestHandler(RequestHandler):
def get(self):
self.render("index.html")
class TestApplication(Application):
def __init__(self, host=None, port=None):
handlers = [
(r"/", TestHandler)
]
settings =
"autoreload": True,
"compiled_template_cache": True,
"debug": True,
"static_path": "static",
"template_path": "templates"
Application.__init__(self, handlers, **settings)
self.host = host
self.port = port
def __call__(self):
if self.port is None:
define("port", default=8001, help="run on the given port", type=int)
else: define("port", default=self.port, help="run on the given port", type=int)
if self.host is None:
define("host", default=gethostname(), help="run on host address", type=str)
else: define("host", default=self.host, help="run on host address", type=str)
parse_command_line()
http_server = HTTPServer(self)
try:
http_server.listen(options.port, address=options.host)
except (KeyboardInterrupt, SystemExit):
print("*!* keyboard interrupt detected...exiting.")
except:
print("*!* error occured...httpserver not listening.")
else:
print("*+* server listening on host port...".format(host=options.host, port=options.port))
autoreload.start()
for dir, _, files in walk('.'):
[autoreload.watch(dir + '/' + f) for f in files if f.endswith("js") or f.endswith("css") or f.endswith("html")]
IOLoop.current().start()
Then just call it and it will autoreload the server whenever you make changes to the files ending with the suffixes specified. i.e .js, .html, .css
It's not entirely clear from your question, but assuming you're serving your HTML and JS from Tornado's StaticFileHandler, you need to pass either debug=True
or static_hash_cache=False
to your Application
constructor.
debug=True
static_hash_cache=False
Application
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.