Config plotly.js during init

plotly.js will be loaded only once, only when first time used

plots show correctly when page re-load or re-open
This commit is contained in:
Jks Liu 2021-01-18 22:59:16 +08:00
parent 672b3a6038
commit 509a8cdc7e
2 changed files with 31 additions and 15 deletions

View File

@ -883,8 +883,6 @@ plotly_series_json(plt::Plot) = JSON.json(plotly_series(plt), 4)
html_head(plt::Plot{PlotlyBackend}) = plotly_html_head(plt)
html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt)
const ijulia_initialized = Ref(false)
function plotly_html_head(plt::Plot)
plotly =
use_local_dependencies[] ? ("file:///" * plotly_local_file_path[]) : "https://cdn.plot.ly/$(_plotly_min_js_filename)"
@ -893,20 +891,11 @@ function plotly_html_head(plt::Plot)
mathjax_file = include_mathjax != "cdn" ? ("file://" * include_mathjax) : "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML"
mathjax_head = include_mathjax == "" ? "" : "<script src=\"$mathjax_file\"></script>\n\t\t"
if isijulia() && !ijulia_initialized[]
# using requirejs seems to be key to load a js depency in IJulia!
# https://requirejs.org/docs/start.html
# https://github.com/JuliaLang/IJulia.jl/issues/345
display("text/html", """
<script type="text/javascript">
requirejs([$(repr(plotly))], function(p) {
window.Plotly = p
});
</script>
""")
ijulia_initialized[] = true
if isijulia()
mathjax_head
else
"$mathjax_head<script src=$(repr(plotly))></script>"
end
return "$mathjax_head<script src=$(repr(plotly))></script>"
end
function plotly_html_body(plt, style = nothing)
@ -914,12 +903,22 @@ function plotly_html_body(plt, style = nothing)
w, h = plt[:size]
style = "width:$(w)px;height:$(h)px;"
end
requirejs_prefix = ""
requirejs_suffix = ""
if isijulia()
requirejs_prefix = "require(['Plotly'], function (Plotly) {"
requirejs_suffix = "});"
end
uuid = UUIDs.uuid4()
html = """
<div id=\"$(uuid)\" style=\"$(style)\"></div>
<script>
$(requirejs_prefix)
PLOT = document.getElementById('$(uuid)');
Plotly.plot(PLOT, $(plotly_series_json(plt)), $(plotly_layout_json(plt)));
$(requirejs_suffix)
</script>
"""
html

View File

@ -89,6 +89,23 @@ function __init__()
use_local_dependencies[] = use_local_plotlyjs[]
if isijulia()
# require.js adds .js automatically
plotly_no_ext =
use_local_dependencies[] ? ("file:///" * plotly_local_file_path[]) : "https://cdn.plot.ly/$(_plotly_min_js_filename)"
plotly_no_ext = plotly_no_ext[1:end-3]
display("text/html", """
<script type="text/javascript">
requirejs.config({
paths: {
Plotly: '$(plotly_no_ext)'
}
});
</script>
""")
end
@require FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" begin
_show(io::IO, mime::MIME"image/png", plt::Plot{<:PDFBackends}) = _show_pdfbackends(io, mime, plt)