Skip to content

Commit f3c39bc

Browse files
docs: Add support for plotly example rendering (#395)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 57156f9 commit f3c39bc

File tree

6 files changed

+64
-22
lines changed

6 files changed

+64
-22
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Docs: Add support for plotly example rendering

doc/source/conf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
latex,
1212
watermark,
1313
)
14+
import plotly.io as pio
15+
from plotly.io._sg_scraper import plotly_sg_scraper
1416
import pyvista
1517
from pyvista.plotting.utilities.sphinx_gallery import DynamicScraper
1618
from sphinx.builders.latex import LaTeXBuilder
@@ -20,6 +22,9 @@
2022

2123
ansys.tools.visualization_interface.DOCUMENTATION_BUILD = True
2224

25+
pio.renderers.default = "sphinx_gallery"
26+
27+
2328
LaTeXBuilder.supported_image_types = ["image/png", "image/pdf", "image/svg+xml"]
2429

2530
os.environ["PYANSYS_VISUALIZER_DOC_MODE"] = "true"
@@ -107,7 +112,7 @@
107112
"backreferences_dir": None,
108113
# Modules for which function level galleries are created. In
109114
"doc_module": "ansys-tools-visualization-interface",
110-
"image_scrapers": (DynamicScraper(), "matplotlib"),
115+
"image_scrapers": (DynamicScraper(), "matplotlib", plotly_sg_scraper),
111116
"ignore_pattern": "flycheck*",
112117
"thumbnail_size": (350, 350),
113118
"remove_config_comments": True,

doc/source/user_guide/migration.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,36 @@ This topic explains how to migrate from the PyVista documentation configuration
214214

215215
.. code-block:: python
216216
217-
nbsphinx_execute = "always"
217+
nbsphinx_execute = "always"
218+
219+
220+
For Plotly, in ``conf.py``, do the following:
221+
222+
1. Add environment variables for documentation:
223+
224+
.. code-block:: python
225+
226+
os.environ["PYANSYS_VISUALIZER_DOC_MODE"] = "true"
227+
228+
229+
2. Add plotly configuration
230+
231+
.. code-block:: python
232+
233+
import plotly.io as pio
234+
235+
pio.renderers.default = "sphinx_gallery"
236+
237+
238+
3. Import and add scraper
239+
240+
.. code-block:: python
241+
242+
from plotly.io._sg_scraper import plotly_sg_scraper
243+
244+
sphinx_gallery_conf = {
245+
"image_scrapers": (DynamicScraper(), "matplotlib", plotly_sg_scraper),
246+
}
247+
248+
249+
4. **[IMPORTANT]** The ``pl.show()`` must be the last line of code in the cell, or else it won't show.

doc/styles/config/vocabularies/ANSYS/accept.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(?i)Ansys
22
pytest
33
unhovered
4+
(?i)Plotly
5+
46

57
t.M
68
a.P

examples/01-basic-plotly-examples/plain-usage.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
# Plot the MultiBlock
5656
pl.plot(multi_block)
5757

58-
#####################
5958
# Display the plotter
60-
#
61-
# code-block:: python
62-
#
63-
# pl.show()
6459

60+
pl.show()
61+
62+
############################
6563
# Now create a custom object
64+
# ==========================
65+
6666
class CustomObject:
6767
def __init__(self):
6868
self.name = "CustomObject"
@@ -89,12 +89,14 @@ def name(self):
8989
# Display the plotter again
9090
# =========================
9191
# Since Plotly is a web-based visualization, we can show the plot again to include the new object.
92-
#
93-
# code-block:: python
94-
#
95-
# pl.show()
9692

93+
pl.show()
94+
95+
96+
#####################################
9797
# Add a Plotly Mesh3d object directly
98+
# ===================================
99+
98100
custom_mesh3d = Mesh3d(
99101
x=[0, 1, 2],
100102
y=[0, 1, 0],
@@ -119,12 +121,4 @@ def name(self):
119121
)
120122
pl.plot(scatter)
121123

122-
123-
124-
###########################
125-
# Display the plotter again
126-
# =========================
127-
#
128-
# code-block:: python
129-
#
130-
# pl.show()
124+
pl.show()

src/ansys/tools/visualization_interface/backends/plotly/plotly_interface.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def show(self,
186186
plottable_object=None,
187187
screenshot: str = None,
188188
name_filter=None,
189-
**kwargs) -> None:
189+
**kwargs) -> Union[go.Figure, None]:
190190
"""Render the Plotly scene.
191191
192192
Parameters
@@ -199,11 +199,19 @@ def show(self,
199199
Flag to filter the object, by default None.
200200
kwargs : dict
201201
Additional options the selected backend accepts.
202+
203+
Returns
204+
-------
205+
Union[go.Figure, None]
206+
The figure of the plot if in doc building environment. Else, None.
202207
"""
208+
import os
209+
if os.environ.get("PYANSYS_VISUALIZER_DOC_MODE"):
210+
return self._fig
211+
203212
if plottable_object is not None:
204213
self.plot(plottable_object)
205214

206-
207215
# Only show in browser if no screenshot is being taken
208216
if not screenshot:
209217
self._fig.show(**kwargs)

0 commit comments

Comments
 (0)