Skip to content

Routing Reference

view.routing

P = ParamSpec('P') module-attribute

PART = re.compile('{(((\\w+)(: *(\\w+)))|(\\w+))}') module-attribute

Path: TypeAlias = Callable[[RouteOrCallable[P]], Route[P]] module-attribute

RouteData = Literal[1, 2] module-attribute

RouteOrCallable: TypeAlias = Union[Route[P], ViewRoute[P]] module-attribute

RouteOrWebsocket: TypeAlias = Union[Route[P], WebSocketRoute[P]] module-attribute

V = TypeVar('V', bound='ValueType') module-attribute

__all__ = ('get', 'post', 'put', 'patch', 'delete', 'options', 'query', 'body', 'route_types', 'BodyParam', 'context', 'route', 'websocket') module-attribute

BodyParam dataclass

Bases: Generic[V]

default: V instance-attribute

types: type[V] | list[type[V]] | tuple[type[V], ...] instance-attribute

__init__(types: type[V] | list[type[V]] | tuple[type[V], ...], default: V) -> None

Method

Bases: Enum

DELETE = 3 class-attribute instance-attribute

GET = 1 class-attribute instance-attribute

OPTIONS = 6 class-attribute instance-attribute

PATCH = 4 class-attribute instance-attribute

POST = 2 class-attribute instance-attribute

PUT = 5 class-attribute instance-attribute

WEBSOCKET = 7 class-attribute instance-attribute

Part dataclass

Bases: Generic[V]

name: str instance-attribute

type: type[V] | None instance-attribute

__init__(name: str, type: type[V] | None) -> None

Route dataclass

Bases: Generic[P], LoadChecker

Standard Route Wrapper

__str__ = __repr__ class-attribute instance-attribute

app: App | None = None class-attribute instance-attribute

cache_rate: int = -1 class-attribute instance-attribute

doc: str | None = None class-attribute instance-attribute

errors: dict[int, ViewRoute] | None = None class-attribute instance-attribute

extra_types: dict[str, Any] = field(default_factory=dict) class-attribute instance-attribute

func: ViewRoute[P] instance-attribute

inputs: list[RouteInput | RouteData] instance-attribute

method: Method | None instance-attribute

method_list: list[Method] | None = None class-attribute instance-attribute

middleware_funcs: list[Middleware[P]] = field(default_factory=list) class-attribute instance-attribute

parallel_build: bool | None = None class-attribute instance-attribute

parts: list[str | Part[Any]] = field(default_factory=list) class-attribute instance-attribute

path: str | None instance-attribute

steps: Iterable[str] | None = None class-attribute instance-attribute

__call__(*args: P.args, **kwargs: P.kwargs) -> ViewResult async

__init__(func: ViewRoute[P], path: str | None, method: Method | None, inputs: list[RouteInput | RouteData], doc: str | None = None, cache_rate: int = -1, errors: dict[int, ViewRoute] | None = None, extra_types: dict[str, Any] = dict(), parts: list[str | Part[Any]] = list(), middleware_funcs: list[Middleware[P]] = list(), method_list: list[Method] | None = None, steps: Iterable[str] | None = None, parallel_build: bool | None = None, app: App | None = None, __str__=__repr__) -> None

__repr__()

error(status_code: int)

middleware(func_or_none: Middleware[P] | None = None)

Define a middleware function for the route.

RouteInput dataclass

Bases: Generic[V]

default: V | None | _NoDefaultType instance-attribute

doc: str | None instance-attribute

is_body: bool instance-attribute

name: str instance-attribute

tp: tuple[type[V], ...] instance-attribute

validators: list[Validator[V]] instance-attribute

__init__(name: str, is_body: bool, tp: tuple[type[V], ...], default: V | None | _NoDefaultType, doc: str | None, validators: list[Validator[V]]) -> None

body(name: str, *tps: type[V], doc: str | None = None, default: V | None | _NoDefaultType = _NoDefault) -> Path[P]

Add a route input for a body parameter.

Parameters:

Name Type Description Default
name str

The name of the parameter to read when the body is received.

required
tps type[V]

All the possible types that are allowed to be used. If none are specified, the type is Any.

()
doc str | None

Description of this parameter.

None
default V | None | _NoDefaultType

The default value to use if the key was not received.

_NoDefault
Example
from view import new_app, body

app = new_app()

@app.get("/")
@body("greeting", str, doc="The greeting to use.", default="hello")
def index(greeting: str):
    return f"{greeting}, world!"

