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:

get_array_canvas_class #

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

Return ArrayCanvas class for current canvas backend.

Source code in ndv/views/_app.py
558
559
560
561
562
563
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 ndv/views/_app.py
553
554
555
def get_array_view_class() -> type[ArrayView]:
    """Return [`ArrayView`][ndv.views.bases.ArrayView] class for current GUI frontend."""  # noqa: E501
    return GUI_PROVIDERS[gui_frontend()].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 ndv/views/_app.py
566
567
568
569
570
571
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()

gui_frontend cached #

gui_frontend() -> GuiFrontend

Return the active GuiFrontend.

This is determined first by the NDV_GUI_FRONTEND environment variable, after which known GUI providers are tried in order until one is found that is either already running, or available.

Source code in ndv/views/_app.py
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
@cache  # not allowed to change
def gui_frontend() -> GuiFrontend:
    """Return the active [`GuiFrontend`][ndv.views.GuiFrontend].

    This is determined first by the `NDV_GUI_FRONTEND` environment variable, after which
    known GUI providers are tried in order until one is found that is either already
    running, or available.
    """
    requested = os.getenv(GUI_ENV_VAR, "").lower()
    valid = {x.value for x in GuiFrontend}
    if requested:
        if requested not in valid:
            raise ValueError(
                f"Invalid GUI frontend: {requested!r}. Valid options: {valid}"
            )
        key = GuiFrontend(requested)
        # ensure the app is created for explicitly requested frontends
        provider = GUI_PROVIDERS[key]
        if not provider.is_running():
            provider.create_app()
        return key

    for key, provider in GUI_PROVIDERS.items():
        if provider.is_running():
            return key

    errors: list[tuple[GuiFrontend, BaseException]] = []
    for key, provider in GUI_PROVIDERS.items():
        try:
            provider.create_app()
            return key
        except Exception as e:
            errors.append((key, e))

    raise RuntimeError(  # pragma: no cover
        f"Could not find an appropriate GUI frontend: {valid!r}. Tried:\n\n"
        + "\n".join(f"- {key.value}: {err}" for key, err in errors)
    )

run_app #

run_app() -> None

Start the active GUI application event loop.

Source code in ndv/views/_app.py
592
593
594
def run_app() -> None:
    """Start the active GUI application event loop."""
    GUI_PROVIDERS[gui_frontend()].exec()