| 1 |
"""Trame view interface for PyVista.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import io |
| 6 |
import weakref |
| 7 |
|
| 8 |
from trame.app import get_server as trame_get_server |
| 9 |
from trame.widgets.vtk import VtkLocalView |
| 10 |
from trame.widgets.vtk import VtkRemoteLocalView |
| 11 |
from trame.widgets.vtk import VtkRemoteView |
| 12 |
from trame_vtk.tools.vtksz2html import write_html |
| 13 |
|
| 14 |
CLOSED_PLOTTER_ERROR = ( |
| 15 |
'The render window for this plotter has been destroyed. ' |
| 16 |
'Do not call `show()` for the plotter before passing to trame.' |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
def get_server(*args, **kwargs): # numpydoc ignore=RT01 |
| 21 |
"""Override trame's get_server. |
| 22 |
|
| 23 |
Parameters |
| 24 |
---------- |
| 25 |
*args : |
| 26 |
Any extra args are passed as option to the server instance. |
| 27 |
|
| 28 |
**kwargs : |
| 29 |
Any extra keyword args are passed as option to the server instance. |
| 30 |
|
| 31 |
Returns |
| 32 |
------- |
| 33 |
trame_server.core.Server |
| 34 |
Trame server. |
| 35 |
|
| 36 |
""" |
| 37 |
server = trame_get_server(*args, **kwargs) |
| 38 |
if 'client_type' in kwargs: |
| 39 |
server.client_type = kwargs['client_type'] |
| 40 |
return server |
| 41 |
|
| 42 |
|
| 43 |
class _BasePyVistaView: |
| 44 |
def __init__(self, plotter): |
| 45 |
"""Initialize the base PyVista view.""" |
| 46 |
self._plotter = weakref.ref(plotter) |
| 47 |
self.pyvista_initialize() |
| 48 |
self._plotter_render_callback = lambda *_: self.update() # type: ignore[attr-defined] |
| 49 |
|
| 50 |
def pyvista_initialize(self): |
| 51 |
if self._plotter().render_window is None: # type: ignore[union-attr] |
| 52 |
raise RuntimeError(CLOSED_PLOTTER_ERROR) |
| 53 |
for renderer in self._plotter().renderers: # type: ignore[union-attr] |
| 54 |
if not renderer.camera.is_set: |
| 55 |
renderer.camera_position = renderer.get_default_cam_pos() |
| 56 |
renderer.ResetCamera() |
| 57 |
|
| 58 |
def _post_initialize(self): |
| 59 |
if self._server.running: # type: ignore[attr-defined] |
| 60 |
self.update() # type: ignore[attr-defined] |
| 61 |
else: |
| 62 |
self._server.controller.on_server_ready.add(self.update) # type: ignore[attr-defined] |
| 63 |
|
| 64 |
# Callback to sync view on PyVista's render call when renders are suppressed |
| 65 |
self._plotter().add_on_render_callback(self._plotter_render_callback, render_event=False) # type: ignore[union-attr] |
| 66 |
|
| 67 |
def update_camera(self): |
| 68 |
"""Update camera or push the image.""" |
| 69 |
self.push_camera() # type: ignore[attr-defined] |
| 70 |
self.update_image() # type: ignore[attr-defined] |
| 71 |
|
| 72 |
def export_html(self): |
| 73 |
"""Export scene to HTML as StringIO buffer.""" |
| 74 |
content = io.StringIO() |
| 75 |
if isinstance(self, PyVistaLocalView): |
| 76 |
data = self.export(format='zip') |
| 77 |
if data is None: |
| 78 |
msg = 'No data to write.' |
| 79 |
raise ValueError(msg) |
| 80 |
write_html(data, content) |
| 81 |
content.seek(0) |
| 82 |
elif isinstance(self, PyVistaRemoteLocalView): |
| 83 |
data = self.export_geometry(format='zip') |
| 84 |
if data is None: |
| 85 |
msg = 'No data to write.' |
| 86 |
raise ValueError(msg) |
| 87 |
write_html(data, content) |
| 88 |
content.seek(0) |
| 89 |
else: |
| 90 |
content = self._plotter().export_html(filename=None) # type: ignore[union-attr] |
| 91 |
return io.BytesIO(content.read().encode('utf8')).read() |
| 92 |
|
| 93 |
|
| 94 |
class PyVistaRemoteView(VtkRemoteView, _BasePyVistaView): # type: ignore[misc] |
| 95 |
"""PyVista wrapping of trame ``VtkRemoteView`` for server rendering. |
| 96 |
|
| 97 |
This will connect to a PyVista plotter and stream the server-side |
| 98 |
renderings. |
| 99 |
|
| 100 |
Parameters |
| 101 |
---------- |
| 102 |
plotter : pyvista.Plotter |
| 103 |
The PyVista Plotter to display in the output view. |
| 104 |
|
| 105 |
interactive_ratio : int, optional |
| 106 |
Image size scale factor while interacting. Increasing this |
| 107 |
value will give higher resulotuion images during interaction |
| 108 |
events at the cost of performance. Use lower values (e.g., |
| 109 |
``0.5``) to increase performance while interacting. |
| 110 |
Defaults to 1. |
| 111 |
|
| 112 |
still_ratio : int, optional |
| 113 |
Image size scale factor while not interacting (still). |
| 114 |
Increasing this value will give higher resulotuion images |
| 115 |
when not interacting with the scene. Defaults to 1. |
| 116 |
|
| 117 |
namespace : str, optional |
| 118 |
The namespace for this view component. A default value is |
| 119 |
chosen based on the ``_id_name`` of the plotter. |
| 120 |
|
| 121 |
**kwargs : dict, optional |
| 122 |
Any additional keyword arguments to pass to |
| 123 |
``trame.widgets.vtk.VtkRemoteView``. |
| 124 |
|
| 125 |
Notes |
| 126 |
----- |
| 127 |
For optimal rendering results, you may want to have the same |
| 128 |
value for ``interactive_ratio`` and ``still_ratio`` so that |
| 129 |
the entire rendering is not re-scaled between interaction events. |
| 130 |
|
| 131 |
""" |
| 132 |
|
| 133 |
def __init__( |
| 134 |
self, |
| 135 |
plotter, |
| 136 |
interactive_ratio=None, |
| 137 |
still_ratio=None, |
| 138 |
namespace=None, |
| 139 |
**kwargs, |
| 140 |
): # numpydoc ignore=PR01,RT01 |
| 141 |
"""Create a trame remote view from a PyVista Plotter.""" |
| 142 |
_BasePyVistaView.__init__(self, plotter) |
| 143 |
if namespace is None: |
| 144 |
namespace = f'{plotter._id_name}' |
| 145 |
if interactive_ratio is None: |
| 146 |
interactive_ratio = plotter._theme.trame.interactive_ratio |
| 147 |
if still_ratio is None: |
| 148 |
still_ratio = plotter._theme.trame.still_ratio |
| 149 |
VtkRemoteView.__init__( |
| 150 |
self, |
| 151 |
self._plotter().render_window, # type: ignore[union-attr] |
| 152 |
interactive_ratio=interactive_ratio, |
| 153 |
still_ratio=still_ratio, |
| 154 |
__properties=[('still_ratio', 'stillRatio')], |
| 155 |
ref=f'view_{plotter._id_name}', |
| 156 |
namespace=namespace, |
| 157 |
**kwargs, |
| 158 |
) |
| 159 |
self._post_initialize() |
| 160 |
|
| 161 |
def push_camera(self, *args, **kwargs): # pragma: no cover |
| 162 |
"""No-op implementation to match local viewers.""" |
| 163 |
|
| 164 |
def update_image(self, *args, **kwargs): # numpydoc ignore=RT01 |
| 165 |
"""Wrap update call.""" |
| 166 |
return self.update(*args, **kwargs) |
| 167 |
|
| 168 |
|
| 169 |
class PyVistaLocalView(VtkLocalView, _BasePyVistaView): # type: ignore[misc] |
| 170 |
"""PyVista wrapping of trame VtkLocalView for in-browser rendering. |
| 171 |
|
| 172 |
This will connect to and synchronize with a PyVista plotter to |
| 173 |
perform client-side rendering with VTK.js in the browser. |
| 174 |
|
| 175 |
Parameters |
| 176 |
---------- |
| 177 |
plotter : pyvista.Plotter |
| 178 |
The PyVista Plotter to represent in the output view. |
| 179 |
|
| 180 |
namespace : str, optional |
| 181 |
The namespace for this view component. A default value is |
| 182 |
chosen based on the ``_id_name`` of the plotter. |
| 183 |
|
| 184 |
**kwargs : dict, optional |
| 185 |
Any additional keyword arguments to pass to |
| 186 |
``trame.widgets.vtk.VtkLocalView``. |
| 187 |
|
| 188 |
""" |
| 189 |
|
| 190 |
def __init__(self, plotter, namespace=None, **kwargs): |
| 191 |
"""Create a trame local view from a PyVista Plotter.""" |
| 192 |
_BasePyVistaView.__init__(self, plotter) |
| 193 |
if namespace is None: |
| 194 |
namespace = f'{plotter._id_name}' |
| 195 |
VtkLocalView.__init__( |
| 196 |
self, |
| 197 |
self._plotter().render_window, # type: ignore[union-attr] |
| 198 |
ref=f'view_{plotter._id_name}', |
| 199 |
namespace=namespace, |
| 200 |
**kwargs, |
| 201 |
) |
| 202 |
self._post_initialize() |
| 203 |
|
| 204 |
def _post_initialize(self): |
| 205 |
super()._post_initialize() |
| 206 |
self.set_widgets( |
| 207 |
[ren.axes_widget for ren in self._plotter().renderers if ren.axes_widget is not None], # type: ignore[union-attr] |
| 208 |
) |
| 209 |
|
| 210 |
def update_image(self, *args, **kwargs): # pragma: no cover |
| 211 |
"""No-op implementation to match remote viewers.""" |
| 212 |
|
| 213 |
|
| 214 |
class PyVistaRemoteLocalView(VtkRemoteLocalView, _BasePyVistaView): # type: ignore[misc] |
| 215 |
"""PyVista wrapping of trame ``VtkRemoteLocalView``. |
| 216 |
|
| 217 |
Dynamically switch between client and server rendering. |
| 218 |
|
| 219 |
Parameters |
| 220 |
---------- |
| 221 |
plotter : pyvista.Plotter |
| 222 |
The PyVista Plotter to display in the output view. |
| 223 |
|
| 224 |
interactive_ratio : int, optional |
| 225 |
Image size scale factor while interacting. Increasing this |
| 226 |
value will give higher resulotuion images during interaction |
| 227 |
events at the cost of performance. Use lower values (e.g., |
| 228 |
``0.5``) to increase performance while interacting. |
| 229 |
Defaults to 1. This is only valid in the ``'remote'`` mode. |
| 230 |
|
| 231 |
still_ratio : int, optional |
| 232 |
Image size scale factor while not interacting (still). |
| 233 |
Increasing this value will give higher resulotuion images |
| 234 |
when not interacting with the scene. Defaults to 1. |
| 235 |
This is only valid in the ``'remote'`` mode. |
| 236 |
|
| 237 |
namespace : str, optional |
| 238 |
The namespace for this view component. A default value is |
| 239 |
chosen based on the ``_id_name`` of the plotter. |
| 240 |
|
| 241 |
**kwargs : dict, optional |
| 242 |
Any additional keyword arguments to pass to |
| 243 |
``trame.widgets.vtk.VtkRemoteLocalView``. |
| 244 |
|
| 245 |
""" |
| 246 |
|
| 247 |
def __init__( |
| 248 |
self, |
| 249 |
plotter, |
| 250 |
interactive_ratio=None, |
| 251 |
still_ratio=None, |
| 252 |
namespace=None, |
| 253 |
**kwargs, |
| 254 |
): # numpydoc ignore=PR01,RT01 |
| 255 |
"""Create a trame remote/local view from a PyVista Plotter.""" |
| 256 |
_BasePyVistaView.__init__(self, plotter) |
| 257 |
if namespace is None: |
| 258 |
namespace = f'{plotter._id_name}' |
| 259 |
if interactive_ratio is None: |
| 260 |
interactive_ratio = plotter._theme.trame.interactive_ratio |
| 261 |
if still_ratio is None: |
| 262 |
still_ratio = plotter._theme.trame.still_ratio |
| 263 |
VtkRemoteLocalView.__init__( |
| 264 |
self, |
| 265 |
self._plotter().render_window, # type: ignore[union-attr] |
| 266 |
interactive_ratio=interactive_ratio, |
| 267 |
still_ratio=still_ratio, |
| 268 |
__properties=[('still_ratio', 'stillRatio')], |
| 269 |
ref=f'view_{plotter._id_name}', |
| 270 |
namespace=namespace, |
| 271 |
**kwargs, |
| 272 |
) |
| 273 |
# Track namespace for our use since trame attributes are name mangled |
| 274 |
self._namespace = namespace |
| 275 |
|
| 276 |
self._post_initialize() |
| 277 |
|
| 278 |
def _post_initialize(self): |
| 279 |
super()._post_initialize() |
| 280 |
self.set_widgets( |
| 281 |
[ren.axes_widget for ren in self._plotter().renderers if ren.axes_widget is not None], # type: ignore[union-attr] |
| 282 |
) |