Skip to content

Commit cd064b9

Browse files
AlejandroFernandezLucespyansys-ci-botRobPasMuegerma89
authored
fix: Improve kwargs processing in show (#312)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Roberto Pastor Muela <[email protected]> Co-authored-by: German <[email protected]>
1 parent 3399a1f commit cd064b9

File tree

5 files changed

+51
-15
lines changed

5 files changed

+51
-15
lines changed

doc/changelog.d/312.miscellaneous.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix: improve kwargs processing in `show`

src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# SOFTWARE.
2222
"""Provides a wrapper to aid in plotting."""
2323
from abc import abstractmethod
24+
from collections.abc import Callable
2425
import importlib.util
2526
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
2627

@@ -405,15 +406,39 @@ def disable_hover(self):
405406
"""Disable hover capabilities in the plotter."""
406407
self._hover_widget.EnabledOff()
407408

409+
def __extract_kwargs(self, func_name: Callable, input_kwargs: Dict[str, Any]) -> Dict[str, Any]:
410+
"""Extracts the keyword arguments from a function signature and returns it as dict.
411+
412+
Parameters
413+
----------
414+
func_name : Callable
415+
Function to extract the keyword arguments from. It should be a callable function
416+
input_kwargs : Dict[str, Any]
417+
Dictionary with the keyword arguments to update the extracted ones.
418+
419+
Returns
420+
-------
421+
Dict[str, Any]
422+
Dictionary with the keyword arguments extracted from the function signature and
423+
updated with the input kwargs.
424+
"""
425+
import inspect
426+
signature = inspect.signature(func_name)
427+
kwargs = {}
428+
for k, v in signature.parameters.items():
429+
# We are ignoring positional arguments, and passing everything as kwarg
430+
if v.default is not inspect.Parameter.empty:
431+
kwargs[k] = input_kwargs[k] if k in input_kwargs else v.default
432+
return kwargs
433+
408434
def show(
409435
self,
410436
plottable_object: Any = None,
411437
screenshot: Optional[str] = None,
412438
view_2d: Dict = None,
413439
name_filter: str = None,
414440
dark_mode: bool = False,
415-
plotting_options: Optional[Dict[str, Any]] = {},
416-
**show_options: Dict[str, Any],
441+
**kwargs: Dict[str, Any],
417442
) -> List[Any]:
418443
"""Plot and show any PyAnsys object.
419444
@@ -432,18 +457,23 @@ def show(
432457
Regular expression with the desired name or names to include in the plotter.
433458
dark_mode : bool, default: False
434459
Whether to use dark mode for the widgets.
435-
plotting_options : dict, default: None
436-
Keyword arguments. For allowable keyword arguments, see the
437-
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
438-
**show_options : Any
439-
Additional keyword arguments for the show method.
460+
**kwargs : Any
461+
Additional keyword arguments for the show or plot method.
440462
441463
Returns
442464
-------
443465
List[Any]
444466
List with the picked bodies in the picked order.
445467
446468
"""
469+
plotting_options = self.__extract_kwargs(
470+
self._pl._scene.add_mesh,
471+
kwargs,
472+
)
473+
show_options = self.__extract_kwargs(
474+
self._pl.scene.show,
475+
kwargs,
476+
)
447477
self.plot(plottable_object, name_filter, **plotting_options)
448478
if self._pl.object_to_actors_map:
449479
self._object_to_actors_map = self._pl.object_to_actors_map
@@ -479,6 +509,8 @@ def show(
479509
# Update all buttons/widgets
480510
[widget.update() for widget in self._widgets]
481511

512+
# Remove screenshot from show options since we pass it manually
513+
show_options.pop("screenshot", None)
482514
self.show_plotter(screenshot, **show_options)
483515

484516
picked_objects_list = []

src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ def show(
328328
jupyter_backend : str, default: None
329329
PyVista Jupyter backend.
330330
**kwargs : dict, default: None
331-
Plotting keyword arguments. For allowable keyword arguments, see the
332-
:meth:`Plotter.show <pyvista.Plotter.show>` method.
331+
Plotting and show keyword arguments. For allowable keyword arguments, see the
332+
:meth:`Plotter.show <pyvista.Plotter.show>` and
333+
:meth:`Plotter.show <pyvista.Plotter.add_mesh>` methods.
333334
334335
Notes
335336
-----
@@ -360,6 +361,8 @@ def show(
360361
if kwargs.get("screenshot") is not None:
361362
self.scene.off_screen = True
362363
if jupyter_backend:
364+
# Remove jupyter_backend from show options since we pass it manually
365+
kwargs.pop("jupyter_backend", None)
363366
self.scene.show(jupyter_backend=jupyter_backend, **kwargs)
364367
else:
365368
if self._use_qt:

src/ansys/tools/visualization_interface/plotter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def show(
6666
plottable_object: Any = None,
6767
screenshot: str = None,
6868
name_filter: bool = None,
69-
**plotting_options
69+
**kwargs
7070
) -> None:
7171
"""Show the plotted objects.
7272
@@ -78,12 +78,12 @@ def show(
7878
Path to save a screenshot, by default None.
7979
name_filter : bool, optional
8080
Flag to filter the object, by default None.
81-
plotting_options : dict
82-
Additional plotting options the selected backend accepts.
81+
kwargs : dict
82+
Additional options the selected backend accepts.
8383
"""
8484
self._backend.show(
8585
plottable_object=plottable_object,
8686
screenshot=screenshot,
8787
name_filter=name_filter,
88-
**plotting_options
88+
**kwargs
8989
)

tests/test_generic_plotter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,5 @@ def test_plotter_show_mix():
188188
# Plot
189189
pl.plot(sphere1, opacity=0.5, color="blue")
190190

191-
# Mix plot and show
192-
pl.show(sphere, plotting_options={"show_edges": True}, cpos="xy")
191+
# Mix plot and show kwargs
192+
pl.show(sphere, show_edges=True, cpos="xy")

0 commit comments

Comments
 (0)