2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121# SOFTWARE.
2222"""Module for dash plotly."""
23+ from typing import TYPE_CHECKING , Union
24+
2325from dash import Dash , dcc , html
2426
2527from ansys .tools .visualization_interface .backends .plotly .plotly_interface import PlotlyBackend
2628
29+ if TYPE_CHECKING :
30+ import plotly .graph_objects as go
31+
2732
2833class PlotlyDashBackend (PlotlyBackend ):
2934 """Plotly Dash interface for visualization."""
@@ -39,9 +44,46 @@ def __init__(self, app: Dash = None) -> None:
3944 super ().__init__ ()
4045 self ._app = app or Dash (__name__ )
4146
42- def show (self ) -> None :
43- """Render the Plotly figure in a Dash application."""
44- self ._app .layout = html .Div ([
45- dcc .Graph (figure = self ._fig )
46- ])
47- self ._app .run ()
47+ def show (self ,
48+ plottable_object = None ,
49+ screenshot : str = None ,
50+ name_filter = None ,
51+ ** kwargs ) -> Union ["go.Figure" , None ]:
52+ """Render the Plotly scene.
53+
54+ Parameters
55+ ----------
56+ plottable_object : Any, optional
57+ Object to show, by default None.
58+ screenshot : str, optional
59+ Path to save a screenshot, by default None.
60+ name_filter : bool, optional
61+ Flag to filter the object, by default None.
62+ kwargs : dict
63+ Additional options the selected backend accepts.
64+
65+ Returns
66+ -------
67+ Union[go.Figure, None]
68+ The figure of the plot if in doc building environment. Else, None.
69+ """
70+ import os
71+ if os .environ .get ("PYANSYS_VISUALIZER_DOC_MODE" ):
72+ return self ._fig
73+
74+ if plottable_object is not None :
75+ self .plot (plottable_object )
76+
77+ # Only show in browser if no screenshot is being taken
78+ if not screenshot :
79+ self ._app .layout = html .Div ([
80+ dcc .Graph (figure = self ._fig )
81+ ])
82+ self ._app .run ()
83+
84+ else :
85+ screenshot_str = str (screenshot )
86+ if screenshot_str .endswith ('.html' ):
87+ self ._fig .write_html (screenshot_str )
88+ else :
89+ self ._fig .write_image (screenshot_str )
0 commit comments