@@ -129,18 +129,17 @@ def test_scraper(gallery, image_format):
129129 for path , color in zip (gallery .paths , COLORS ):
130130 root = os .path .splitext (path )[0 ]
131131 assert_image_color (Path (f"{ root } .{ image_format } " ), color , image_format )
132- # One interactive embed per figure: wrapped like sphinx-gallery wraps HTML
133- # reprs so themes can style both alike, resized once the page finishes
134- # laying out, and styled as a card so the light background works on dark
135- # pages.
136- for token in (
137- ".. raw:: html" ,
138- "plotly-graph-div" ,
139- 'class="output_subarea' ,
140- "Plotly.Plots.resize" ,
141- 'class="plotly-output-card"' ,
132+ # One interactive embed per figure, wrapped like sphinx-gallery wraps HTML
133+ # reprs, plus one fix-up block styling the embeds as cards on dark pages
134+ # and resizing them once the page finishes laying out.
135+ for token , count in (
136+ (".. raw:: html" , 3 ),
137+ ('class="plotly-graph-div"' , 2 ),
138+ ('class="output_subarea' , 2 ),
139+ ("div.output_subarea:has(.plotly-graph-div)" , 2 ),
140+ ("Plotly.Plots.resize" , 1 ),
142141 ):
143- assert rst .count (token ) == 2 , token
142+ assert rst .count (token ) == count , token
144143
145144 # The thumbnail must be a scraped figure rather than a "no image" default,
146145 # and one image per figure in order is what makes `thumbnail_number` work
@@ -169,7 +168,7 @@ def test_scraper_ignores_other_examples(gallery):
169168 for other in others :
170169 assert other .read_text () == "another example" , f"{ other .name } was scraped"
171170 assert len (gallery .paths ) == 1
172- assert rst .count (".. raw:: html" ) == 1
171+ assert rst .count ('class="plotly-graph-div"' ) == 1
173172
174173
175174def test_scraper_repr_figure (gallery ):
@@ -180,7 +179,9 @@ def test_scraper_repr_figure(gallery):
180179 """
181180 fig = go .Figure ()
182181 gallery .globals ["___" ] = fig # as sphinx-gallery's repr capture leaves it
183- assert gallery .scrape ("fig.update_layout(title='hi')\n fig" ) == ""
182+ rst = gallery .scrape ("fig.update_layout(title='hi')\n fig" )
183+ assert 'class="plotly-graph-div"' not in rst # sphinx-gallery embeds it
184+ assert "Plotly.Plots.resize" in rst # but the fix-up block still applies
184185 assert len (gallery .paths ) == 1
185186 assert_image_color (Path (gallery .paths [0 ]), COLORS [0 ])
186187 assert_image_color (gallery .thumbnail (), COLORS [0 ])
@@ -193,7 +194,7 @@ def test_scraper_repr_figure(gallery):
193194 # A figure both shown and repr-displayed is scraped once
194195 fig .show ()
195196 rst = gallery .scrape ("fig.show()\n fig" )
196- assert rst .count (".. raw:: html" ) == 1
197+ assert rst .count ('class="plotly-graph-div"' ) == 1
197198 assert len (gallery .paths ) == 2
198199
199200
@@ -227,14 +228,14 @@ def kaleido_warnings():
227228 rst = gallery .scrape ("fig.show()" )
228229
229230 # The shown figure is embedded inline instead of via files
230- assert rst .count ("plotly-graph-div" ) == 1
231+ assert rst .count ('class= "plotly-graph-div"' ) == 1
231232 assert gallery .paths == []
232233 (warning ,) = kaleido_warnings ()
233234 assert "no browser" in warning .getMessage ()
234235
235236 # Only one warning per build, however many blocks follow; repr-displayed
236237 # figures are embedded by sphinx-gallery itself so they scrape to nothing
237- assert gallery .scrape ("fig" ) == ""
238+ assert 'class="plotly-graph-div"' not in gallery .scrape ("fig" )
238239 assert gallery .paths == []
239240 assert len (kaleido_warnings ()) == 1
240241
@@ -249,10 +250,6 @@ def test_repr_html_fallback_size(monkeypatch):
249250 monkeypatch .setattr (pio .renderers , "default" , "sphinx_gallery_png" )
250251 html = go .Figure ()._repr_html_ ()
251252 assert 'style="height:525px; width:100%;"' in html
252- # Fixes up figures drawn while the page was still laying out
253- assert "Plotly.Plots.resize" in html
254- # Styled as a card so the light-background figure works on dark pages
255- assert 'class="plotly-output-card"' in html
256253 fig = go .Figure (layout = {"height" : 400 })
257254 assert 'style="height:400px; width:100%;"' in fig ._repr_html_ ()
258255
0 commit comments