diff --git a/doc/changelog.d/312.miscellaneous.md b/doc/changelog.d/312.miscellaneous.md new file mode 100644 index 00000000..4ec3d222 --- /dev/null +++ b/doc/changelog.d/312.miscellaneous.md @@ -0,0 +1 @@ +Fix: improve kwargs processing in `show` \ No newline at end of file diff --git a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py index 499a2a88..a9daf734 100644 --- a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py +++ b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py @@ -21,6 +21,7 @@ # SOFTWARE. """Provides a wrapper to aid in plotting.""" from abc import abstractmethod +from collections.abc import Callable import importlib.util from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union @@ -405,6 +406,31 @@ def disable_hover(self): """Disable hover capabilities in the plotter.""" self._hover_widget.EnabledOff() + def __extract_kwargs(self, func_name: Callable, input_kwargs: Dict[str, Any]) -> Dict[str, Any]: + """Extracts the keyword arguments from a function signature and returns it as dict. + + Parameters + ---------- + func_name : Callable + Function to extract the keyword arguments from. It should be a callable function + input_kwargs : Dict[str, Any] + Dictionary with the keyword arguments to update the extracted ones. + + Returns + ------- + Dict[str, Any] + Dictionary with the keyword arguments extracted from the function signature and + updated with the input kwargs. + """ + import inspect + signature = inspect.signature(func_name) + kwargs = {} + for k, v in signature.parameters.items(): + # We are ignoring positional arguments, and passing everything as kwarg + if v.default is not inspect.Parameter.empty: + kwargs[k] = input_kwargs[k] if k in input_kwargs else v.default + return kwargs + def show( self, plottable_object: Any = None, @@ -412,8 +438,7 @@ def show( view_2d: Dict = None, name_filter: str = None, dark_mode: bool = False, - plotting_options: Optional[Dict[str, Any]] = {}, - **show_options: Dict[str, Any], + **kwargs: Dict[str, Any], ) -> List[Any]: """Plot and show any PyAnsys object. @@ -432,11 +457,8 @@ def show( Regular expression with the desired name or names to include in the plotter. dark_mode : bool, default: False Whether to use dark mode for the widgets. - plotting_options : dict, default: None - Keyword arguments. For allowable keyword arguments, see the - :meth:`Plotter.add_mesh ` method. - **show_options : Any - Additional keyword arguments for the show method. + **kwargs : Any + Additional keyword arguments for the show or plot method. Returns ------- @@ -444,6 +466,14 @@ def show( List with the picked bodies in the picked order. """ + plotting_options = self.__extract_kwargs( + self._pl._scene.add_mesh, + kwargs, + ) + show_options = self.__extract_kwargs( + self._pl.scene.show, + kwargs, + ) self.plot(plottable_object, name_filter, **plotting_options) if self._pl.object_to_actors_map: self._object_to_actors_map = self._pl.object_to_actors_map @@ -479,6 +509,8 @@ def show( # Update all buttons/widgets [widget.update() for widget in self._widgets] + # Remove screenshot from show options since we pass it manually + show_options.pop("screenshot", None) self.show_plotter(screenshot, **show_options) picked_objects_list = [] diff --git a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py index 3a67d35b..1708cd3d 100644 --- a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py +++ b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py @@ -328,8 +328,9 @@ def show( jupyter_backend : str, default: None PyVista Jupyter backend. **kwargs : dict, default: None - Plotting keyword arguments. For allowable keyword arguments, see the - :meth:`Plotter.show ` method. + Plotting and show keyword arguments. For allowable keyword arguments, see the + :meth:`Plotter.show ` and + :meth:`Plotter.show ` methods. Notes ----- @@ -360,6 +361,8 @@ def show( if kwargs.get("screenshot") is not None: self.scene.off_screen = True if jupyter_backend: + # Remove jupyter_backend from show options since we pass it manually + kwargs.pop("jupyter_backend", None) self.scene.show(jupyter_backend=jupyter_backend, **kwargs) else: if self._use_qt: diff --git a/src/ansys/tools/visualization_interface/plotter.py b/src/ansys/tools/visualization_interface/plotter.py index bcf36aad..38c5ad56 100644 --- a/src/ansys/tools/visualization_interface/plotter.py +++ b/src/ansys/tools/visualization_interface/plotter.py @@ -66,7 +66,7 @@ def show( plottable_object: Any = None, screenshot: str = None, name_filter: bool = None, - **plotting_options + **kwargs ) -> None: """Show the plotted objects. @@ -78,12 +78,12 @@ def show( Path to save a screenshot, by default None. name_filter : bool, optional Flag to filter the object, by default None. - plotting_options : dict - Additional plotting options the selected backend accepts. + kwargs : dict + Additional options the selected backend accepts. """ self._backend.show( plottable_object=plottable_object, screenshot=screenshot, name_filter=name_filter, - **plotting_options + **kwargs ) diff --git a/tests/test_generic_plotter.py b/tests/test_generic_plotter.py index f6c28862..33b66d3f 100644 --- a/tests/test_generic_plotter.py +++ b/tests/test_generic_plotter.py @@ -188,5 +188,5 @@ def test_plotter_show_mix(): # Plot pl.plot(sphere1, opacity=0.5, color="blue") - # Mix plot and show - pl.show(sphere, plotting_options={"show_edges": True}, cpos="xy") + # Mix plot and show kwargs + pl.show(sphere, show_edges=True, cpos="xy")