Skip to content

Commit 7490cb3

Browse files
authored
add canvas kwarg that takes str (#235)
* add canvas kwarg that takes str * fix
1 parent 6211ea4 commit 7490cb3

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

fastplotlib/layouts/_subplot.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,34 @@
88
import traceback
99

1010
from pygfx import Scene, OrthographicCamera, PanZoomController, OrbitController, \
11-
AxesHelper, GridHelper, WgpuRenderer
11+
AxesHelper, GridHelper, WgpuRenderer, Texture
1212
from wgpu.gui.auto import WgpuCanvas
13+
from wgpu.gui.base import WgpuCanvasBase
14+
15+
16+
# TODO: this determination can be better
17+
try:
18+
from wgpu.gui.jupyter import JupyterWgpuCanvas
19+
except ImportError:
20+
JupyterWgpuCanvas = False
21+
22+
try:
23+
from wgpu.gui.qt import QWgpuCanvas
24+
except ImportError:
25+
QWgpuCanvas = False
26+
27+
try:
28+
from wgpu.gui.glfw import GlfwWgpuCanvas
29+
except ImportError:
30+
GlfwWgpuCanvas = False
31+
32+
33+
CANVAS_OPTIONS = ["jupyter", "glfw", "qt"]
34+
CANVAS_OPTIONS_AVAILABLE = {
35+
"jupyter": JupyterWgpuCanvas,
36+
"glfw": GlfwWgpuCanvas,
37+
"qt": QWgpuCanvas
38+
}
1339

1440
from ._base import PlotArea
1541
from .. import graphics
@@ -24,7 +50,7 @@ def __init__(
2450
parent_dims: Tuple[int, int] = None,
2551
camera: str = '2d',
2652
controller: Union[PanZoomController, OrbitController] = None,
27-
canvas: WgpuCanvas = None,
53+
canvas: Union[str, WgpuCanvas, Texture] = None,
2854
renderer: WgpuRenderer = None,
2955
name: str = None,
3056
**kwargs
@@ -45,8 +71,10 @@ def __init__(
4571
controller: PanZoomController or OrbitOrthoController, optional
4672
``PanZoomController`` type is used for 2D pan-zoom camera control and ``OrbitController`` type is used for
4773
rotating the camera around a center position, used to control the camera
48-
canvas: WgpuCanvas, optional
49-
provides surface on which a scene will be rendered
74+
canvas: WgpuCanvas, Texture, or one of "jupyter", "glfw", "qt", optional
75+
Provides surface on which a scene will be rendered. Can optionally provide a WgpuCanvas instance or a str
76+
to force the PlotArea to use a specific canvas from one of the following options: "jupyter", "glfw", "qt".
77+
Can also provide a pygfx Texture to render to.
5078
renderer: WgpuRenderer, optional
5179
object used to render scenes using wgpu
5280
name: str, optional
@@ -55,6 +83,24 @@ def __init__(
5583
if canvas is None:
5684
canvas = WgpuCanvas()
5785

86+
elif isinstance(canvas, str):
87+
if canvas not in CANVAS_OPTIONS:
88+
raise ValueError(
89+
f"str canvas argument must be one of: {CANVAS_OPTIONS}"
90+
)
91+
elif not CANVAS_OPTIONS_AVAILABLE[canvas]:
92+
raise ImportError(
93+
f"The {canvas} framework is not installed for using this canvas"
94+
)
95+
else:
96+
canvas = CANVAS_OPTIONS_AVAILABLE[canvas]()
97+
98+
elif not isinstance(canvas, (WgpuCanvasBase, Texture)):
99+
raise ValueError(
100+
f"canvas option must either be a valid WgpuCanvas implementation, a pygfx Texture"
101+
f" or a str from the following options: {CANVAS_OPTIONS}"
102+
)
103+
58104
if renderer is None:
59105
renderer = WgpuRenderer(canvas)
60106

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy