Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove references to plotly.js CDN and requirejs #4762

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 0 additions & 24 deletions packages/python/plotly/plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3524,22 +3524,10 @@ def to_html(self, *args, **kwargs):
is included in the output. HTML files generated with this option are
fully self-contained and can be used offline.

If 'cdn', a script tag that references the plotly.js CDN is included
in the output. HTML files generated with this option are about 3MB
smaller than those generated with include_plotlyjs=True, but they
require an active internet connection in order to load the plotly.js
library.

If 'directory', a script tag is included that references an external
plotly.min.js bundle that is assumed to reside in the same
directory as the HTML file.

If 'require', Plotly.js is loaded using require.js. This option
assumes that require.js is globally available and that it has been
globally configured to know how to find Plotly.js as 'plotly'.
This option is not advised when full_html=True as it will result
in a non-functional html file.

If a string that ends in '.js', a script tag is included that
references the specified path. This approach can be used to point
the resulting HTML file to an alternative CDN or local bundle.
Expand Down Expand Up @@ -3624,12 +3612,6 @@ def write_html(self, *args, **kwargs):
is included in the output. HTML files generated with this option are
fully self-contained and can be used offline.

If 'cdn', a script tag that references the plotly.js CDN is included
in the output. HTML files generated with this option are about 3MB
smaller than those generated with include_plotlyjs=True, but they
require an active internet connection in order to load the plotly.js
library.

If 'directory', a script tag is included that references an external
plotly.min.js bundle that is assumed to reside in the same
directory as the HTML file. If `file` is a string to a local file
Expand All @@ -3643,12 +3625,6 @@ def write_html(self, *args, **kwargs):
directory because the plotly.js source code will be included only
once per output directory, rather than once per output file.

If 'require', Plotly.js is loaded using require.js. This option
assumes that require.js is globally available and that it has been
globally configured to know how to find Plotly.js as 'plotly'.
This option is not advised when full_html=True as it will result
in a non-functional html file.

If a string that ends in '.js', a script tag is included that
references the specified path. This approach can be used to point
the resulting HTML file to an alternative CDN or local bundle.
Expand Down
71 changes: 7 additions & 64 deletions packages/python/plotly/plotly/io/_base_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from plotly import utils, optional_imports
from plotly.io import to_json, to_image, write_image, write_html
from plotly.io._orca import ensure_server
from plotly.io._utils import plotly_cdn_url
from plotly.offline.offline import _get_jconfig, get_plotlyjs
from plotly.tools import return_figure_from_figure_or_data

