Skip to content

App Reference

view.app

A = TypeVar('A') module-attribute

B = TypeVar('B', bound=BaseException) module-attribute

CustomLoader: TypeAlias = Callable[['App', Path], Iterable[Route]] module-attribute

ERROR_CODES: tuple[int, ...] = (400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511) module-attribute

P = ParamSpec('P') module-attribute

S = TypeVar('S', int, str, dict, bool) module-attribute

T = TypeVar('T') module-attribute

__all__ = ('App', 'new_app', 'get_app', 'Error', 'ERROR_CODES') module-attribute

App

Bases: ViewApp

Public view.py app object.

config = config instance-attribute

loaded: bool = False instance-attribute

loaded_routes: list[Route] = [] instance-attribute

routes: list[Route] = [] instance-attribute

running = False instance-attribute

start = run class-attribute instance-attribute

templaters: dict[str, Any] = {} instance-attribute

__init__(config: Config, *, error_class: type[Error] = Error) -> None

Parameters:

Name Type Description Default
config Config

Configuration object to be used. Automatically generated by new_app.

required

__repr__() -> str

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

Set a body parameter.

Parameters:

Name Type Description Default
name str

Name of the parameter.

required
tps type[V]

Types that can be passed to the server. If empty, any is used.

()
doc str | None

Description of this body parameter.

None
default V | None | _NoDefaultType

Default value to be used if not supplied.

_NoDefault

build() -> None async

Run the default build steps for the app.

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

critical(*messages: object, **kwargs: Unpack[_LogArgs]) -> None

custom_loader(loader: CustomLoader)

debug(*messages: object, **kwargs: Unpack[_LogArgs]) -> None

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

Add a DELETE route.

Parameters:

Name Type Description Default
path str

The path to this route.

required
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
Example
from view import new_app

app = new_app()

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

app.run()

docs(file: str | TextIO | Path | None = None, *, encoding: str = 'utf-8', overwrite: bool = True) -> str | None

Generate documentation for the app.

error(*messages: object, **kwargs: Unpack[_LogArgs]) -> None

export(path: str | Path | None = None) -> None async

Export the app as static HTML.

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

Add a GET route.

Parameters:

Name Type Description Default
path str

The path to this route.

required
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
Example
from view import new_app

app = new_app()

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

app.run()

info(*messages: object, **kwargs: Unpack[_LogArgs]) -> None

load(*routes: Route) -> None

Load the app. This is automatically called most of the time and should only be called manually during manual loading.

Parameters:

Name Type Description Default
routes Route

Routes to load into the app.

()

markdown(name: str | Path, *, directory: str | Path | None = _ConfigSpecified) -> HTML async

Convert a markdown file into HTML. This returns a view.py HTML response.

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

Add an OPTIONS route.

Parameters:

Name Type Description Default
path str

The path to this route.

required
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
Example
from view import new_app

app = new_app()

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

app.run()

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

Add a PATCH route.

Parameters:

Name Type Description Default
path str

The path to this route.

required
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
Example
from view import new_app

app = new_app()

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

app.run()

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

Add a POST route.

Parameters:

Name Type Description Default
path str

The path to this route.

required
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
Example
from view import new_app

app = new_app()

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

app.run()

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

Add a PUT route.

Parameters:

Name Type Description Default
path str

The path to this route.

required
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
Example
from view import new_app

app = new_app()

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

app.run()

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

Set a query parameter.

Parameters:

Name Type Description Default
name str

Name of the parameter.

required
tps type[V]

Types that can be passed to the server. If empty, any is used.

()
doc str | None

Description of this query parameter.

None
default V | None | _NoDefaultType

Default value to be used if not supplied.

_NoDefault

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) -> _RouteDeco[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!"

run(*, fancy: bool | None = None) -> None

Run the app.

run_async(loop: asyncio.AbstractEventLoop | None = None) -> None

Run the app in an event loop.

run_task(loop: asyncio.AbstractEventLoop | None = None) -> asyncio.Task[None]

Run the app as a task.

run_threaded(*, daemon: bool = True) -> Thread

Run the app in a thread.

template(name: str | Path, directory: str | Path | None = _ConfigSpecified, engine: TemplateEngine | None = _ConfigSpecified, frame: Frame | None | _CurrentFrameType = _CurrentFrame, **parameters: Any) -> HTML async

Render a template with the specified engine. This returns a view.py HTML response.

test() async

Open the testing context.

warning(*messages: object, **kwargs: Unpack[_LogArgs]) -> None

websocket(path: str, doc: str | None = None) -> Callable[[RouteOrWebsocket[P]], Route[P]]

Error

Bases: BaseException

Base class to act as a transport for raising HTTP exceptions.

message = message instance-attribute

status = status instance-attribute

__init__(status: int = 400, message: str | None = None) -> None

Parameters:

Name Type Description Default
status int

The status code for the resulting response.

400
message str | None

The (optional) message to send back to the client. If none, uses the default error message (e.g. Bad Request for status 400).

None

InputDoc dataclass

Bases: Generic[T]

default: T | _NoDefaultType instance-attribute

desc: str instance-attribute

type: tuple[type[T], ...] instance-attribute

__init__(desc: str, type: tuple[type[T], ...], default: T | _NoDefaultType) -> None

RouteDoc dataclass

body: dict[str, InputDoc] instance-attribute

desc: str instance-attribute

query: dict[str, InputDoc] instance-attribute

__init__(desc: str, body: dict[str, InputDoc], query: dict[str, InputDoc]) -> None

TestingContext

app = app instance-attribute

__init__(app: Callable[[Any, Any, Any], Any]) -> None

delete(route: str, *, body: dict[str, Any] | None = None, query: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> TestingResponse async

get(route: str, *, body: dict[str, Any] | None = None, query: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> TestingResponse async

options(route: str, *, body: dict[str, Any] | None = None, query: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> TestingResponse async

patch(route: str, *, body: dict[str, Any] | None = None, query: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> TestingResponse async

post(route: str, *, body: dict[str, Any] | None = None, query: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> TestingResponse async

put(route: str, *, body: dict[str, Any] | None = None, query: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> TestingResponse async

start() -> None async

stop() -> None async

websocket(route: str, *, query: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> AsyncIterator[VirtualWebSocket] async

TestingResponse

content = content instance-attribute

headers = headers instance-attribute

message: str property

status = status instance-attribute

__init__(message: str | None, headers: dict[str, str], status: int, content: bytes) -> None

VirtualWebSocket

recv_queue = Queue() instance-attribute

send_queue = Queue() instance-attribute

__init__() -> None

close() async

handshake() -> None async

receive() -> str async

send(message: str) -> None async

get_app(*, address: int | None = None) -> App

Get the last app created by new_app.

new_app(*, start: bool = False, config_path: Path | str | None = None, config_directory: Path | str | None = None, app_dealloc: Callback | None = None, store: bool = True, config: Config | None = None, error_class: type[Error] = Error) -> App

Create a new view app.

Parameters:

Name Type Description Default
start bool

Should the app be started automatically? (In a new thread)

False
config_path Path | str | None

Path of the target configuration file

None
config_directory Path | str | None

Directory path to search for a configuration

None
app_dealloc Callback | None

Callback to run when the App instance is freed from memory

None
store bool

Whether to store the app, to allow use from get_app()

True
config Config | None

Raw Config object to use instead of loading the config.

None
error_class type[Error]

Class to be recognized as the view.py HTTP error object.

Error