Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jupyter = [
'trame-vuetify>=2.3.1',
'trame>=2.5.2',
]
wasm = ['trame-vtklocal']

[dependency-groups]
pinned = [ # Pinned versions of core dependencies
Expand Down
18 changes: 17 additions & 1 deletion pyvista/jupyter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pyvista
from pyvista.core.errors import PyVistaDeprecationWarning as PyVistaDeprecationWarning

JupyterBackendOptions = Literal['static', 'client', 'server', 'trame', 'html', 'none']
JupyterBackendOptions = Literal['static', 'client', 'server', 'trame', 'html', 'vtk-wasm', 'none']
ALLOWED_BACKENDS = get_args(JupyterBackendOptions)


Expand Down Expand Up @@ -52,6 +52,17 @@ def _validate_jupyter_backend(
msg = 'Please install trame dependencies: pip install "pyvista[jupyter]"'
raise ImportError(msg)

if backend == 'vtk-wasm':
try:
import trame_vtklocal # noqa: F401, PLC0415
import vtk # noqa: F401, PLC0415
except ImportError: # pragma: no cover
msg = (
'VTK-WASM backend requires VTK>=9.4 and trame-vtklocal. Install with:'
' pip install --extra-index-url vtk>=9.4 trame-vtklocal'
)
raise ImportError(msg)

return backend


Expand Down Expand Up @@ -88,6 +99,11 @@ def set_jupyter_backend(backend, name=None, **kwargs): # noqa: ARG001
* ``'html'`` : Export/serialize the scene graph to be rendered
with the Trame client backend but in a static HTML file.

* ``'vtk-wasm'`` : Use VTK WebAssembly for client-side rendering.
This provides native VTK performance in the browser without
requiring a server connection. Requires ``vtk>=9.4`` and
``trame-vtklocal`` to be installed.

* ``'none'`` : Do not display any plots within jupyterlab,
instead display using dedicated VTK render windows. This
will generate nothing on headless servers even with a
Expand Down
49 changes: 49 additions & 0 deletions pyvista/jupyter/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from __future__ import annotations

import tempfile
from typing import TYPE_CHECKING
import warnings

Expand Down Expand Up @@ -52,6 +53,9 @@ def handle_plotter(

return show_trame(plotter, mode=backend, **kwargs)

if backend == 'vtk-wasm':
return show_vtk_wasm(plotter, **kwargs)

except ImportError as e:
warnings.warn(
f'Failed to use notebook backend: \n\n{e}\n\nFalling back to a static output.',
Expand All @@ -60,6 +64,51 @@ def handle_plotter(
return show_static_image(plotter, screenshot)


def show_vtk_wasm(plotter: Plotter, **kwargs) -> IFrame: # noqa: ARG001 # numpydoc ignore=RT01
"""Display a VTK WebAssembly widget in a jupyter notebook."""
try:
from IPython.display import IFrame # noqa: PLC0415
import trame_vtklocal # noqa: F401, PLC0415
import vtk # noqa: F401, PLC0415
except ImportError as e:
msg = (
'VTK-WASM backend requires VTK>=9.4 and trame-vtklocal. Install with:'
' pip install --extra-index-url vtk>=9.4 trame-vtklocal'
)
raise ImportError(msg) from e

# Export the scene to a format compatible with vtk-wasm
# Create a temporary HTML file with the vtk-wasm viewer
# TODO: Integrate actual VTK scene data with vtk-wasm viewer

# Export the VTK scene data
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://kitware.github.io/vtk-wasm/guide/viewer/data.html but we don't have a self contained html+data option.

# Create HTML content with vtk-wasm viewer
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>VTK-WASM Viewer</title>
<script src="https://unpkg.com/vtk-wasm/dist/vtk-wasm.js"></script>
</head>
<body>
<div id="vtk-container" style="width: 100%; height: 600px;"></div>
<script>
// Initialize vtk-wasm viewer
const container = document.getElementById('vtk-container');
// VTK-WASM integration code will be added here
console.log('VTK-WASM viewer initialized');
</script>
</body>
</html>
"""
f.write(html_content)
temp_file = f.name

# Return an IFrame pointing to the temporary file
return IFrame(temp_file, width='100%', height=600)


def show_static_image(
plotter: Plotter,
screenshot: str | Path | io.BytesIO | bool | None, # noqa: FBT001
Expand Down
Loading