1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
from __future__ import annotations
from trame.app import get_server
from trame.ui.vuetify3 import SinglePageLayout
from trame.widgets import vuetify3
from vtkmodules.vtkFiltersCore import vtkContourFilter
import pyvista as pv
from pyvista import examples
from pyvista.trame.ui import plotter_ui
# -----------------------------------------------------------------------------
# Trame initialization
# -----------------------------------------------------------------------------
pv.OFF_SCREEN = True
server = get_server(client_type='vue3')
state, ctrl = server.state, server.controller
state.trame__title = 'Contour'
ctrl.on_server_ready.add(ctrl.view_update)
# -----------------------------------------------------------------------------
# Pipeline
# -----------------------------------------------------------------------------
volume = examples.download_head_2()
contour = vtkContourFilter()
contour.SetInputDataObject(volume)
# contour.SetComputeNormals(True)
# contour.SetComputeScalars(False)
# Extract data range => Update store/state
data_range = tuple(volume.get_data_range())
contour_value = 0.5 * (data_range[0] + data_range[1])
state.contour_value = contour_value
state.data_range = (float(data_range[0]), float(data_range[1]))
# Configure contour with valid values
contour.SetNumberOfContours(1)
contour.SetValue(0, contour_value)
# -----------------------------------------------------------------------------
# Plotting
# -----------------------------------------------------------------------------
pl = pv.Plotter()
actor = pl.add_mesh(contour, cmap='viridis', clim=data_range)
# -----------------------------------------------------------------------------
# Callbacks
# -----------------------------------------------------------------------------
@state.change('contour_value')
def update_contour(contour_value, **kwargs): # noqa: ARG001
contour.SetValue(0, contour_value)
ctrl.view_update_image()
# -----------------------------------------------------------------------------
# GUI
# -----------------------------------------------------------------------------
with SinglePageLayout(server) as layout:
layout.title.set_text('Contour')
with layout.toolbar:
vuetify3.VSpacer()
vuetify3.VSlider(
v_model='contour_value',
min=('data_range[0]',),
max=('data_range[1]',),
hide_details=True,
density='compact',
style='max-width: 300px',
start="trigger('demoAnimateStart')",
end="trigger('demoAnimateStop')",
change=ctrl.view_update,
)
vuetify3.VProgressLinear(
indeterminate=True,
absolute=True,
bottom=True,
active=('trame__busy',),
)
with layout.content:
with vuetify3.VContainer(
fluid=True,
classes='pa-0 fill-height',
):
# Use PyVista UI template for Plotters
view = plotter_ui(pl, namespace='demo')
ctrl.view_update = view.update
ctrl.view_update_image = view.update_image
server.start()
|