Debian Codegraph @

examples/03-widgets/multi_slider_widget.py

raw
1 """
2 .. _multi_slider_widget_example:
3
4 Multiple Slider Widgets
5 ~~~~~~~~~~~~~~~~~~~~~~~
6
7 Use :func:`~pyvista.Plotter.add_slider_widget` and a class-based callback
8 to track multiple slider widgets for updating a single mesh.
9
10 In this example we simply change a few parameters for the
11 :func:`pyvista.Sphere` method, but this could easily be applied to any
12 mesh-generating/altering code.
13
14 """
15
16 # sphinx_gallery_start_ignore
17 # widgets do not work in interactive examples
18 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
19 # sphinx_gallery_end_ignore
20
21 import pyvista as pv
22
23
24 class MyCustomRoutine: # noqa: D101
25 def __init__(self, mesh):
26 self.output = mesh # Expected PyVista mesh type
27 # default parameters
28 self.kwargs = {
29 'radius': 0.5,
30 'theta_resolution': 30,
31 'phi_resolution': 30,
32 }
33
34 def __call__(self, param, value):
35 self.kwargs[param] = value
36 self.update()
37
38 def update(self):
39 # This is where you call your simulation
40 result = pv.Sphere(**self.kwargs)
41 self.output.copy_from(result)
42
43
44 # %%
45
46 starting_mesh = pv.Sphere()
47 engine = MyCustomRoutine(starting_mesh)
48
49 # %%
50
51 pl = pv.Plotter()
52 pl.add_mesh(starting_mesh, show_edges=True)
53 pl.add_slider_widget(
54 callback=lambda value: engine('phi_resolution', int(value)),
55 rng=[3, 60],
56 value=30,
57 title='Phi Resolution',
58 pointa=(0.025, 0.1),
59 pointb=(0.31, 0.1),
60 style='modern',
61 )
62 pl.add_slider_widget(
63 callback=lambda value: engine('theta_resolution', int(value)),
64 rng=[3, 60],
65 value=30,
66 title='Theta Resolution',
67 pointa=(0.35, 0.1),
68 pointb=(0.64, 0.1),
69 style='modern',
70 )
71 pl.add_slider_widget(
72 callback=lambda value: engine('radius', value),
73 rng=[0.1, 1.5],
74 value=0.5,
75 title='Radius',
76 pointa=(0.67, 0.1),
77 pointb=(0.98, 0.1),
78 style='modern',
79 )
80 pl.show()
81 # %%
82 # .. tags:: widgets