Skip to content

Commit ce64e49

Browse files
committed
also handle non-connected notebook mode we use in dashboard
In this case, plotly.py will embed the full plotly.js library so we need to dectect this code cell and put it in the html head to not have a non figure div in the output
1 parent 9c9b6e4 commit ce64e49

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/core/jupyter/widgets.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,29 @@ function isWidgetIncludeHtml(html: string) {
190190
}
191191

192192
function isPlotlyLibrary(html: string) {
193-
return (/^\s*<script type="text\/javascript">/.test(html) &&
193+
// Plotly before version 6 used require.js to load the library
194+
const hasRequireScript = (
195+
html: string,
196+
) => (/^\s*<script type="text\/javascript">/.test(html) &&
194197
(/require\.undef\(["']plotly["']\)/.test(html) ||
195-
/define\('plotly'/.test(html))) ||
196-
// also handle new module syntax from plotly.py 6+
198+
/define\('plotly'/.test(html)));
199+
// Plotly 6+ uses the new module syntax
200+
const hasModuleScript = (html: string) =>
197201
/\s*<script type=\"module\">import .*plotly.*<\/script>/.test(html);
202+
// notebook mode non connected embed plotly.js scripts like this:
203+
// * plotly.js v3.0.1
204+
// * Copyright 2012-2025, Plotly, Inc.
205+
// * All rights reserved.
206+
// * Licensed under the MIT license
207+
const hasEmbedScript = (
208+
html: string,
209+
) => (/\* plotly\.js v\d+\.\d+\.\d+\s*\n\s*\* Copyright \d{4}-\d{4}, Plotly, Inc\.\s*\n\s*\* All rights reserved\.\s*\n\s*\* Licensed under the MIT license/
210+
.test(html));
211+
return hasRequireScript(html) ||
212+
// also handle new module syntax from plotly.py 6+
213+
hasModuleScript(html) ||
214+
// detect plotly by its copyright header
215+
hasEmbedScript(html);
198216
}
199217

200218
function htmlLibrariesText(htmlText: string) {

tests/docs/smoke-all/jupyter/subfigures/plotly.qmd

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
---
22
title: "Bugged plotly figure: phantom subfigure"
3-
keep-ipynb: true
4-
keep-md: true
53
_quarto:
64
tests:
75
html:
@@ -17,6 +15,19 @@ _quarto:
1715
ensureHtmlElementCount:
1816
selectors: ['figure.quarto-float-fig figure.quarto-subfloat-fig']
1917
counts: [2]
18+
dashboard:
19+
ensureHtmlElements:
20+
-
21+
- 'figure.quarto-float-fig div#fig-gapminder-1 figure.quarto-subfloat-fig div.plotly-graph-div'
22+
- 'figure.quarto-float-fig div#fig-gapminder-2 figure.quarto-subfloat-fig div.plotly-graph-div'
23+
ensureHtmlElementContents:
24+
selectors:
25+
- 'div#fig-gapminder-1 figcaption.quarto-subfloat-caption'
26+
- 'div#fig-gapminder-2 figcaption.quarto-subfloat-caption'
27+
matches: ['\((a|b)\) Gapminder: (1957|2007)']
28+
ensureHtmlElementCount:
29+
selectors: ['figure.quarto-float-fig figure.quarto-subfloat-fig']
30+
counts: [2]
2031
---
2132

2233
```{python}

0 commit comments

Comments
 (0)