From 543844c75aed2ec7c14977da06e97ab020d188de Mon Sep 17 00:00:00 2001 From: afernand Date: Thu, 26 Jun 2025 11:23:12 +0200 Subject: [PATCH 1/6] fix: Improve kwargs processing in `show` --- .../backends/pyvista/pyvista.py | 49 ++++++++++++++++--- .../backends/pyvista/pyvista_interface.py | 6 ++- tests/test_generic_plotter.py | 4 +- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py index 499a2a88..a0bbc5ac 100644 --- a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py +++ b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py @@ -20,7 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. """Provides a wrapper to aid in plotting.""" -from abc import abstractmethod +from abc import Callable, abstractmethod import importlib.util from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union @@ -405,6 +405,35 @@ 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] = v.default + for k, v in kwargs.items(): + # my_args is the arguments passed to the ansys-visualization API + if k in input_kwargs: + kwargs[k] = input_kwargs[k] + return kwargs + def show( self, plottable_object: Any = None, @@ -412,8 +441,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,10 +460,7 @@ 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 + **kwargs : Any Additional keyword arguments for the show method. Returns @@ -444,6 +469,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 +512,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..f4c9d4da 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,7 @@ def show( if kwargs.get("screenshot") is not None: self.scene.off_screen = True if jupyter_backend: + kwargs.pop("jupyter_backend", None) self.scene.show(jupyter_backend=jupyter_backend, **kwargs) else: if self._use_qt: 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") From ea307cc25b6ff76f9726181d5c5defd5e5cdbc1f Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Thu, 26 Jun 2025 09:25:57 +0000 Subject: [PATCH 2/6] chore: adding changelog file 312.miscellaneous.md [dependabot-skip] --- doc/changelog.d/312.miscellaneous.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/312.miscellaneous.md 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 From b1783001a1666997b8f7392dc77bdd809132fcf4 Mon Sep 17 00:00:00 2001 From: afernand Date: Thu, 26 Jun 2025 11:27:38 +0200 Subject: [PATCH 3/6] fix: Callable import --- .../tools/visualization_interface/backends/pyvista/pyvista.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py index a0bbc5ac..0f0efe1e 100644 --- a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py +++ b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py @@ -20,7 +20,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. """Provides a wrapper to aid in plotting.""" -from abc import Callable, abstractmethod +from abc import abstractmethod +from collections.abc import Callable import importlib.util from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union From 9827e143b9664009ac0b0e815beefa0e309f57c8 Mon Sep 17 00:00:00 2001 From: afernand Date: Thu, 26 Jun 2025 11:57:18 +0200 Subject: [PATCH 4/6] fix: rename kwargs --- src/ansys/tools/visualization_interface/plotter.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 ) From 66bd53f56a966668c9eb308a1efd870a1a3332b3 Mon Sep 17 00:00:00 2001 From: Alex Fernandez Date: Thu, 26 Jun 2025 14:50:08 +0200 Subject: [PATCH 5/6] Update src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com> --- .../backends/pyvista/pyvista_interface.py | 1 + 1 file changed, 1 insertion(+) 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 f4c9d4da..1708cd3d 100644 --- a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py +++ b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py @@ -361,6 +361,7 @@ 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: From fb2b436cf1f6a634603d2b39e29d7324cb3fd8bc Mon Sep 17 00:00:00 2001 From: Alex Fernandez Date: Fri, 27 Jun 2025 11:43:21 +0200 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: German <28149841+germa89@users.noreply.github.com> --- .../visualization_interface/backends/pyvista/pyvista.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py index 0f0efe1e..a9daf734 100644 --- a/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py +++ b/src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py @@ -428,11 +428,7 @@ def __extract_kwargs(self, func_name: Callable, input_kwargs: Dict[str, Any]) -> 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] = v.default - for k, v in kwargs.items(): - # my_args is the arguments passed to the ansys-visualization API - if k in input_kwargs: - kwargs[k] = input_kwargs[k] + kwargs[k] = input_kwargs[k] if k in input_kwargs else v.default return kwargs def show( @@ -462,7 +458,7 @@ def show( dark_mode : bool, default: False Whether to use dark mode for the widgets. **kwargs : Any - Additional keyword arguments for the show method. + Additional keyword arguments for the show or plot method. Returns -------