Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/312.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix: improve kwargs processing in `show`
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -405,15 +406,43 @@ 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,
screenshot: Optional[str] = None,
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.

Expand All @@ -432,10 +461,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 <pyvista.Plotter.add_mesh>` method.
**show_options : Any
**kwargs : Any
Additional keyword arguments for the show method.

Returns
Expand All @@ -444,6 +470,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
Expand Down Expand Up @@ -479,6 +513,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 = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pyvista.Plotter.show>` method.
Plotting and show keyword arguments. For allowable keyword arguments, see the
:meth:`Plotter.show <pyvista.Plotter.show>` and
:meth:`Plotter.show <pyvista.Plotter.add_mesh>` methods.

Notes
-----
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/ansys/tools/visualization_interface/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
)
4 changes: 2 additions & 2 deletions tests/test_generic_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Loading