在Tornado应用程序中使用Xstatic的实用程序
tornado_xstatic的Python项目详细描述
XStatic是 为python应用程序打包静态文件,特别是js库。 Tornado是一个python web框架。
此集成提供:
- XStaticFileHandler提供xstatic包中的静态文件。
- xstatic_urlui方法为xstatic文件构建url,包括 tornado用于缓存失效的?v=...标记。
使用这些:
importtornado.ioloopimporttornado.webfromtornado_xstaticimportXStaticFileHandler,xstatic_urlclassMyHandler(tornado.web.RequestHandler):defget(self):self.render("mytemplate.html")if__name__=="__main__":application=tornado.web.Application([(r"/",MyHandler),(r"/xstatic/(.*)",XStaticFileHandler,{"allowed_modules":["jquery","bootstrap"]}),],ui_methods={'xstatic_url':xstatic_url('/xstatic/')})application.listen(8888)tornado.ioloop.IOLoop.instance().start()
传递allowed_modules是可选的:如果没有提供,则来自 X可提供静态模块。
在模板中,您可以执行以下操作:
<script src="{{ xstatic_url('jquery', 'jquery.min.js') }}"></script> <script src="{{ xstatic_url('bootstrap', 'js/bootstrap.min.js') }}"></script> <link href="{{ xstatic_url('bootstrap', 'css/bootstrap.min.css') }}" rel="stylesheet">