Turn off template processing in Tornadoweb

I need to use Tornadoweb as a RESTfull for our existing AngularJs application.

{{}} are heavily used in an angular application. I would like to serve angular files from tornadoes as static files

Is there a way to turn off tornado template processing to avoid conflicts with the {{}} tornadoes used?

I know how to change {{}} in an angular application using the $ interpolateProvider method, but it will include many changes in the angular application.

As a workaround, I put the angular application in a static tornado folder and used redirection:

class IndexHandler(RequestHandler): def get(self): self.redirect(self.static_url('ng-app/index.html'),True) 

It works, but this is not a good solution because the display URL looks like this:

 http://localhost:8080/static/ng-app/index.html?v=efb937e6a0cb0739eb0edfd88cfb4844 

Any better idea?

Thank you in advance

+6
source share
4 answers

Use {{! tag}} to disable tornado translation.

+4
source

You can use Tornado built into StaticFileHandler. This transfers everything as static files in Angular:

 from tornado import options, ioloop, web options.define("port", default=8888, help="run on the given port", type=int) SETTINGS = { "debug" : True } application = web.Application([ (r'/(.*)', web.StaticFileHandler, {"path": "angular_app"}) ],**SETTINGS) if __name__ == "__main__": options.parse_command_line() application.listen(options.options.port) ioloop.IOLoop.instance().start() 
+4
source

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() 
+2
source

stupid way:

just use:

 self.render('some-template.html', var1='{{var1}}') 

since {{var1}} template first removes {{ }} and then replaces var1 , so the result will be {{var1}} in the rendered html file;)

-1
source

Source: https://habr.com/ru/post/948012/


All Articles