A difficult and rather ugly solution: you can just use self.write() instead of self.render() to print the contents of the file. If this is an HTML page, then there will be more GET requests for .css, .js and image files, so you should have a second handler to return all of them. AngularJS application example: http://architects.dzone.com/articles/angularjs-get-first-impression
Project Tree:
$ tree . โโโ angular_app โ โโโ css โ โ โโโ app.css โ โ โโโ bootstrap.css โ โโโ img โ โ โโโ ajax-loader.gif โ โโโ index.html โ โโโ js โ โโโ app.js โ โโโ contollers โ โ โโโ CurrencyConvertCtrl.js โ โโโ db.js โ โโโ models โ โ โโโ Currency.js โ โโโ _references.js โ โโโ vendor โ โโโ angular.js โ โโโ bootstrap.js โ โโโ highcharts.js โ โโโ jquery-1.9.1.js โโโ test.py โโโ test.py~
Tornado Code:
#!/usr/bin/python # -*- coding: utf-8 -*- import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import logging from tornado.options import define, options define("port", default=8000, help="run on the given port", type=int) import os angular_app_path=os.path.join(os.path.dirname(__file__), "angular_app") class IndexHandler(tornado.web.RequestHandler): def get(self): with open(angular_app_path + "/index.html", 'r') as file: self.write(file.read()) class StaticHandler(tornado.web.RequestHandler): def get(self): self.set_header('Content-Type', '') # I have to set this header with open(angular_app_path + self.request.uri, 'r') as file: self.write(file.read()) if __name__ == "__main__": tornado.options.parse_command_line() app = tornado.web.Application( handlers=[(r'/', IndexHandler), (r'/js.*', StaticHandler), (r'/cs.*', StaticHandler), (r'/img.*', StaticHandler)]) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
source share