Expand Down Expand Up @@ -249,7 +248,6 @@ def __init__(
self,
connected=False,
full_html=False,
requirejs=True,
global_init=False,
config=None,
auto_play=False,
Expand All @@ -261,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 @@ -275,49 +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");
requirejs.config({{
paths: {{
'plotly': ['{plotly_cdn}']
}}
}});
require(['plotly'], function(Plotly) {{
window._Plotly = Plotly;
}});
}}
</script>
""".format(
win_config=_window_plotly_config,
mathjax_config=_mathjax_config,
plotly_cdn=plotly_cdn_url().rstrip(".js"),
)

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 @@ -331,15 +293,8 @@ def to_mimebundle(self, fig_dict):

from plotly.io import to_html

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

# build post script
post_script = [
Expand Down Expand Up @@ -416,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 @@ -444,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 @@ -472,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 @@ -497,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 @@ -767,7 +718,7 @@ def __init__(
auto_play=False,
post_script=None,
animation_opts=None,
include_plotlyjs="cdn",
include_plotlyjs=True,
):

self.config = config
Expand Down Expand Up @@ -832,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 @@ -844,15 +794,8 @@ def to_mimebundle(self, fig_dict):

from plotly.io import to_html

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

html = to_html(
fig_dict,
Expand Down Expand Up @@ -882,7 +825,7 @@ def render(self, fig_dict):
filename_html = filename_root + ".html"
filename_png = filename_root + ".png"
figure = return_figure_from_figure_or_data(fig_dict, True)
_ = write_html(fig_dict, file=filename_html, include_plotlyjs="cdn")
_ = write_html(fig_dict, file=filename_html, include_plotlyjs=True)
try:
write_image(figure, filename_png)
except (ValueError, ImportError):
Expand Down
54 changes: 4 additions & 50 deletions packages/python/plotly/plotly/io/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import webbrowser

from _plotly_utils.optional_imports import get_module
from plotly.io._utils import validate_coerce_fig_to_dict, plotly_cdn_url
from plotly.io._utils import validate_coerce_fig_to_dict
from plotly.offline.offline import _get_jconfig, get_plotlyjs

_json = get_module("json")
Expand Down Expand Up @@ -57,22 +57,10 @@ def to_html(
is included in the output. HTML files generated with this option are
fully self-contained and can be used offline.

If 'cdn', a script tag that references the plotly.js CDN is included
in the output. The url used is versioned to match the bundled plotly.js.
HTML files generated with this option are about 3MB smaller than those
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep cdn option as it could help reduce smaller HTML files.
But for loading that we shouldn't use requirejs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok - but we don't have a cdn currently right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we publish every version to CDN.
It's only the -latest version is no longer updated (plotly/plotly.js#5697) since plotly.js v2 and stayed as latest v1 version i.e. 1.58.5.
So we should be able to continue loading from CDN by specifying the exact version.
To achieve that you could add a separate script tag instead of requirejs to load e.g.

<script src="https://cdn.plot.ly/plotly-2.35.1.min.js" charset="utf-8"></script>

Or alternatively for async loading

<script type="module">
    import "https://cdn.plot.ly/plotly-2.35.2.min.js"
    Plotly.newPlot("gd", [{ y: [1, 2, 3] }])
</script>

generated with include_plotlyjs=True, but they require an active
internet connection in order to load the plotly.js library.

If 'directory', a script tag is included that references an external
plotly.min.js bundle that is assumed to reside in the same
directory as the HTML file.

If 'require', Plotly.js is loaded using require.js. This option
assumes that require.js is globally available and that it has been
globally configured to know how to find Plotly.js as 'plotly'.
This option is not advised when full_html=True as it will result
in a non-functional html file.

If a string that ends in '.js', a script tag is included that
references the specified path. This approach can be used to point
the resulting HTML file to an alternative CDN or local bundle.
Expand Down Expand Up @@ -254,28 +242,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}
<script charset="utf-8" src="{cdn_url}"></script>\
""".format(
win_config=_window_plotly_config, cdn_url=plotly_cdn_url()
)

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 @@ -343,10 +313,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 @@ -355,9 +323,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 Expand Up @@ -414,12 +380,6 @@ def write_html(
is included in the output. HTML files generated with this option are
fully self-contained and can be used offline.

If 'cdn', a script tag that references the plotly.js CDN is included
in the output. The url used is versioned to match the bundled plotly.js.
HTML files generated with this option are about 3MB smaller than those
generated with include_plotlyjs=True, but they require an active
internet connection in order to load the plotly.js library.

If 'directory', a script tag is included that references an external
plotly.min.js bundle that is assumed to reside in the same
directory as the HTML file. If `file` is a string to a local file
Expand All @@ -433,12 +393,6 @@ def write_html(
directory because the plotly.js source code will be included only
once per output directory, rather than once per output file.

If 'require', Plotly.js is loaded using require.js. This option
assumes that require.js is globally available and that it has been
globally configured to know how to find Plotly.js as 'plotly'.
This option is not advised when full_html=True as it will result
in a non-functional html file.

If a string that ends in '.js', a script tag is included that
references the specified path. This approach can be used to point
the resulting HTML file to an alternative CDN or local bundle.
Expand Down
7 changes: 0 additions & 7 deletions packages/python/plotly/plotly/io/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,3 @@ def validate_coerce_output_type(output_type):
Must be one of: 'Figure', 'FigureWidget'"""
)
return cls


def plotly_cdn_url(cdn_ver=get_plotlyjs_version()):
"""Return a valid plotly CDN url."""
return "https://cdn.plot.ly/plotly-{cdn_ver}.min.js".format(
cdn_ver=cdn_ver,
)
Loading