From c721ac3727dbf14cc3c3c890d56600ad461166d0 Mon Sep 17 00:00:00 2001 From: Kushal Kolar Date: Fri, 23 Jun 2023 03:33:42 -0400 Subject: [PATCH 1/2] remove JupyterWgpuCanvas from imports, allow minimal install to work (#254) --- fastplotlib/__init__.py | 14 +++++++++++--- fastplotlib/layouts/_gridplot.py | 8 ++++---- fastplotlib/plot.py | 15 +++++++++------ fastplotlib/widgets/image.py | 19 +++++++++---------- setup.py | 3 ++- 5 files changed, 35 insertions(+), 24 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/layouts/_gridplot.py b/fastplotlib/layouts/_gridplot.py index 8cf46d33e..8f25f927b 100644 --- a/fastplotlib/layouts/_gridplot.py +++ b/fastplotlib/layouts/_gridplot.py @@ -8,10 +8,10 @@ import pygfx -from wgpu.gui.auto import WgpuCanvas -from wgpu.gui.jupyter import JupyterWgpuCanvas +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 @@ -310,7 +310,7 @@ def show( 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 (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 b239c0f46..e3c358bc2 100644 --- a/fastplotlib/plot.py +++ b/fastplotlib/plot.py @@ -1,12 +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 wgpu.gui.jupyter import JupyterWgpuCanvas from .layouts._record_mixin import RecordMixin -from datetime import datetime -import traceback class Plot(Subplot, RecordMixin): @@ -140,7 +143,7 @@ def show( 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): + if (self.canvas.__class__.__name__ != "JupyterWgpuCanvas") or (not toolbar): return self.canvas if self.toolbar is None: diff --git a/fastplotlib/widgets/image.py b/fastplotlib/widgets/image.py index 8a0c67e03..e57bae216 100644 --- a/fastplotlib/widgets/image.py +++ b/fastplotlib/widgets/image.py @@ -1,15 +1,12 @@ 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 +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 +239,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 +915,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: 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 e001c53f535eecb6e80426303f247bc709f26a19 Mon Sep 17 00:00:00 2001 From: Kushal Kolar Date: Sat, 24 Jun 2023 05:52:22 -0400 Subject: [PATCH 2/2] bump VERSION --- fastplotlib/VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastplotlib/VERSION b/fastplotlib/VERSION index 6cb992894..31af98a76 100644 --- a/fastplotlib/VERSION +++ b/fastplotlib/VERSION @@ -1 +1 @@ -0.1.0.a10 \ No newline at end of file +0.1.0.a11