Skip to content

Commit ae32a70

Browse files
authored
Merge pull request #4763 from plotly/remove-requirejs
Use modern `native ES6 import` to load plotly.js bundle instead of `requirejs` which is no longer under active development
2 parents 8c75004 + d717a22 commit ae32a70

File tree

5 files changed

+14
-94
lines changed

5 files changed

+14
-94
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2525

2626
- Fixed a bug in integer validation of arrays that threw an error when an array contained a mix of strings and integers.
2727

28+
- Fixed a bug in JupyterLab >= 4 and Jupyter Notebook >= 7 that caused latex to not render in plotly charts.
29+
30+
- Use modern [native ES6 import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) to load plotly.js bundle instead of requirejs which is [no longer under active development](https://github.com/requirejs/r.js/compare/2.3.6...2.3.7)
31+
2832
## [5.23.0] - 2024-07-23
2933

3034
### Updated

Diff for: packages/python/plotly/plotly/basedatatypes.py

-12
Original file line numberDiff line numberDiff line change
@@ -3538,12 +3538,6 @@ def to_html(self, *args, **kwargs):
35383538
plotly.min.js bundle that is assumed to reside in the same
35393539
directory as the HTML file.
35403540
3541-
If 'require', Plotly.js is loaded using require.js. This option
3542-
assumes that require.js is globally available and that it has been
3543-
globally configured to know how to find Plotly.js as 'plotly'.
3544-
This option is not advised when full_html=True as it will result
3545-
in a non-functional html file.
3546-
35473541
If a string that ends in '.js', a script tag is included that
35483542
references the specified path. This approach can be used to point
35493543
the resulting HTML file to an alternative CDN or local bundle.
@@ -3647,12 +3641,6 @@ def write_html(self, *args, **kwargs):
36473641
directory because the plotly.js source code will be included only
36483642
once per output directory, rather than once per output file.
36493643
3650-
If 'require', Plotly.js is loaded using require.js. This option
3651-
assumes that require.js is globally available and that it has been
3652-
globally configured to know how to find Plotly.js as 'plotly'.
3653-
This option is not advised when full_html=True as it will result
3654-
in a non-functional html file.
3655-
36563644
If a string that ends in '.js', a script tag is included that
36573645
references the specified path. This approach can be used to point
36583646
the resulting HTML file to an alternative CDN or local bundle.

Diff for: packages/python/plotly/plotly/io/_base_renderers.py

+4-39
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ def __init__(
249249
self,
250250
connected=False,
251251
full_html=False,
252-
requirejs=True,
253252
global_init=False,
254253
config=None,
255254
auto_play=False,
@@ -261,7 +260,6 @@ def __init__(
261260
self.auto_play = auto_play
262261
self.connected = connected
263262
self.global_init = global_init
264-
self.requirejs = requirejs
265263
self.full_html = full_html
266264
self.animation_opts = animation_opts
267265
self.post_script = post_script
@@ -275,27 +273,13 @@ def activate(self):
275273
)
276274
)
277275

278-
if not self.requirejs:
279-
raise ValueError("global_init is only supported with requirejs=True")
280-
281276
if self.connected:
282-
# Connected so we configure requirejs with the plotly CDN
283277
script = """\
284278
<script type="text/javascript">
285279
{win_config}
286280
{mathjax_config}
287-
if (typeof require !== 'undefined') {{
288-
require.undef("plotly");
289-
requirejs.config({{
290-
paths: {{
291-
'plotly': ['{plotly_cdn}']
292-
}}
293-
}});
294-
require(['plotly'], function(Plotly) {{
295-
window._Plotly = Plotly;
296-
}});
297-
}}
298281
</script>
282+
<script type="module">import \"{plotly_cdn}\"</script>
299283
""".format(
300284
win_config=_window_plotly_config,
301285
mathjax_config=_mathjax_config,
@@ -309,16 +293,8 @@ def activate(self):
309293
<script type="text/javascript">
310294
{win_config}
311295
{mathjax_config}
312-
if (typeof require !== 'undefined') {{
313-
require.undef("plotly");
314-
define('plotly', function(require, exports, module) {{
315-
{script}
316-
}});
317-
require(['plotly'], function(Plotly) {{
318-
window._Plotly = Plotly;
319-
}});
320-
}}
321296
</script>
297+
<script type="module">{script}</script>
322298
""".format(
323299
script=get_plotlyjs(),
324300
win_config=_window_plotly_config,
@@ -331,10 +307,7 @@ def to_mimebundle(self, fig_dict):
331307

332308
from plotly.io import to_html
333309

334-
if self.requirejs:
335-
include_plotlyjs = "require"
336-
include_mathjax = False
337-
elif self.connected:
310+
if self.connected:
338311
include_plotlyjs = "cdn"
339312
include_mathjax = "cdn"
340313
else:
@@ -416,7 +389,6 @@ def __init__(
416389
super(NotebookRenderer, self).__init__(
417390
connected=connected,
418391
full_html=False,
419-
requirejs=True,
420392
global_init=True,
421393
config=config,
422394
auto_play=auto_play,
@@ -444,7 +416,6 @@ def __init__(
444416
super(KaggleRenderer, self).__init__(
445417
connected=True,
446418
full_html=False,
447-
requirejs=True,
448419
global_init=True,
449420
config=config,
450421
auto_play=auto_play,
@@ -472,7 +443,6 @@ def __init__(
472443
super(AzureRenderer, self).__init__(
473444
connected=True,
474445
full_html=False,
475-
requirejs=True,
476446
global_init=True,
477447
config=config,
478448
auto_play=auto_play,
@@ -497,7 +467,6 @@ def __init__(
497467
super(ColabRenderer, self).__init__(
498468
connected=True,
499469
full_html=True,
500-
requirejs=False,
501470
global_init=False,
502471
config=config,
503472
auto_play=auto_play,
@@ -832,7 +801,6 @@ def __init__(
832801
super(SphinxGalleryHtmlRenderer, self).__init__(
833802
connected=connected,
834803
full_html=False,
835-
requirejs=False,
836804
global_init=False,
837805
config=config,
838806
auto_play=auto_play,
@@ -844,10 +812,7 @@ def to_mimebundle(self, fig_dict):
844812

845813
from plotly.io import to_html
846814

847-
if self.requirejs:
848-
include_plotlyjs = "require"
849-
include_mathjax = False
850-
elif self.connected:
815+
if self.connected:
851816
include_plotlyjs = "cdn"
852817
include_mathjax = "cdn"
853818
else:

Diff for: packages/python/plotly/plotly/io/_html.py

+3-29
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ def to_html(
6767
plotly.min.js bundle that is assumed to reside in the same
6868
directory as the HTML file.
6969
70-
If 'require', Plotly.js is loaded using require.js. This option
71-
assumes that require.js is globally available and that it has been
72-
globally configured to know how to find Plotly.js as 'plotly'.
73-
This option is not advised when full_html=True as it will result
74-
in a non-functional html file.
75-
7670
If a string that ends in '.js', a script tag is included that
7771
references the specified path. This approach can be used to point
7872
the resulting HTML file to an alternative CDN or local bundle.
@@ -254,20 +248,10 @@ def to_html(
254248
if isinstance(include_plotlyjs, str):
255249
include_plotlyjs = include_plotlyjs.lower()
256250

257-
# Start/end of requirejs block (if any)
258-
require_start = ""
259-
require_end = ""
260-
261251
# Init and load
262252
load_plotlyjs = ""
263253

264-
# Init plotlyjs. This block needs to run before plotly.js is loaded in
265-
# order for MathJax configuration to work properly
266-
if include_plotlyjs == "require":
267-
require_start = 'require(["plotly"], function(Plotly) {'
268-
require_end = "});"
269-
270-
elif include_plotlyjs == "cdn":
254+
if include_plotlyjs == "cdn":
271255
load_plotlyjs = """\
272256
{win_config}
273257
<script charset="utf-8" src="{cdn_url}"></script>\
@@ -343,10 +327,8 @@ def to_html(
343327
<div id="{id}" class="plotly-graph-div" \
344328
style="height:{height}; width:{width};"></div>\
345329
<script type="text/javascript">\
346-
{require_start}\
347-
window.PLOTLYENV=window.PLOTLYENV || {{}};{base_url_line}\
348-
{script};\
349-
{require_end}\
330+
window.PLOTLYENV=window.PLOTLYENV || {{}};{base_url_line}\
331+
{script};\
350332
</script>\
351333
</div>""".format(
352334
mathjax_script=mathjax_script,
@@ -355,9 +337,7 @@ def to_html(
355337
width=div_width,
356338
height=div_height,
357339
base_url_line=base_url_line,
358-
require_start=require_start,
359340
script=script,
360-
require_end=require_end,
361341
).strip()
362342

363343
if full_html:
@@ -433,12 +413,6 @@ def write_html(
433413
directory because the plotly.js source code will be included only
434414
once per output directory, rather than once per output file.
435415
436-
If 'require', Plotly.js is loaded using require.js. This option
437-
assumes that require.js is globally available and that it has been
438-
globally configured to know how to find Plotly.js as 'plotly'.
439-
This option is not advised when full_html=True as it will result
440-
in a non-functional html file.
441-
442416
If a string that ends in '.js', a script tag is included that
443417
references the specified path. This approach can be used to point
444418
the resulting HTML file to an alternative CDN or local bundle.

Diff for: packages/python/plotly/plotly/tests/test_io/test_renderers.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,6 @@ def assert_offline(html):
145145
assert get_plotlyjs() in html
146146

147147

148-
def assert_requirejs(html):
149-
assert 'require(["plotly"]' in html
150-
151-
152-
def assert_not_requirejs(html):
153-
assert 'require(["plotly"]' not in html
154-
155-
156148
def test_colab_renderer_show(fig1):
157149
pio.renderers.default = "colab"
158150

@@ -170,7 +162,6 @@ def test_colab_renderer_show(fig1):
170162
html = mock_arg1["text/html"]
171163
assert_full_html(html)
172164
assert_html_renderer_connected(html)
173-
assert_not_requirejs(html)
174165

175166
# check kwargs
176167
mock_kwargs = mock_call_args[1]
@@ -213,7 +204,6 @@ def test_notebook_connected_show(fig1, name, connected):
213204
# Check html display contents
214205
bundle_html = mock_arg1["text/html"]
215206
assert_not_full_html(bundle_html)
216-
assert_requirejs(bundle_html)
217207

218208
# check kwargs
219209
mock_kwargs = mock_call_args[1]
@@ -275,7 +265,6 @@ def open_url(url, new=0, autoraise=True):
275265
html = response.content.decode("utf8")
276266
assert_full_html(html)
277267
assert_offline(html)
278-
assert_not_requirejs(html)
279268

280269

281270
# Validation
@@ -317,12 +306,12 @@ def test_repr_html(renderer):
317306
+ '"></script> '
318307
'<div id="cd462b94-79ce-42a2-887f-2650a761a144" class="plotly-graph-div" '
319308
'style="height:100%; width:100%;"></div> <script type="text/javascript">'
320-
" window.PLOTLYENV=window.PLOTLYENV || {};"
321-
' if (document.getElementById("cd462b94-79ce-42a2-887f-2650a761a144"))'
309+
" window.PLOTLYENV=window.PLOTLYENV || {};"
310+
' if (document.getElementById("cd462b94-79ce-42a2-887f-2650a761a144"))'
322311
' { Plotly.newPlot( "cd462b94-79ce-42a2-887f-2650a761a144",'
323312
' [], {"template":{}},'
324313
' {"responsive": true} ) };'
325-
" </script> </div>"
314+
" </script> </div>"
326315
)
327316
if "text/html" in bundle:
328317
str_bundle = bundle["text/html"]

0 commit comments

Comments
 (0)