|
6 | 6 | from os.path import isdir |
7 | 7 |
|
8 | 8 | 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 |
10 | 10 | from plotly.io._utils import plotly_cdn_url |
11 | 11 | from plotly.offline.offline import _get_jconfig, get_plotlyjs |
12 | | -from plotly.tools import return_figure_from_figure_or_data |
13 | 12 |
|
14 | 13 | ipython_display = optional_imports.get_module("IPython.display") |
15 | 14 | IPython = optional_imports.get_module("IPython") |
@@ -812,29 +811,20 @@ def to_mimebundle(self, fig_dict): |
812 | 811 | return {"text/html": html} |
813 | 812 |
|
814 | 813 |
|
| 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 | + |
815 | 820 | 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 | + |
819 | 829 | 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