| 1 |
""" |
| 2 |
.. _customize_trame_toolbar_example: |
| 3 |
|
| 4 |
Customize Trame toolbar |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Bring more of the power of trame to the jupyter view. |
| 8 |
This example shows how to add custom tools using the |
| 9 |
`jupyter_kwargs` option with :meth:`~pyvista.Plotter.show`. |
| 10 |
""" |
| 11 |
|
| 12 |
import asyncio |
| 13 |
|
| 14 |
import pyvista as pv |
| 15 |
from pyvista.trame.ui.vuetify3 import button |
| 16 |
from pyvista.trame.ui.vuetify3 import divider |
| 17 |
from pyvista.trame.ui.vuetify3 import select |
| 18 |
from pyvista.trame.ui.vuetify3 import slider |
| 19 |
from pyvista.trame.ui.vuetify3 import text_field |
| 20 |
|
| 21 |
# %% |
| 22 |
# Let's first create the menu items we want to add to the trame's toolbar. |
| 23 |
# Here we want a "play" button that will be later connected to a slider |
| 24 |
# through the ``button_play`` function. The slider itself will represent the |
| 25 |
# "resolution" of the model we will render, a text field where the value of |
| 26 |
# the "resolution" will be displayed. |
| 27 |
# We will also add a dropdown menu to toggle the visibility of the model. |
| 28 |
# The dividers are the same as already used to divide and organize the toolbar. |
| 29 |
|
| 30 |
|
| 31 |
def custom_tools(): |
| 32 |
divider(vertical=True, classes='mx-1') |
| 33 |
button( |
| 34 |
click=button_play, |
| 35 |
icon='mdi-play', |
| 36 |
tooltip='Play', |
| 37 |
) |
| 38 |
|
| 39 |
slider( |
| 40 |
model=('resolution', 10), |
| 41 |
tooltip='Resolution slider', |
| 42 |
min=3, |
| 43 |
max=20, |
| 44 |
step=1, |
| 45 |
dense=True, |
| 46 |
hide_details=True, |
| 47 |
style='width: 300px', |
| 48 |
classes='my-0 py-0 ml-1 mr-1', |
| 49 |
) |
| 50 |
text_field( |
| 51 |
model=('resolution', 10), |
| 52 |
tooltip='Resolution value', |
| 53 |
readonly=True, |
| 54 |
type='number', |
| 55 |
dense=True, |
| 56 |
hide_details=True, |
| 57 |
style='min-width: 40px; width: 60px', |
| 58 |
classes='my-0 py-0 ml-1 mr-1', |
| 59 |
) |
| 60 |
|
| 61 |
divider(vertical=True, classes='mx-1') |
| 62 |
select( |
| 63 |
model=('visibility', 'Show'), |
| 64 |
tooltip='Toggle visibility', |
| 65 |
items=['Visibility', ['Hide', 'Show']], |
| 66 |
hide_details=True, |
| 67 |
dense=True, |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
# %% |
| 72 |
# The button callback function ``button_play`` needs to be created before starting |
| 73 |
# the server. This function will toggle the boolean state variable ``play`` |
| 74 |
# and flush the server, i.e. "force" the server to see the change. |
| 75 |
# We will see more on the state variables in a bit, but we need to create the |
| 76 |
# function here otherwise the server will complain ``button_play`` does not exist. |
| 77 |
|
| 78 |
|
| 79 |
def button_play(): |
| 80 |
state.play = not state.play |
| 81 |
state.flush() |
| 82 |
|
| 83 |
|
| 84 |
# %% |
| 85 |
# We will do a simple rendering of a Cone using `ConeSouce`. |
| 86 |
# |
| 87 |
# When using the ``pl.show`` method. The function we created ``custom_tools`` |
| 88 |
# should be passed as a ``jupyter_kwargs`` argument under the key |
| 89 |
# ``add_menu_items``. |
| 90 |
|
| 91 |
pl = pv.Plotter(notebook=True) |
| 92 |
algo = pv.ConeSource() |
| 93 |
mesh_actor = pl.add_mesh(algo) |
| 94 |
|
| 95 |
widget = pl.show(jupyter_kwargs=dict(add_menu_items=custom_tools), return_viewer=True) |
| 96 |
|
| 97 |
# %% |
| 98 |
# To interact with ``trame``'s server we need to get the server's state. |
| 99 |
# |
| 100 |
# We initialize the ``play`` variable in the shared state and this will be |
| 101 |
# controlled by the play button we created. Note that when creating the |
| 102 |
# ``slider``, the ``text_field`` and the ``select`` tools, we passed something |
| 103 |
# like ``model=("variable", value). This will automatically create the variable |
| 104 |
# "variable" with value ``value`` in the server's shared state, so we do not need |
| 105 |
# to create ``state.resolution`` or ``state.visibility``. |
| 106 |
|
| 107 |
state, ctrl = widget.viewer.server.state, widget.viewer.server.controller |
| 108 |
state.play = False |
| 109 |
ctrl.view_update = widget.viewer.update |
| 110 |
|
| 111 |
# %% |
| 112 |
# Now we can create the callback functions for our menu items. |
| 113 |
# |
| 114 |
# The functions are decorated with a ``state.change("variable")``. This means |
| 115 |
# they will be called when this specific variable has its value changed in the |
| 116 |
# server's shared state. When ``resolution`` changes, we want to update the |
| 117 |
# resolution of our cone algorithm. When ``visibility`` changes, we want to toggle the |
| 118 |
# visibility of our cone. |
| 119 |
# |
| 120 |
# The ``play`` variable is a little bit trickier. We want to start something like |
| 121 |
# a timer so that an animation can be set to play. To do that with ``trame`` we need |
| 122 |
# to have an asynchronous function so we can continue to do stuff while the |
| 123 |
# "timer" function is running. The ``_play`` function will be called when the ``play`` |
| 124 |
# variable is changed (when we click the play button, through the ``button_play`` |
| 125 |
# callback). While ``state.play`` is ``True`` we want to play the animation. We |
| 126 |
# change the ``state.resolution`` value, but to really call the ``update_resolution`` |
| 127 |
# function we need to ``flush`` the server and force it to see the change in |
| 128 |
# the shared variables. When ``state.play`` changes to ``False``, the animation stops. |
| 129 |
# |
| 130 |
# Note that using ``while play: ...`` would not work here because it is not the |
| 131 |
# actual state variable, but only an argument value passed to the callback function. |
| 132 |
|
| 133 |
|
| 134 |
# trame callbacks |
| 135 |
@state.change('play') |
| 136 |
async def _play(play, **kwargs): # noqa: ARG001 |
| 137 |
while state.play: |
| 138 |
state.resolution += 1 |
| 139 |
state.flush() |
| 140 |
if state.resolution >= 20: |
| 141 |
state.play = False |
| 142 |
await asyncio.sleep(0.3) |
| 143 |
|
| 144 |
|
| 145 |
@state.change('resolution') |
| 146 |
def update_resolution(resolution, **kwargs): # noqa: ARG001 |
| 147 |
algo.resolution = resolution |
| 148 |
ctrl.view_update() |
| 149 |
|
| 150 |
|
| 151 |
@state.change('visibility') |
| 152 |
def set_visibility(visibility, **kwargs): # noqa: ARG001 |
| 153 |
toggle = {'Hide': 0, 'Show': 1} |
| 154 |
mesh_actor.visibility = toggle[visibility] |
| 155 |
ctrl.view_update() |
| 156 |
|
| 157 |
|
| 158 |
widget |