From 695d1c05e94e12c205e340de55092f23f76146f8 Mon Sep 17 00:00:00 2001 From: Luis Ferraz Date: Tue, 14 Feb 2023 22:39:59 +0100 Subject: [PATCH] avoid installing dependencies for widgets --- limbus/widgets/types.py | 23 +++++++++++++++++++++-- setup.py | 8 +++++--- setup_dev_env.sh | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/limbus/widgets/types.py b/limbus/widgets/types.py index b6ffa59..fa86c9e 100644 --- a/limbus/widgets/types.py +++ b/limbus/widgets/types.py @@ -5,9 +5,15 @@ import logging import functools -import cv2 +try: + # NOTE: we import the cv2 & visdom modules here to avoid having it as a dependency + # for the whole project. + import cv2 + import visdom +except ImportError: + pass + import torch -import visdom import kornia import numpy as np @@ -163,6 +169,11 @@ class Visdom(Viz): def __init__(self) -> None: super().__init__() + try: + import visdom + except: + raise ImportError("To use Visdom as backend install the widgets extras: " + "pip install limbus[widgets]") self._vis: Optional[visdom.Visdom] = None self._try_init() @@ -305,6 +316,14 @@ def show_text(self, component: Component, title: str, text: str, append: bool = class OpenCV(Console): """Console visualization backend + openCV for images.""" + def __init__(self) -> None: + super().__init__() + try: + import cv2 + except: + raise ImportError("To use OpenCV as backend install the widgets extras: " + "pip install limbus[widgets]") + @is_enabled @set_title def show_image(self, component: Component, title: str, image: torch.Tensor): diff --git a/setup.py b/setup.py index 6bcddd4..baa6927 100644 --- a/setup.py +++ b/setup.py @@ -10,10 +10,8 @@ install_requires=[ 'torch', 'numpy', - 'visdom', 'typeguard', 'kornia', - 'opencv-python' ], extras_require={ 'dev': [ @@ -30,7 +28,11 @@ ], 'components': [ 'limbus-components' - ] + ], + 'widgets': [ + 'visdom', + 'opencv-python', + ] }, packages=find_packages(where='.'), package_dir={'': '.'}, diff --git a/setup_dev_env.sh b/setup_dev_env.sh index 210010d..de0199d 100755 --- a/setup_dev_env.sh +++ b/setup_dev_env.sh @@ -43,5 +43,5 @@ $conda_bin env create source $conda_bin_dir/activate $dev_env_dir/envs/limbus # install dev requirements -pip install -e .[dev,components] +pip install -e .[dev,components,widgets] # note that limbus-components is not installed in editable mode