Green
Green

Reputation: 30795

Pyramid: How to get all app's routes within a view?

I want to see all the routes which my application has. Return them as a response like key=>value pair:

'route1' => '{foo:\w+}'
'route2' => '{baz:\w+\d+}'
... and so on

But I don't know how to get them within my view. For example, this is my view. I want it to return a map of routes. I do this:

@view_config(route_name='route1')
def someView(request):
    routes = request.registry.settings.getRoutes()  ## what should I print here to get a map of routes?
    r = ''
    for k,v in sorted(routes.items()):
        r += str(k) + "=>" + str(v) + "<br/>";
    return Response(r)

There is a RoutesConfiguratorMixin class with get_routes_mapper method. I tried to import the class and called its method but got an error that no registry was in the instance of it:

from pyramid.config.routes import RoutesConfiguratorMixin as Router

r = Router();
routes = r.get_routes_mapper();
## ... and the same code as above

Doesn't work.

Upvotes: 6

Views: 4365

Answers (3)

naoko
naoko

Reputation: 5216

Install pshell then pshell to login to pshell with your app config. then run

print("\n".join([r.path for r in app.routes_mapper.routelist]))

Upvotes: 1

pansen
pansen

Reputation: 371

Pyramid installs a bin script called proutes for that purpose.

Upvotes: 5

Michael Merickel
Michael Merickel

Reputation: 23331

There are 2 ways, one is supported (public) and one is unsupported (private).

Option #1 is to use the introspector and is explained here.

Option #2 is to use the route mapper (which is not a public api), in the way that the pyramid debugtoolbar does in its routes panel.

Upvotes: 10

Related Questions