Debian Codegraph @

examples_trame/advanced/contour.py

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