app.run()

context(r_or_none: RouteOrCallable[P] | None = None) -> Callable[[RouteOrCallable[P]], Route[P]] | Route[P]

Add a context input to the route.

delete(path_or_route: str | None | RouteOrCallable[P] = None, doc: str | None = None, *, cache_rate: int = -1, steps: Iterable[str] | None = None, parallel_build: bool | None = _DefinedByConfig) -> Path[P]

Add a DELETE route.

Parameters:

Name Type Description Default
path_or_route str | None | RouteOrCallable[P]

The path to this route, or the route itself.

None
doc str | None

The description of the route to be used in documentation.

None
cache_rate int

Reload the cache for this route every x number of requests. -1 means to never cache.

-1
steps Iterable[str] | None

Build steps to run before this route is executed.

None
parallel_build bool | None

Whether to run the build steps in parallel. If this is not specified, this is defined by the app configuration.

_DefinedByConfig
Example
from view import delete

@delete("/")
async def index():
    return "Hello, view.py!"

get(path_or_route: str | None | RouteOrCallable[P] = None, doc: str | None = None, *, cache_rate: int = -1, steps: Iterable[str] | None = None, parallel_build: bool | None = _DefinedByConfig) -> Path[P]

Add a GET route.

Parameters:

Name Type Description Default
path_or_route str | None | RouteOrCallable[P]

The path to this route, or the route itself.

None
doc str | None

The description of the route to be used in documentation.

None
cache_rate int

Reload the cache for this route every x number of requests. -1 means to never cache.

-1
steps Iterable[str] | None

Build steps to run before this route is executed.

None
parallel_build bool | None

Whether to run the build steps in parallel. If this is not specified, this is defined by the app configuration.

_DefinedByConfig
Example
from view import get

@get("/")
async def index():
    return "Hello, view.py!"

options(path_or_route: str | None | RouteOrCallable[P] = None, doc: str | None = None, *, cache_rate: int = -1, steps: Iterable[str] | None = None, parallel_build: bool | None = _DefinedByConfig) -> Path[P]

Add an OPTIONS route.

Parameters:

Name Type Description Default
path_or_route str | None | RouteOrCallable[P]

The path to this route, or the route itself.

None
doc str | None

The description of the route to be used in documentation.

None
cache_rate int

Reload the cache for this route every x number of requests. -1 means to never cache.

-1
steps Iterable[str] | None

Build steps to run before this route is executed.

None
parallel_build bool | None

Whether to run the build steps in parallel. If this is not specified, this is defined by the app configuration.

_DefinedByConfig
Example
from view import options

@options("/")
async def index():
    return "Hello, view.py!"

patch(path_or_route: str | None | RouteOrCallable[P] = None, doc: str | None = None, *, cache_rate: int = -1, steps: Iterable[str] | None = None, parallel_build: bool | None = _DefinedByConfig) -> Path[P]

Add a PATCH route.

Parameters:

Name Type Description Default
path_or_route str | None | RouteOrCallable[P]

The path to this route, or the route itself.

None
doc str | None

The description of the route to be used in documentation.

None
cache_rate int

Reload the cache for this route every x number of requests. -1 means to never cache.

-1
steps Iterable[str] | None

Build steps to run before this route is executed.

None
parallel_build bool | None

Whether to run the build steps in parallel. If this is not specified, this is defined by the app configuration.

_DefinedByConfig
Example
from view import patch

@patch("/")
async def index():
    return "Hello, view.py!"

post(path_or_route: str | None | RouteOrCallable[P] = None, doc: str | None = None, *, cache_rate: int = -1, steps: Iterable[str] | None = None, parallel_build: bool | None = _DefinedByConfig) -> Path[P]

Add a POST route.

Parameters:

Name Type Description Default
path_or_route str | None | RouteOrCallable[P]

The path to this route, or the route itself.

None
doc str | None

The description of the route to be used in documentation.

None
cache_rate int

Reload the cache for this route every x number of requests. -1 means to never cache.

-1
steps Iterable[str] | None

Build steps to run before this route is executed.

None
parallel_build bool | None

Whether to run the build steps in parallel. If this is not specified, this is defined by the app configuration.

_DefinedByConfig
Example
from view import post

@post("/")
async def index():
    return "Hello, view.py!"

put(path_or_route: str | None | RouteOrCallable[P] = None, doc: str | None = None, *, cache_rate: int = -1, steps: Iterable[str] | None = None, parallel_build: bool | None = _DefinedByConfig) -> Path[P]

