Skip to content

Instantly share code, notes, and snippets.

@skazhy
Created August 12, 2013 12:05
Show Gist options
  • Save skazhy/6210290 to your computer and use it in GitHub Desktop.
Save skazhy/6210290 to your computer and use it in GitHub Desktop.
reverse_angular_url for Tornado
import tornado.web
class BaseHandler(tornado.web.RequestHandler):
def reverse_angular_url(self, name, *args):
# Works like regular reverse_url but replaces regex groups
# with :arg, so it can fit nicely in the AngularJS router
# for given route named "employee" "/company/(.*)/employees/(.*)$" ->
# reverse_angular_url("employee", "company_id", "employee_id")
# returns "/company/:company_id/employees/:employee_id"
if name in self.application.named_handlers:
raw_url = self.application.named_handlers[name]._find_groups()[0]
return raw_url % tuple(":" + a for a in args)
raise KeyError("%s not found in named urls" % name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment