differt.plotting.dispatch

Contents

differt.plotting.dispatch#

dispatch(fun)[source]#

Transform a function into a backend dispatcher for plot functions.

Parameters:

fun (Callable[[ParamSpec(P, bound= None)], TypeVar(T, SceneCanvas, Figure, Figure)]) – The callable that will register future dispatch functions for each backend implementation.

Return type:

_Dispatcher[ParamSpec(P, bound= None), TypeVar(T, SceneCanvas, Figure, Figure)]

Returns:

A callable that can register backend implementations with register.

Notes

Only the functions registered with register will be called. The fun argument wrapped inside dispatch is only used for documentation, but never called.

Examples

The following example shows how one can implement plotting utilities on different backends for a given plot.

>>> import differt.plotting as dplt
>>>
>>> @dplt.dispatch
... def plot_line(vertices, color):
...     pass
>>>
>>> @plot_line.register("matplotlib")
... def _(vertices, color):
...     print("Using matplotlib backend")
>>>
>>> @plot_line.register("plotly")
... def _(vertices, color):
...     print("Using plotly backend")
>>>
>>> plot_line(
...     _,
...     _,
...     backend="matplotlib",
... )
Using matplotlib backend
>>>
>>> plot_line(
...     _,
...     _,
...     backend="plotly",
... )
Using plotly backend
>>>
>>> plot_line(
...     _,
...     _,
...     backend="vispy",
... )
Traceback (most recent call last):
NotImplementedError: No backend implementation for 'vispy'
>>>
>>> # The default backend is VisPy so unimplemented too.
>>> plot_line(_, _)
Traceback (most recent call last):
NotImplementedError: No backend implementation for 'vispy'
>>>
>>> @plot_line.register("numpy")
... def _(vertices, color):
...     pass
Traceback (most recent call last):
ValueError: Unsupported backend 'numpy', allowed values are: ...