@@ -23,10 +23,9 @@ def plotly_sg_scraper(block, block_vars, gallery_conf, **kwargs):
2323 ``sphinx_gallery_png`` renderer, which is made the default renderer as a
2424 side effect of importing this module.
2525
26- Every figure shown that way is written to the gallery image directory
27- twice: once as an interactive HTML file, which is embedded in the page,
28- and once as a static image, which sphinx-gallery uses to generate the
29- thumbnail of the example.
26+ Every figure shown that way is embedded in the page as interactive HTML,
27+ and written to the gallery image directory as a static image, which
28+ sphinx-gallery uses to generate the thumbnail of the example.
3029
3130 A figure that is instead displayed by making it the last expression of a
3231 code block (sphinx-gallery's repr capture) gets a static image too, so
@@ -72,35 +71,20 @@ def plotly_sg_scraper(block, block_vars, gallery_conf, **kwargs):
7271 if fig_dict not in sphinx_gallery_figures :
7372 figures .append ((fig_dict , False ))
7473 try :
75- if not _static_export_available ():
76- # No images means no thumbnails, and sphinx-gallery requires an
77- # image for every path taken from the iterator, so embed shown
78- # figures inline instead of via files.
79- return "" .join (
80- _inline_html (fig_dict ) for fig_dict , shown in figures if shown
81- )
82- html_names = []
83- for (fig_dict , shown ), image_path in zip (figures , image_path_iterator ):
84- # sphinx-gallery hands out one path per image; the HTML file sits
85- # next to the image it is the interactive counterpart of.
86- path_root = os .path .splitext (image_path )[0 ]
87- _write_image (fig_dict , f"{ path_root } .{ image_format } " , image_format )
88- if not shown :
89- # Repr-displayed: sphinx-gallery embeds the HTML itself, the
90- # static image only makes the figure available as a thumbnail.
91- continue
92- plotly .io .write_html (
93- fig_dict ,
94- file = f"{ path_root } .html" ,
95- include_plotlyjs = "cdn" ,
96- full_html = False ,
97- default_width = "100%" ,
98- default_height = 525 ,
99- validate = False ,
100- )
101- html_names .append (f"{ path_root } .html" )
102- # Use the `figure_rst` helper function to generate rST for image files
103- return figure_rst (html_names , gallery_conf ["src_dir" ])
74+ export_available = _static_export_available ()
75+ rst = ""
76+ for fig_dict , shown in figures :
77+ if export_available :
78+ # sphinx-gallery requires an image at every path taken from
79+ # the iterator, so don't consume paths when export failed.
80+ image_path = next (image_path_iterator )
81+ path_root = os .path .splitext (image_path )[0 ]
82+ _write_image (fig_dict , f"{ path_root } .{ image_format } " , image_format )
83+ if shown :
84+ # Repr-displayed figures are embedded by sphinx-gallery
85+ # itself; their static image only serves as the thumbnail.
86+ rst += _inline_html (fig_dict )
87+ return rst
10488 finally :
10589 # Don't let figures leak into the next block if writing one failed.
10690 del sphinx_gallery_figures [:]
@@ -158,7 +142,11 @@ def _static_export_available():
158142
159143
160144def _inline_html (fig_dict ):
161- """Embed a figure into the rst directly, rather than via a file."""
145+ """Embed a figure into the rst directly, rather than via a file.
146+
147+ The figure is wrapped in the same div that sphinx-gallery wraps captured
148+ HTML reprs in, so that themes can style both kinds of embed alike.
149+ """
162150 html = plotly .io .to_html (
163151 fig_dict ,
164152 include_plotlyjs = "cdn" ,
@@ -167,6 +155,11 @@ def _inline_html(fig_dict):
167155 default_height = 525 ,
168156 validate = False ,
169157 )
158+ html = (
159+ '<div class="output_subarea output_html rendered_html output_result">\n '
160+ f"{ html } \n "
161+ "</div>"
162+ )
170163 return "\n .. raw:: html\n \n " + textwrap .indent (html , " " ) + "\n "
171164
172165
@@ -182,31 +175,3 @@ def _write_image(fig_dict, file, image_format):
182175 ) from exc
183176
184177
185- def figure_rst (figure_list , sources_dir ):
186- """Generate RST for a list of HTML filenames.
187-
188- Parameters
189- ----------
190- figure_list : list
191- List of strings of the figures' absolute paths.
192- sources_dir : str
193- absolute path of Sphinx documentation sources (unused, kept for
194- compatibility with the equivalent sphinx-gallery helper)
195-
196- Returns
197- -------
198- images_rst : str
199- rst code to embed the images in the document
200- """
201- # The HTML files live in the "images" directory next to the document that
202- # includes them, so the paths are relative to that document.
203- return "" .join (
204- SINGLE_HTML % ("images/" + os .path .basename (figure_path ))
205- for figure_path in figure_list
206- )
207-
208-
209- SINGLE_HTML = """
210- .. raw:: html
211- :file: %s
212- """
0 commit comments