| 1 |
"""How to use PyVista UI template. |
| 2 |
|
| 3 |
This example demonstrates how to use ``plotter_ui`` to add a PyVista |
| 4 |
``Plotter`` to a UI with scene controls and standard UI features. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import matplotlib.pyplot as plt |
| 10 |
from trame.app import get_server |
| 11 |
from trame.ui.vuetify3 import SinglePageLayout |
| 12 |
from trame.widgets import vuetify3 |
| 13 |
|
| 14 |
import pyvista as pv |
| 15 |
from pyvista import examples |
| 16 |
from pyvista.trame.ui import get_viewer |
| 17 |
|
| 18 |
pv.OFF_SCREEN = True |
| 19 |
|
| 20 |
server = get_server(client_type='vue3') |
| 21 |
state, ctrl = server.state, server.controller |
| 22 |
|
| 23 |
state.trame__title = 'PyVista UI Template' |
| 24 |
|
| 25 |
# ----------------------------------------------------------------------------- |
| 26 |
|
| 27 |
mesh = examples.load_random_hills() |
| 28 |
|
| 29 |
pl = pv.Plotter() |
| 30 |
actor = pl.add_mesh(mesh, cmap='viridis') |
| 31 |
|
| 32 |
|
| 33 |
@state.change('cmap') |
| 34 |
def update_cmap(cmap='viridis', **kwargs): # noqa: ARG001 |
| 35 |
actor.mapper.lookup_table.cmap = cmap |
| 36 |
ctrl.view_update() |
| 37 |
|
| 38 |
|
| 39 |
# ----------------------------------------------------------------------------- |
| 40 |
# GUI |
| 41 |
# ----------------------------------------------------------------------------- |
| 42 |
|
| 43 |
viewer = get_viewer(pl) |
| 44 |
|
| 45 |
with SinglePageLayout(server) as layout: |
| 46 |
layout.icon.click = ctrl.view_reset_camera |
| 47 |
layout.title.set_text('PyVista Colormaps') |
| 48 |
|
| 49 |
with layout.toolbar: |
| 50 |
# Make sure `mode` matches |
| 51 |
viewer.ui_controls(mode='trame') |
| 52 |
|
| 53 |
vuetify3.VSpacer() |
| 54 |
|
| 55 |
vuetify3.VSelect( |
| 56 |
label='Color map', |
| 57 |
v_model=('cmap', 'viridis'), |
| 58 |
items=('array_list', plt.colormaps()), |
| 59 |
hide_details=True, |
| 60 |
density='compact', |
| 61 |
outlined=True, |
| 62 |
classes='pt-1 ml-2', |
| 63 |
style='max-width: 250px', |
| 64 |
) |
| 65 |
|
| 66 |
with layout.content: |
| 67 |
# Use PyVista UI template for Plotters |
| 68 |
view = viewer.ui(add_menu=False, mode='trame') |
| 69 |
ctrl.view_update = view.update |
| 70 |
|
| 71 |
# hide footer |
| 72 |
layout.footer.hide() |
| 73 |
|
| 74 |
server.start() |