Add a PUT route.

Parameters:

Name Type Description Default
path_or_route str | None | RouteOrCallable[P]

The path to this route, or the route itself.

None
doc str | None

The description of the route to be used in documentation.

None
cache_rate int

Reload the cache for this route every x number of requests. -1 means to never cache.

-1
steps Iterable[str] | None

Build steps to run before this route is executed.

None
parallel_build bool | None

Whether to run the build steps in parallel. If this is not specified, this is defined by the app configuration.

_DefinedByConfig
Example
from view import put

@put("/")
async def index():
    return "Hello, view.py!"

query(name: str, *tps: type[V], doc: str | None = None, default: V | None | _NoDefaultType = _NoDefault) -> Path[P]

Add a route input for a query parameter.

Parameters:

Name Type Description Default
name str

The name of the parameter to read when the query string is received.

required
tps type[V]

All the possible types that are allowed to be used. If none are specified, the type is Any.

()
doc str | None

Description of this parameter.

None
default V | None | _NoDefaultType

The default value to use if the key was not received.

_NoDefault
Example
from view import new_app, query

app = new_app()

@app.get("/")
@query("greeting", str, doc="The greeting to use.", default="hello")
def index(greeting: str):
    return f"{greeting}, world!"

app.run()

route(path_or_route: str | None | RouteOrCallable[P] = None, doc: str | None = None, *, cache_rate: int = -1, methods: Iterable[StrMethod] | None = None, steps: Iterable[str] | None = None, parallel_build: bool | None = _DefinedByConfig) -> Path[P]

Add a route that can be called with any method (or only specific methods).

Parameters:

Name Type Description Default
path_or_route str | None | RouteOrCallable[P]

The path to this route, or the route itself.

None
doc str | None

The description of the route to be used in documentation.

None
cache_rate int

Reload the cache for this route every x number of requests. -1 means to never cache.

-1
methods Iterable[StrMethod] | None

Methods that can be used to access this route. If this is None, then all methods are allowed.

None
Example
from view import route

@route("/", methods=("GET", "POST"))
async def index():
    return "Hello, view.py!"

route_types(r: RouteOrCallable[P], data: type[Any] | tuple[type[Any]] | dict[str, Any]) -> Route

websocket(path_or_route: str | None | RouteOrCallable[P] = None, doc: str | None = None, *, steps: Iterable[str] | None = None, parallel_build: bool | None = _DefinedByConfig) -> Callable[[RouteOrWebsocket[P]], Route[P]]

Add a websocket route.

wrap_step(app: App, step: str) -> None async

view._loader

TYPECODE_ANY = 0 module-attribute

TYPECODE_BOOL = 3 module-attribute

TYPECODE_CLASS = 7 module-attribute

TYPECODE_CLASSTYPES = 8 module-attribute

TYPECODE_DICT = 5 module-attribute

TYPECODE_FLOAT = 4 module-attribute

TYPECODE_INT = 2 module-attribute

TYPECODE_LIST = 9 module-attribute

TYPECODE_NONE = 6 module-attribute

TYPECODE_STR = 1 module-attribute

__all__ = ('load_fs', 'load_simple', 'finalize') module-attribute

LoaderDoc dataclass

default: Any instance-attribute

desc: str instance-attribute

tp: Any instance-attribute

__init__(desc: str, tp: Any, default: Any) -> None

finalize(routes: list[Route], app: ViewApp)

Attach list of routes to an app and validate all parameters.

Parameters:

Name Type Description Default
routes list[Route]

List of routes.

required
app App

App to attach to.

required

load_fs(app: ViewApp, target_dir: Path) -> None

Filesystem loading implementation. Similiar to NextJS's routing system. You take target_dir and search it, if a file is found and not prefixed with _, then convert the directory structure to a path. For example, target_dir/hello/world/index.py would be converted to a route for /hello/world

Parameters:

Name Type Description Default
app App

App to attach routes to.

required
target_dir Path

Directory to search for routes.

required

load_patterns(app: ViewApp, target_path: Path) -> None

load_simple(app: ViewApp, target_dir: Path) -> None

Simple loading implementation. Simple loading is essentially searching a directory recursively for files, and then extracting Route instances from each file.

If a file is prefixed with _, it will not be loaded.

Parameters:

Name Type Description Default
app App

App to attach routes to.

required
target_dir Path

Directory to search for routes.

required