Registry / web-framework / sanic-routing

sanic-routing

JSON →
library23.12.0pypypi✓ verified 22d ago

Sanic-Routing is the standalone, high-performance core routing component used by the Sanic web framework. It handles URL parsing, route matching, and parameter extraction, providing a flexible and efficient routing engine. The library maintains an active development pace with frequent updates, typically releasing new versions quarterly.

pip install sanic-routing
INSTALL
IMPORT
SIG · SANIC-ROUTING
S
sanic-routing
web-frameworkpythonv23.12.0
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v23.12.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Router
from sanic_routing import Router
from sanic_routing import Router
Route
from sanic_routing import Route
BaseRouter
from sanic_routing import BaseRouter

This quickstart demonstrates how to initialize the `Router`, add various types of routes (simple, string parameter, path parameter), and then perform route lookups using `router.get()`. It also shows how to handle `NotFound` exceptions for non-existent routes.

from sanic_routing import Router from sanic_routing.exceptions import NotFound # Initialize the router router = Router() # Define dummy handlers (in a real app, these would be your view functions) def handler_root(request): pass def handler_greet(request, name): pass # Add routes to the router router.add("/", handler_root, methods=["GET"]) router.add("/greet/<name:str>", handler_greet, methods=["GET"]) router.add("/files/<path:path>", lambda req, path: None, methods=["GET"]) # Look up a route by URI and method try: # Matching a simple route handler, params, uri, kwargs, route = router.get("/greet/Alice", "GET") print(f"Found handler for /greet/Alice: {handler.__name__}") print(f"Parameters: {params}") # Expected: {'name': 'Alice'} # Matching a path parameter handler_path, params_path, uri_path, kwargs_path, route_path = router.get("/files/path/to/document.txt", "GET") print(f"Found handler for /files/path/to/document.txt: {params_path}") # Expected: {'path': 'path/to/document.txt'} except NotFound as e: print(f"Could not find route: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") # Example of a missing route try: router.get("/missing", "GET") except NotFound as e: print(f"Attempted to get /missing, caught expected error: {type(e).__name__}")
Debug
Known issues
breakingRoute parameters defined with `<name:str>` or `<name:path>` types will no longer match empty strings. Previously, a route like `/users/<user_id:str>` might have matched `/users/` resulting in `user_id=''`.
fix
Ensure your routes explicitly handle empty string segments if that was desired behavior, or update path definitions to be more specific (e.g., using `/<user_id:uuid>` for UUIDs) and avoid matching empty string segments.
affects: >=22.3.0
deprecatedThe route parameter types `<foo:string>` and `<foo:number>` were deprecated. They have been replaced with `<foo:str>` and `<foo:float>` respectively.
fix
Update route definitions to use the new parameter types: change `<foo:string>` to `<foo:str>` and `<foo:number>` to `<foo:float>`.
affects: >=0.7.0
gotchaStarting with version 23.6.0, `sanic-routing` will only unquote URI components when casting them to string types. This means that if you're not explicitly casting a parameter, it might retain its URL-encoded state (e.g., `%20` for space).
fix
Be mindful of how URI parameters are handled and explicitly decode them if necessary, particularly if you rely on parameters always being fully unquoted before processing. For standard string casts via `<param:str>`, unquoting should still occur.
affects: >=23.6.0
Upgrade
Version history
23.12.0latest on PyPI · released Dec 31, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
32 hits · last 30 days
node
26
OpenAI (training)
1
Resources
sanic-routing — pip install sanic-routing · libregistry