VTK widgets for trame
trame-vtk extends the trame web framework with components to interface with VTK (Visualization Toolkit) and/or ParaView. It enables the creation of rich visualization and data processing applications in Python, leveraging VTK's capabilities with options for both remote and client-side rendering via vtk.js. The library is actively developed, receiving regular updates in line with the trame ecosystem. The current version is 2.11.7.
Warnings
- breaking With the release of `trame` v3, `trame-vtk` (along with `trame-vuetify` and other widget sets) is no longer an implicit dependency of the core `trame` package. Applications must now explicitly install `trame-vtk` and other required widget packages.
- breaking Starting January 2024, `trame` v3 began defaulting to a Vue3 client. Existing applications built with Vue2-based widgets might experience breakage. While `trame` aims for backward compatibility, it's safer to explicitly declare the client type.
- gotcha Web browser caches can cause issues when switching between Vue2 and Vue3-based trame applications, leading to unexpected rendering or behavior.
- gotcha The `vue-vtk-js` library (used by `trame-vtk`) uses `Math.round` for setting view sizes, which rounds half to the nearest even integer. Python's built-in `round()` function might behave differently depending on the Python version (e.g., Python 3's `round()` also rounds half to even, but custom implementations or older Python versions might vary).
- deprecated Kitware is actively developing `trame-vtklocal` as the default implementation for local rendering using VTK.wasm, replacing the older `vtk.js` based local rendering in `trame-vtk`. While `trame-vtk` still supports `vtk.js`, `trame-vtklocal` offers a more robust solution.
Install
-
pip install trame-vtk -
pip install trame trame-vuetify trame-vtk vtk
Imports
- vtk
from trame.widgets import vtk
- VtkRemoteView
from trame.widgets.vtk import VtkRemoteView
- VtkLocalView
from trame.widgets.vtk import VtkLocalView
- vtkConeSource
from vtkmodules.vtkFiltersSources import vtkConeSource
Quickstart
import vtk
from trame.app import get_server
from trame.ui.vuetify3 import VAppLayout
from trame.widgets import vuetify3 as v3
from trame.widgets.vtk import VtkRemoteView
from vtkmodules.vtkFiltersSources import vtkConeSource
from vtkmodules.vtkRenderingCore import (vtkActor, vtkPolyDataMapper, vtkRenderer, vtkRenderWindow)
# -----------------------------------------------------------------------------
# VTK pipeline
# -----------------------------------------------------------------------------
cone = vtkConeSource()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(cone.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
renderer = vtkRenderer()
renderer.AddActor(actor)
renderer.ResetCamera()
render_window = vtkRenderWindow()
render_window.AddRenderer(renderer)
# -----------------------------------------------------------------------------
# Trame setup
# -----------------------------------------------------------------------------
server = get_server('trame_vtk_cone')
server.client_type = "vue3" # Explicitly set for trame v3 and above
state, ctrl = server.state, server.controller
with VAppLayout(server, full_height=True) as layout:
with v3.VContainer(fluid=True, classes='fill-height'):
with VtkRemoteView(render_window) as view:
ctrl.view_update = view.update
ctrl.view_reset_camera = view.reset_camera
@state.change('resolution')
def update_resolution(resolution, **kwargs):
cone.SetResolution(resolution)
ctrl.view_update()
@ctrl.add('reset_camera')
def reset_camera():
ctrl.view_reset_camera()
if __name__ == '__main__':
server.start()