Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove requirejs
  • Loading branch information
marthacryan committed Sep 11, 2024
commit ebd482a12a163de32679129edf4c810424cc9189
54 changes: 5 additions & 49 deletions packages/python/plotly/plotly/io/_base_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ def __init__(
self,
connected=False,
full_html=False,
requirejs=True,
global_init=False,
config=None,
auto_play=False,
Expand All @@ -260,7 +259,6 @@ def __init__(
self.auto_play = auto_play
self.connected = connected
self.global_init = global_init
self.requirejs = requirejs
self.full_html = full_html
self.animation_opts = animation_opts
self.post_script = post_script
Expand All @@ -274,43 +272,14 @@ def activate(self):
)
)

if not self.requirejs:
raise ValueError("global_init is only supported with requirejs=True")

if self.connected:
# Connected so we configure requirejs with the plotly CDN
script = """\
<script type="text/javascript">
{win_config}
{mathjax_config}
if (typeof require !== 'undefined') {{
require.undef("plotly");
require(['plotly'], function(Plotly) {{
window._Plotly = Plotly;
}});
}}
</script>
""".format(
win_config=_window_plotly_config,
mathjax_config=_mathjax_config,
)

else:
# If not connected then we embed a copy of the plotly.js
# library in the notebook
script = """\
<script type="text/javascript">
{win_config}
{mathjax_config}
if (typeof require !== 'undefined') {{
require.undef("plotly");
define('plotly', function(require, exports, module) {{
{script}
}});
require(['plotly'], function(Plotly) {{
window._Plotly = Plotly;
}});
}}
{script}
</script>
""".format(
script=get_plotlyjs(),
Expand All @@ -324,12 +293,8 @@ def to_mimebundle(self, fig_dict):

from plotly.io import to_html

if self.requirejs:
include_plotlyjs = "require"
include_mathjax = False
else:
include_plotlyjs = True
include_mathjax = "cdn"
include_plotlyjs = True
include_mathjax = "cdn"

# build post script
post_script = [
Expand Down Expand Up @@ -406,7 +371,6 @@ def __init__(
super(NotebookRenderer, self).__init__(
connected=connected,
full_html=False,
requirejs=True,
global_init=True,
config=config,
auto_play=auto_play,
Expand Down Expand Up @@ -434,7 +398,6 @@ def __init__(
super(KaggleRenderer, self).__init__(
connected=True,
full_html=False,
requirejs=True,
global_init=True,
config=config,
auto_play=auto_play,
Expand Down Expand Up @@ -462,7 +425,6 @@ def __init__(
super(AzureRenderer, self).__init__(
connected=True,
full_html=False,
requirejs=True,
global_init=True,
config=config,
auto_play=auto_play,
Expand All @@ -487,7 +449,6 @@ def __init__(
super(ColabRenderer, self).__init__(
connected=True,
full_html=True,
requirejs=False,
global_init=False,
config=config,
auto_play=auto_play,
Expand Down Expand Up @@ -822,7 +783,6 @@ def __init__(
super(SphinxGalleryHtmlRenderer, self).__init__(
connected=connected,
full_html=False,
requirejs=False,
global_init=False,
config=config,
auto_play=auto_play,
Expand All @@ -834,12 +794,8 @@ def to_mimebundle(self, fig_dict):

from plotly.io import to_html

if self.requirejs:
include_plotlyjs = "require"
include_mathjax = False
else:
include_plotlyjs = True
include_mathjax = "cdn"
include_plotlyjs = True
include_mathjax = "cdn"

html = to_html(
fig_dict,
Expand Down
27 changes: 3 additions & 24 deletions packages/python/plotly/plotly/io/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,27 +254,10 @@ def to_html(
if isinstance(include_plotlyjs, str):
include_plotlyjs = include_plotlyjs.lower()

# Start/end of requirejs block (if any)
require_start = ""
require_end = ""

# Init and load
load_plotlyjs = ""

# Init plotlyjs. This block needs to run before plotly.js is loaded in
# order for MathJax configuration to work properly
if include_plotlyjs == "require":
require_start = 'require(["plotly"], function(Plotly) {'
require_end = "});"

elif include_plotlyjs == "cdn":
load_plotlyjs = """\
{win_config}
""".format(
win_config=_window_plotly_config
)

elif include_plotlyjs == "directory":
if include_plotlyjs == "directory":
load_plotlyjs = """\
{win_config}
<script charset="utf-8" src="plotly.min.js"></script>\
Expand Down Expand Up @@ -342,10 +325,8 @@ def to_html(
<div id="{id}" class="plotly-graph-div" \
style="height:{height}; width:{width};"></div>\
<script type="text/javascript">\
{require_start}\
window.PLOTLYENV=window.PLOTLYENV || {{}};{base_url_line}\
{script};\
{require_end}\
window.PLOTLYENV=window.PLOTLYENV || {{}};{base_url_line}\
{script};\
</script>\
</div>""".format(
mathjax_script=mathjax_script,
Expand All @@ -354,9 +335,7 @@ def to_html(
width=div_width,
height=div_height,
base_url_line=base_url_line,
require_start=require_start,
script=script,
require_end=require_end,
).strip()

if full_html:
Expand Down
11 changes: 0 additions & 11 deletions packages/python/plotly/plotly/tests/test_io/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ def assert_offline(html):
assert get_plotlyjs() in html


def assert_requirejs(html):
assert 'require(["plotly"]' in html


def assert_not_requirejs(html):
assert 'require(["plotly"]' not in html


def test_colab_renderer_show(fig1):
pio.renderers.default = "colab"

Expand All @@ -165,7 +157,6 @@ def test_colab_renderer_show(fig1):
html = mock_arg1["text/html"]
assert_full_html(html)
assert_html_renderer_connected(html)
assert_not_requirejs(html)

# check kwargs
mock_kwargs = mock_call_args[1]
Expand Down Expand Up @@ -208,7 +199,6 @@ def test_notebook_connected_show(fig1, name, connected):
# Check html display contents
bundle_html = mock_arg1["text/html"]
assert_not_full_html(bundle_html)
assert_requirejs(bundle_html)

# check kwargs
mock_kwargs = mock_call_args[1]
Expand Down Expand Up @@ -270,7 +260,6 @@ def open_url(url, new=0, autoraise=True):
html = response.content.decode("utf8")
assert_full_html(html)
assert_offline(html)
assert_not_requirejs(html)


# Validation
Expand Down