From f2f2e8faa1bc15e0c2a56de5f09f16b87f7c3b0c Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Thu, 22 Jun 2023 18:21:14 -0400 Subject: [PATCH 1/6] conditional import of JupyterWgpuCanvas when checking toolbars --- fastplotlib/layouts/_gridplot.py | 16 +++++++++++++--- fastplotlib/plot.py | 19 +++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/fastplotlib/layouts/_gridplot.py b/fastplotlib/layouts/_gridplot.py index 8cf46d33e..2159bb20b 100644 --- a/fastplotlib/layouts/_gridplot.py +++ b/fastplotlib/layouts/_gridplot.py @@ -9,7 +9,13 @@ import pygfx from wgpu.gui.auto import WgpuCanvas -from wgpu.gui.jupyter import JupyterWgpuCanvas + +try: + from wgpu.gui.jupyter import JupyterWgpuCanvas +except (ImportError, ModuleNotFoundError): + HAS_JUPYTER_CANVAS = False +else: + HAS_JUPYTER_CANVAS = True from ipywidgets import HBox, Layout, Button, ToggleButton, VBox, Dropdown @@ -309,8 +315,12 @@ def show( _maintain_aspect = maintain_aspect subplot.auto_scale(maintain_aspect=_maintain_aspect, zoom=0.95) - # check if in jupyter notebook, or if toolbar is False - if (not isinstance(self.canvas, JupyterWgpuCanvas)) or (not toolbar): + # if jupyter_rfb is installed + if HAS_JUPYTER_CANVAS: + # check if in jupyter notebook, or if toolbar is False + if (not isinstance(self.canvas, JupyterWgpuCanvas)) or (not toolbar): + return self.canvas + else: return self.canvas if self.toolbar is None: diff --git a/fastplotlib/plot.py b/fastplotlib/plot.py index b239c0f46..b756bd9b7 100644 --- a/fastplotlib/plot.py +++ b/fastplotlib/plot.py @@ -3,7 +3,14 @@ from wgpu.gui.auto import WgpuCanvas from .layouts._subplot import Subplot from ipywidgets import HBox, Layout, Button, ToggleButton, VBox -from wgpu.gui.jupyter import JupyterWgpuCanvas + +try: + from wgpu.gui.jupyter import JupyterWgpuCanvas +except (ImportError, ModuleNotFoundError): + HAS_JUPYTER_CANVAS = False +else: + HAS_JUPYTER_CANVAS = True + from .layouts._record_mixin import RecordMixin from datetime import datetime import traceback @@ -139,9 +146,13 @@ def show( if autoscale: self.auto_scale(maintain_aspect=maintain_aspect, zoom=0.95) - # check if in jupyter notebook, or if toolbar is False - if (not isinstance(self.canvas, JupyterWgpuCanvas)) or (not toolbar): - return self.canvas + # if jupyter_rfb is installed + if HAS_JUPYTER_CANVAS: + # check if in jupyter notebook, or if toolbar is False + if (not isinstance(self.canvas, JupyterWgpuCanvas)) or (not toolbar): + return self.canvas + else: + return self.canvas if self.toolbar is None: self.toolbar = ToolBar(self) From 6a32800f90afaabaf0c4523528711f26b7a875ff Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Thu, 22 Jun 2023 18:25:20 -0400 Subject: [PATCH 2/6] better way than conditional imports --- fastplotlib/layouts/_gridplot.py | 15 ++------------- fastplotlib/plot.py | 18 +++--------------- 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/fastplotlib/layouts/_gridplot.py b/fastplotlib/layouts/_gridplot.py index 2159bb20b..69ed90203 100644 --- a/fastplotlib/layouts/_gridplot.py +++ b/fastplotlib/layouts/_gridplot.py @@ -10,13 +10,6 @@ from wgpu.gui.auto import WgpuCanvas -try: - from wgpu.gui.jupyter import JupyterWgpuCanvas -except (ImportError, ModuleNotFoundError): - HAS_JUPYTER_CANVAS = False -else: - HAS_JUPYTER_CANVAS = True - from ipywidgets import HBox, Layout, Button, ToggleButton, VBox, Dropdown from ._utils import make_canvas_and_renderer @@ -315,12 +308,8 @@ def show( _maintain_aspect = maintain_aspect subplot.auto_scale(maintain_aspect=_maintain_aspect, zoom=0.95) - # if jupyter_rfb is installed - if HAS_JUPYTER_CANVAS: - # check if in jupyter notebook, or if toolbar is False - if (not isinstance(self.canvas, JupyterWgpuCanvas)) or (not toolbar): - return self.canvas - else: + # check if in jupyter notebook, or if toolbar is False + if (self.canvas.__class__.__name__ != "JupyterWgpuCanvas") or (not toolbar): return self.canvas if self.toolbar is None: diff --git a/fastplotlib/plot.py b/fastplotlib/plot.py index b756bd9b7..1dcb75e5f 100644 --- a/fastplotlib/plot.py +++ b/fastplotlib/plot.py @@ -3,14 +3,6 @@ from wgpu.gui.auto import WgpuCanvas from .layouts._subplot import Subplot from ipywidgets import HBox, Layout, Button, ToggleButton, VBox - -try: - from wgpu.gui.jupyter import JupyterWgpuCanvas -except (ImportError, ModuleNotFoundError): - HAS_JUPYTER_CANVAS = False -else: - HAS_JUPYTER_CANVAS = True - from .layouts._record_mixin import RecordMixin from datetime import datetime import traceback @@ -146,13 +138,9 @@ def show( if autoscale: self.auto_scale(maintain_aspect=maintain_aspect, zoom=0.95) - # if jupyter_rfb is installed - if HAS_JUPYTER_CANVAS: - # check if in jupyter notebook, or if toolbar is False - if (not isinstance(self.canvas, JupyterWgpuCanvas)) or (not toolbar): - return self.canvas - else: - return self.canvas + # check if in jupyter notebook, or if toolbar is False + if (self.canvas.__class__.__name__ != "JupyterWgpuCanvas") or (not toolbar): + return self.canvas if self.toolbar is None: self.toolbar = ToolBar(self) From 28ece73598e002a3fc869cd2e2a0c1f9ab27b473 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Thu, 22 Jun 2023 18:35:13 -0400 Subject: [PATCH 3/6] conditional imports for ipywidgets --- fastplotlib/layouts/_gridplot.py | 5 +++-- fastplotlib/plot.py | 12 ++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/fastplotlib/layouts/_gridplot.py b/fastplotlib/layouts/_gridplot.py index 69ed90203..8f25f927b 100644 --- a/fastplotlib/layouts/_gridplot.py +++ b/fastplotlib/layouts/_gridplot.py @@ -8,9 +8,10 @@ import pygfx -from wgpu.gui.auto import WgpuCanvas +from wgpu.gui.auto import WgpuCanvas, is_jupyter -from ipywidgets import HBox, Layout, Button, ToggleButton, VBox, Dropdown +if is_jupyter(): + from ipywidgets import HBox, Layout, Button, ToggleButton, VBox, Dropdown from ._utils import make_canvas_and_renderer from ._defaults import create_controller diff --git a/fastplotlib/plot.py b/fastplotlib/plot.py index 1dcb75e5f..e3c358bc2 100644 --- a/fastplotlib/plot.py +++ b/fastplotlib/plot.py @@ -1,11 +1,15 @@ from typing import * +from datetime import datetime +import traceback + import pygfx -from wgpu.gui.auto import WgpuCanvas +from wgpu.gui.auto import WgpuCanvas, is_jupyter + +if is_jupyter(): + from ipywidgets import HBox, Layout, Button, ToggleButton, VBox + from .layouts._subplot import Subplot -from ipywidgets import HBox, Layout, Button, ToggleButton, VBox from .layouts._record_mixin import RecordMixin -from datetime import datetime -import traceback class Plot(Subplot, RecordMixin): From 9338f2520f9880777bef8c015c19e6b25597b518 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Fri, 23 Jun 2023 02:32:07 -0400 Subject: [PATCH 4/6] jupyter conditional for image widget --- fastplotlib/widgets/image.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/fastplotlib/widgets/image.py b/fastplotlib/widgets/image.py index 8a0c67e03..038033e7c 100644 --- a/fastplotlib/widgets/image.py +++ b/fastplotlib/widgets/image.py @@ -1,15 +1,13 @@ from typing import * from warnings import warn from functools import partial -from copy import deepcopy -import weakref import numpy as np -from ipywidgets.widgets import IntSlider, VBox, HBox, Layout, FloatRangeSlider, Button, BoundedIntText, Play, jslink -from wgpu.gui.jupyter import JupyterWgpuCanvas +from wgpu.gui.auto import is_jupyter +if is_jupyter(): + from ipywidgets.widgets import IntSlider, VBox, HBox, Layout, FloatRangeSlider, Button, BoundedIntText, Play, jslink -from ..plot import Plot from ..layouts import GridPlot from ..graphics import ImageGraphic from ..utils import quick_min_max, calculate_gridshape @@ -242,6 +240,11 @@ def __init__( kwargs: Any passed to fastplotlib.graphics.Image """ + if not is_jupyter(): + raise EnvironmentError( + "ImageWidget is currently not supported outside of jupyter" + ) + self._names = None self.toolbar = None @@ -913,11 +916,8 @@ def show(self, toolbar: bool = True): ``ipywidgets.VBox`` stacking the plotter and sliders in a vertical layout """ - if not isinstance(self.gridplot.canvas, JupyterWgpuCanvas): - raise TypeError("ImageWidget is currently not supported outside of Jupyter") - - # check if in jupyter notebook, or if toolbar is False - if (not isinstance(self.gridplot.canvas, JupyterWgpuCanvas)) or (not toolbar): + # don't need to check for jupyter since ImageWidget is only supported within jupyter anyways + if not toolbar: return VBox([self.gridplot.show(toolbar=False), self._vbox_sliders]) if self.toolbar is None: From cb60c9122af55e4b01ffbb31dc22f227fff681a2 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Fri, 23 Jun 2023 02:42:42 -0400 Subject: [PATCH 5/6] more stuff to fix conditional imports --- fastplotlib/__init__.py | 14 +++++++++++--- fastplotlib/widgets/image.py | 4 +--- setup.py | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/fastplotlib/__init__.py b/fastplotlib/__init__.py index 04886de25..7eef88276 100644 --- a/fastplotlib/__init__.py +++ b/fastplotlib/__init__.py @@ -1,9 +1,17 @@ -from .plot import Plot -from .layouts import GridPlot -from .widgets import ImageWidget from pathlib import Path + from wgpu.gui.auto import run +from .plot import Plot +from .layouts import GridPlot + +try: + import ipywidgets +except (ModuleNotFoundError, ImportError): + pass +else: + from .widgets import ImageWidget + with open(Path(__file__).parent.joinpath("VERSION"), "r") as f: __version__ = f.read().split("\n")[0] diff --git a/fastplotlib/widgets/image.py b/fastplotlib/widgets/image.py index 038033e7c..6f5fd2cc5 100644 --- a/fastplotlib/widgets/image.py +++ b/fastplotlib/widgets/image.py @@ -4,9 +4,7 @@ import numpy as np -from wgpu.gui.auto import is_jupyter -if is_jupyter(): - from ipywidgets.widgets import IntSlider, VBox, HBox, Layout, FloatRangeSlider, Button, BoundedIntText, Play, jslink +from ipywidgets.widgets import IntSlider, VBox, HBox, Layout, FloatRangeSlider, Button, BoundedIntText, Play, jslink from ..layouts import GridPlot from ..graphics import ImageGraphic diff --git a/setup.py b/setup.py index b7212fa23..1f4e5cb3a 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,8 @@ "docs": [ "sphinx", "pydata-sphinx-theme<0.10.0", - "glfw" + "glfw", + "jupyter_rfb" # required so ImageWidget docs show up ], "notebook": From 50ddceb23a5ca683788ab5d053b3edf7747ba527 Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Fri, 23 Jun 2023 02:56:24 -0400 Subject: [PATCH 6/6] forgot import --- fastplotlib/widgets/image.py | 1 + 1 file changed, 1 insertion(+) diff --git a/fastplotlib/widgets/image.py b/fastplotlib/widgets/image.py index 6f5fd2cc5..e57bae216 100644 --- a/fastplotlib/widgets/image.py +++ b/fastplotlib/widgets/image.py @@ -4,6 +4,7 @@ import numpy as np +from wgpu.gui.auto import is_jupyter from ipywidgets.widgets import IntSlider, VBox, HBox, Layout, FloatRangeSlider, Button, BoundedIntText, Play, jslink from ..layouts import GridPlot