Module h2o_wave.routing

Functions

handle_on

async def handle_on(q: Query) ‑> bool

Handle the query using a query handler (a function annotated with @on()).

Args
q
The query context.
Returns

True if a matching query handler was found and invoked, else False.

on

def on(arg: str = None, predicate: Optional[Callable] = None)

Indicate that a function is a query handler that should be invoked when q.args contains an argument that matches a specific name or pattern or value.

Examples: A function annotated with @on('foo') is invoked whenever q.args['foo'] is found and the value is truthy. A function annotated with @on('foo', lambda x: x is False) is invoked whenever q.args['foo'] is False. A function annotated with @on('foo', lambda x: isinstance(x, bool) is invoked whenever q.args['foo'] is True or False. A function annotated with @on('foo', lambda x: 42 <= x <= 420) is invoked whenever q.args['foo'] between 42 and 420. A function annotated with @on('#foo') is invoked whenever q.args['#'] equals 'foo'. A function annotated with @on('#foo/bar') is invoked whenever q.args['#'] equals 'foo/bar'. A function annotated with @on('#foo/&lcub;fruit&rcub;') is invoked whenever q.args['#'] matches 'foo/apple', 'foo/orange', etc. The parameter 'fruit' is passed to the function (in this case, 'apple', 'orange', etc.)

Parameters in patterns (indicated within curly braces) can be converted to str, int, float or uuid.UUID instances by suffixing the parameters with str, int, float or uuid, respectively.

Examples: - user_id:int: user_id is converted to an integer. - amount:float: amount is converted to a float. - id:uuid: id is converted to a uuid.UUID.

Args
arg
The name of the q.arg argument (in case of plain arguments) or a pattern (in case of hash arguments, or q.args['#']). If not provided, the q.arg argument is assumed to be the same as the name of the function.
predicate
A function (or lambda) to test the value of the argument. If provided, the query handler is invoked if the function returns a truthy value.