Skip to content

Commit 7c4ee83

Browse files
authored
Merge pull request #5701 from larsoner/sgthumb
fix: make the sphinx-gallery scraper capture every shown figure
2 parents 6d6aaa7 + 8472446 commit 7c4ee83

9 files changed

Lines changed: 1397 additions & 96 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1111
### Fixed
1212
- Fix `mpl_to_plotly` not setting `paper_bgcolor` and `plot_bgcolor` from the matplotlib figure and axes backgrounds, so converted figures match the source figure's background colors [[#5285](https://github.com/plotly/plotly.py/pull/5285)], with thanks to @robertoffmoura for the contribution!
1313
- Fix rendering issue causing a too-large div when calling `Figure.show()` in Google Colab [[#5718](https://github.com/plotly/plotly.py/pull/5718)]
14+
- Fix the sphinx-gallery scraper so that it generates thumbnails for figures shown with `fig.show()` or displayed as the last expression of a code block [[#5701](https://github.com/plotly/plotly.py/pull/5701)], with thanks to @larsoner for the contribution!
15+
- The scaper now warns once (instead of failing the build) when static image export is unavailable
16+
- The sphinx-gallery scraper no longer scrapes files belonging to other examples during parallel builds [[#5701](https://github.com/plotly/plotly.py/pull/5701)], with thanks to @larsoner for the contribution!
1417

1518
### Updated
1619
- Update plotly.js from version 4.0.0 to version 4.1.1 [[#5722](https://github.com/plotly/plotly.py/pull/5722), [#5730](https://github.com/plotly/plotly.py/pull/5730)]. See the plotly.js release notes for [v4.1.0](https://github.com/plotly/plotly.js/releases/tag/v4.1.0) and [v4.1.1](https://github.com/plotly/plotly.js/releases/tag/v4.1.1) for details. Notable changes include:

‎plotly/__init__.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ def hist_series(data_frame, **kwargs):
182182
return histogram(data_frame, **new_kwargs)
183183

184184

185+
def _get_sg_image_scraper():
186+
"""Called by sphinx-gallery when ``"plotly"`` is listed in ``image_scrapers``.
187+
188+
See https://sphinx-gallery.github.io/stable/advanced.html#integrate-custom-scrapers-with-sphinx-gallery
189+
"""
190+
import plotly.io as pio
191+
from plotly.io._sg_scraper import plotly_sg_scraper
192+
193+
# Not left to the import side effect: sphinx-gallery resolves the scraper
194+
# repeatedly, so this also undoes any later renderer change.
195+
pio.renderers.default = "sphinx_gallery_png"
196+
return plotly_sg_scraper
197+
198+
185199
def _jupyter_labextension_paths():
186200
"""Called by Jupyter Lab Server to detect if it is a valid labextension and
187201
to install the extension.

‎plotly/io/_base_renderers.py‎

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
from os.path import isdir
77

88
from plotly import optional_imports
9-
from plotly.io import to_json, to_image, write_image, write_html
9+
from plotly.io import to_json, to_image, write_html
1010
from plotly.io._utils import plotly_cdn_url
1111
from plotly.offline.offline import _get_jconfig, get_plotlyjs
12-
from plotly.tools import return_figure_from_figure_or_data
1312

1413
ipython_display = optional_imports.get_module("IPython.display")
1514
IPython = optional_imports.get_module("IPython")
@@ -812,29 +811,20 @@ def to_mimebundle(self, fig_dict):
812811
return {"text/html": html}
813812

814813

814+
# Figures shown with the "sphinx_gallery_png" renderer are queued here until
815+
# plotly.io._sg_scraper.plotly_sg_scraper collects them, so the renderer itself
816+
# does not need to know where sphinx-gallery wants the files to be written.
817+
sphinx_gallery_figures = []
818+
819+
815820
class SphinxGalleryPngRenderer(ExternalRenderer):
816-
# Note: This renderer was originally designed for use with the orca image generation utility
817-
# before the introduction of kaleido. It has not been tested with kaleido, but I'm not aware
818-
# of any reason why it shouldn't work.
821+
"""Renderer used together with the sphinx-gallery image scraper.
822+
823+
Rather than displaying the figure, this renderer queues it for
824+
``plotly.io.sg_scraper.plotly_sg_scraper``, which embeds it in the example
825+
page as interactive HTML and, when static image export is available,
826+
saves a static image of it for the gallery thumbnail.
827+
"""
828+
819829
def render(self, fig_dict):
820-
stack = inspect.stack()
821-
# Name of script from which plot function was called is retrieved
822-
try:
823-
filename = stack[3].filename # let's hope this is robust...
824-
except Exception: # python 2
825-
filename = stack[3][1]
826-
filename_root, _ = os.path.splitext(filename)
827-
filename_html = filename_root + ".html"
828-
filename_png = filename_root + ".png"
829-
figure = return_figure_from_figure_or_data(fig_dict, True)
830-
_ = write_html(fig_dict, file=filename_html, include_plotlyjs="cdn")
831-
try:
832-
write_image(figure, filename_png)
833-
except (ValueError, ImportError, RuntimeError) as e:
834-
raise RuntimeError(
835-
"kaleido and psutil are required to use the `sphinx_gallery_png` renderer. "
836-
"See https://plotly.com/python/static-image-export/ for instructions on "
837-
"how to install kaleido. Alternatively, you can use the `sphinx_gallery` "
838-
"renderer (note that png thumbnails can only be generated with "
839-
"the `sphinx_gallery_png` renderer)."
840-
) from e
830+
sphinx_gallery_figures.append(fig_dict)

0 commit comments

Comments
 (0)