puepy.router
The puepy.router
module contains code relevant to optional client-side routing in PuePy.
See Also
PuePy's router functionality can be optionally installed by calling the install_router method of the Application class.
Example
Once installed, the Router
instance is available on app.Router
and can be used throughout the application to manage
client-side routing. Routes are defined by either using the @app.page
decorator or by calling methods manually on
the Router
instance.
Classes:
Name | Description |
---|---|
puepy.router.Route |
Represents a route in the router. |
puepy.router.Router |
Represents a router for managing client-side routing in a web application. |
Route
Represents a route in the router. A route is defined by a path match pattern, a page class, and a name.
Note
This is usually not instanciated directly. Instead, use the Router.add_route
method to create a new route or
use the @app.page decorator to define a route at the time you define your Pages.
Source code in puepy/router.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
__init__(path_match, page, name, base_path, router=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_match |
str
|
The path match pattern used for routing. |
required |
page |
Page
|
An instance of the Page class representing the page. |
required |
name |
str
|
The name of the page. |
required |
base_path |
str
|
The base path used for routing. |
required |
router |
Router
|
An optional parameter representing the router used for routing. |
None
|
Source code in puepy/router.py
match(path)
Evaluates a path against the route's pattern to determine if there is a match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
The path to be matched against the pattern. |
required |
Returns:
Type | Description |
---|---|
Match found (tuple): A tuple containing a True boolean value and a dictionary. The dictionary contains the |
|
matched variables extracted from the path. |
|
Match not found (tuple): If no match is found, returns |
Source code in puepy/router.py
reverse(**kwargs)
Reverse method is used to generate a URL path using the given parameters. It replaces the placeholders in the path template with the corresponding values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
A variable number of keyword arguments representing the values to be inserted into the path |
{}
|
Returns:
Type | Description |
---|---|
str
|
The generated URL path. |
Example
Let's say we have a path template /users/<username>/posts/<post_id>
. We can use the reverse method to
generate the URL path by providing the values for "username" and "post_id" as keyword arguments:
route.reverse(username="john", post_id=123)
=> "/users/john/posts/123"
Source code in puepy/router.py
Router
Class representing a router for managing client-side routing in a web application.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
application |
object
|
The web application object. Defaults to None. |
None
|
base_path |
str
|
The base path URL. Defaults to None. |
None
|
link_mode |
str
|
The link mode for navigating. Defaults to "hash". |
LINK_MODE_HASH
|
Attributes:
Name | Type | Description |
---|---|---|
LINK_MODE_DIRECT |
str
|
Direct link mode. |
LINK_MODE_HTML5 |
str
|
HTML5 link mode. |
LINK_MODE_HASH |
str
|
Hash link mode. |
routes |
list
|
List of Route instances. |
routes_by_name |
dict
|
Dictionary mapping route names to Route instances. |
routes_by_page |
dict
|
Dictionary mapping page classes to Route instances. |
application |
object
|
The web application object. |
base_path |
str
|
The base path URL. |
link_mode |
str
|
The link mode for navigating. |
Source code in puepy/router.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
__init__(application=None, base_path=None, link_mode=LINK_MODE_HASH)
Initializes an instance of the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
application |
Application
|
The application used for routing. |
None
|
base_path |
str
|
The base path for the routes. |
None
|
link_mode |
str
|
The mode for generating links. |
LINK_MODE_HASH
|
Source code in puepy/router.py
add_route(path_match, page_class, name=None)
Adds a route to the router. This method creates a new Route instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_match |
str
|
The URL path pattern to match for the route. |
required |
page_class |
Page class
|
The class or function to be associated with the route. |
required |
name |
str
|
The name of the route. If not provided, the name will be derived from the page class name. |
None
|
Source code in puepy/router.py
add_route_instance(route)
Add a route instance to the current router.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
route |
Route
|
The route instance to be added. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the route instance or route name already exists in the router. |
Source code in puepy/router.py
match(path)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to be matched. |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple containing the matching route and the matched route arguments (if any). If no route is found, returns (None, None). |
Source code in puepy/router.py
navigate_to_path(path, **kwargs)
Navigates to the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str or Page
|
The path to navigate to. If path is a subclass of Page, it will be reversed using the reverse method |
required |
**kwargs |
Additional key-value pairs to be included in the query string. Each key-value pair will be |
{}
|
Raises:
Type | Description |
---|---|
Exception
|
If the link mode is invalid. |
Source code in puepy/router.py
reverse(destination, **kwargs)
Reverses a
Parameters:
Name | Type | Description | Default |
---|---|---|---|
destination |
The destination to reverse. It can be the name of a route, the mapped page of a route, or the default page of the application. |
required | |
**kwargs |
Additional keyword arguments to be passed to the reverse method of the destination route. |
{}
|
Returns:
Type | Description |
---|---|
str
|
The reversed URL for the given destination. |
Raises:
Type | Description |
---|---|
KeyError
|
If the destination is not found in the routes. |