Pyjoyment.Routes - Always find your destination with routes¶
from Pyjoyment.Routes import Routes
# Simple route
r = Routes()
r.any('/').to(controller='blog', action='welcome')
# Or import the module, as in Google Python Style Guide
import Pyjoyment.Routes
# Simple route
r = Pyjoyment.Routes.new()
r.any('/').to(controller='blog', action='welcome')
# More advanced routes
blog = r.under('/blog')
blog.get('/list').to('blog#list')
blog.get('/:id', id=re.compile(r'\d+')).to('blog#show', id=23)
blog.patch(lambda c: c.render(text='Go away!', status=405))
Pyjoyment.Routes is the core of the Pyjoyment web framework.
Types¶
These placeholder types are available by default.
num
r.get('/article/<id:num>')
Placeholder value needs to be a non-fractional number, similar to the regular
expression ([0-9]+).
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojolicious::Routes of Mojolicious 9.52.
- class Pyjoyment.Routes.Pyjoyment_Routes[source]¶
Pyjoyment.Routesinherits all attributes and methods fromPyjoyment.Routes.Routeand implements the following new ones.- add_condition(name: str, cb: Condition) Self[source]¶
r = r.add_condition('foo', lambda route, c, captures, arg: ...)
Register a condition.
def foo(route, c, captures, arg): ... return True r.add_condition('foo', foo)
- add_shortcut(name: str, cb: Callable[[...], Any]) Self[source]¶
r = r.add_shortcut('foo', lambda route, *args: ...)
Register a shortcut.
r.add_shortcut('foo', lambda route, *args: ...)
- add_type(name: str, type: Constraint) Self[source]¶
r = r.add_type('foo', re.compile(r'\w+')) r = r.add_type('foo', ['bar', 'baz'])
Register a placeholder type.
r.add_type('lower', re.compile(r'[a-z]+'))
- base_classes¶
classes = r.base_classes r.base_classes = ['MyApp.Controller']
Base classes used to identify controllers, defaults to
Pyjoyment.ControllerandPyjoyment.
- cache: Pyjo_Cache[str, Any]¶
cache = r.cache r.cache = Pyjo.Cache.new()
Routing cache, defaults to a
Pyjo.Cacheobject.
- conditions: dict[str, Condition]¶
conditions = r.conditions r.conditions = {'foo': lambda route, c, captures, arg: ...}
Contains all available conditions.
- dispatch(c: Pyjoyment_Controller) bool[source]¶
boolean = r.dispatch(Pyjoyment.Controller.new())
- lookup(name: str) Pyjoyment_Routes_Route | None[source]¶
route = r.lookup('foo')
Find route by name with
find()and cache all results for future lookups.
- match(c: Pyjoyment_Controller) None[source]¶
r.match(Pyjoyment.Controller.new())
Match routes with
Pyjoyment.Routes.Match.
- namespaces: list[str]¶
namespaces = r.namespaces r.namespaces = ['MyApp.Controller', 'MyApp']
Namespaces to load controllers from.
# Add another namespace to load controllers from r.namespaces.append('MyApp.MyController')
- proceed(c: Pyjoyment_Controller) None[source]¶
r.proceed(Pyjoyment.Controller.new())
Continue dispatch chain and emit the hook
around_actionofPyjoymentfor every action.continue_is an alias, the name of Mojolicious with an underscore.
- shortcuts: dict[str, Callable[[...], Any]]¶
shortcuts = r.shortcuts r.shortcuts = {'foo': lambda route, *args: ...}
Contains all available shortcuts.
- types: dict[str, Constraint]¶
types = r.types r.types = {'lower': re.compile(r'[a-z]+')}
Registered placeholder types, by default only
numis already defined.