Skip to content

ndv.views #

Wrappers around GUI & graphics frameworks.

Most stuff in this module is not intended for public use, but ndv.views.bases shows the protocol that GUI & graphics classes should implement.

Modules:

  • bases

    Abstract base classes for views and viewable objects.

Classes:

Functions:

CanvasBackend #

Bases: str, Enum

Enum of available canvas backends.

Attributes:

GuiFrontend #

Bases: str, Enum

Enum of available GUI frontends.

Attributes:

call_later #

call_later(msec: int, func: Callable[[], None]) -> None

Call func after msec milliseconds.

This can be used to enqueue a function to be called after the current event loop iteration. For example, before calling run_app(), to ensure that the event loop is running before the function is called.

Parameters:

  • msec #

    (int) –

    The number of milliseconds to wait before calling func.

  • func #

    (Callable[[], None]) –

    The function to call.

Source code in src/ndv/views/_app.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
def call_later(msec: int, func: Callable[[], None]) -> None:
    """Call `func` after `msec` milliseconds.

    This can be used to enqueue a function to be called after the current event loop
    iteration.  For example, before calling `run_app()`, to ensure that the event
    loop is running before the function is called.

    Parameters
    ----------
    msec : int
        The number of milliseconds to wait before calling `func`.
    func : Callable[[], None]
        The function to call.
    """
    ndv_app().call_later(msec, func)

get_array_canvas_class #

get_array_canvas_class(
    backend: str | None = None,
) -> type[ArrayCanvas]

Return ArrayCanvas class for current canvas backend.

Source code in src/ndv/views/_app.py
291
292
293
294
295
296
def get_array_canvas_class(backend: str | None = None) -> type[ArrayCanvas]:
    """Return [`ArrayCanvas`][ndv.views.bases.ArrayCanvas] class for current canvas backend."""  # noqa: E501
    _backend = canvas_backend(backend)
    if _backend not in CANVAS_PROVIDERS:  # pragma: no cover
        raise NotImplementedError(f"No canvas backend found for {_backend}")
    return CANVAS_PROVIDERS[_backend].array_canvas_class()

get_array_view_class #

get_array_view_class() -> type[ArrayView]

Return ArrayView class for current GUI frontend.

Source code in src/ndv/views/_app.py
286
287
288
def get_array_view_class() -> type[ArrayView]:
    """Return [`ArrayView`][ndv.views.bases.ArrayView] class for current GUI frontend."""  # noqa: E501
    return ndv_app().array_view_class()

get_histogram_canvas_class #

get_histogram_canvas_class(
    backend: str | None = None,
) -> type[HistogramCanvas]

Return HistogramCanvas class for current canvas backend.

Source code in src/ndv/views/_app.py
299
300
301
302
303
304
def get_histogram_canvas_class(backend: str | None = None) -> type[HistogramCanvas]:
    """Return [`HistogramCanvas`][ndv.views.bases.HistogramCanvas] class for current canvas backend."""  # noqa: E501
    _backend = canvas_backend(backend)
    if _backend not in CANVAS_PROVIDERS:  # pragma: no cover
        raise NotImplementedError(f"No canvas backend found for {_backend}")
    return CANVAS_PROVIDERS[_backend].histogram_canvas_class()

process_events #

process_events() -> None

Force processing of events for the application.

Source code in src/ndv/views/_app.py
342
343
344
def process_events() -> None:
    """Force processing of events for the application."""
    ndv_app().process_events()

run_app #

run_app() -> None

Start the active GUI application event loop.

Source code in src/ndv/views/_app.py
347
348
349
def run_app() -> None:
    """Start the active GUI application event loop."""
    ndv_app().run()

set_canvas_backend #

set_canvas_backend(
    backend: Literal["pygfx", "vispy"] | None = None,
) -> None

Sets the preferred canvas backend. Cannot be set after the GUI is running.

Source code in src/ndv/views/_app.py
227
228
229
230
231
232
233
234
def set_canvas_backend(backend: Literal["pygfx", "vispy"] | None = None) -> None:
    """Sets the preferred canvas backend. Cannot be set after the GUI is running."""
    if _APP:
        raise RuntimeError("Cannot change the backend once the app is running")
    if backend is None:
        os.environ.pop(CANVAS_ENV_VAR)
    else:
        os.environ[CANVAS_ENV_VAR] = CanvasBackend(backend).value  # validate

set_gui_backend #

set_gui_backend(
    backend: Literal["jupyter", "qt", "wx"] | None = None,
) -> None

Sets the preferred GUI backend. Cannot be set after the GUI is running.

Source code in src/ndv/views/_app.py
237
238
239
240
241
242
243
244
def set_gui_backend(backend: Literal["jupyter", "qt", "wx"] | None = None) -> None:
    """Sets the preferred GUI backend. Cannot be set after the GUI is running."""
    if _APP:
        raise RuntimeError("Cannot change the backend once the app is running")
    if backend is None:
        os.environ.pop(GUI_ENV_VAR)
    else:
        os.environ[GUI_ENV_VAR] = GuiFrontend(backend).value  # validate