Tornado + bootstrap quickly builds your own website, tornadobootstrap
Since bootstrap is so popular and saves a lot of trouble, why not use it? In addition, tornado of niux's produced by FB is a huge addition!
1. Install the configuration
The library required for installation. There is nothing to talk about here. Tornado uses easy_install or pip directly. Bootstrap just goes down. Of course, you also need to download jquery on which bootstrap depends. Download and install them one by one.
2. directory structure
Put the content in the bootstrap directory in css, fonts, and js under the static directory. Js corresponding to jquery is also stored in the js directory under the static directory.
The templatesdirectory stores HTML files. You can see a high-light index.html file.
3. tornado code
import loggingimport tornado.authimport tornado.escapeimport tornado.ioloopimport tornado.webimport os.pathimport uuidfrom tornado.concurrent import Futurefrom tornado import genfrom tornado.options import define, options, parse_command_linedefine("port", default=8888, help="run on the given port", type=int)define("debug", default=False, help="run in debug mode")class BaseHandler(tornado.web.RequestHandler): def get_current_user(self): user_json = self.get_secure_cookie("chatdemo_user") if not user_json: return None return tornado.escape.json_decode(user_json)class MainHandler(BaseHandler): @tornado.web.authenticated def get(self): self.render("index.html", messages=global_message_buffer.cache)def main(): parse_command_line() app = tornado.web.Application( [ (r"/", MainHandler), ], cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__", template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), xsrf_cookies=True, debug=options.debug, ) app.listen(options.port) tornado.ioloop.IOLoop.instance().start()if __name__ == "__main__": main()
Define defines the site port.
Class BaseHandler (tornado. web. RequestHandler)Defines a base class for simple encapsulation of tornado RequestHandler. In the future, each class needs to integrate this BaseHandler, for example, MainHandler. In this way, the http request can be obtained.
Finally(R "/", MainHandler), Bind the request url and the corresponding handler. At this time, it cannot be run, because we need to parse the template html in MainHandler.
4. Html Template
Here, the index.htm template is obtained directly from the bootstrap example.
<!DOCTYPE html>
Css and js are all placed under the static directory in the directory organization above. Therefore, you must make some modifications to the content in the original template:<Link href = "{static_url (" css/bootstrap.min.css ")}" rel = "stylesheet">And<Script src = "{static_url (" js/jquery-1.11.2.min.js ") }}"> </script>And<Script src = "{static_url (" js/bootstrap. min. js ") }}"> </script>. The built-in static_url method is used to redirect paths.
If you do not want to set the path like this, but these css and js files still need to be placed in the static directory. Otherwise, static content may not be obtained.
Run the code at this time and you will see this webpage.
Well, it seems that there are still some problems at this time. But the general structure is like this!