Skip to content

Commit

Permalink
ENH: clean up html_body(::JuptyerPlot)
Browse files Browse the repository at this point in the history
- use requirejs.specified to see if 'plotly' dep is defined. If so, use
it
- Only unload plot data and json once and reuse in both cdn and local
based Plotly.newPlot
-  wrap plot code in anonymous function so unpacked data and layout
variables don't become globally defined
  • Loading branch information
sglyon committed Feb 6, 2018
1 parent 5cf0d6c commit 20a3c98
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/displays/ijulia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,44 @@ js_loaded(::JupyterDisplay) = _jupyter_js_loaded[1]
js_loaded(::Type{JupyterDisplay}) = _jupyter_js_loaded[1]

function html_body(p::JupyterPlot)
lowered = JSON.lower(p.plot)
suffix = split(string(p.plot.divid, "-"), "-")[1]
unpack_json = """
var data_$(suffix) = $(lowered[:data]);
var layout_$(suffix) = $(lowered[:layout]);
"""

plot_code = """
Plotly.newPlot('$(p.plot.divid)',
data_$(suffix), layout_$(suffix),
{showLink: false}
);
"""

"""
<div id="$(p.view.divid)" class="plotly-graph-div"></div>
<script>
if (window.Plotly == undefined) {
requirejs.config({
paths: {
'plotly2': ['https://cdn.plot.ly/plotly-latest.min']
},
});
require(['plotly2'], function(Plotly) {
$(script_content(p.plot))
});
} else {
$(script_content(p.plot))
}
(function() {
$(unpack_json)
if (!requirejs.specified('plotly')) {
requirejs.config({
paths: {
'plotly_cdn': ['https://cdn.plot.ly/plotly-latest.min']
},
});
require(['plotly_cdn'], function(Plotly) {
$(plot_code)
});
} else {
require(['plotly'], function(Plotly) {
$(plot_code)
if (window.Plotly == undefined) {
window.Plotly = Plotly;
}
});
}
})();
</script>
"""
end
Expand Down

0 comments on commit 20a3c98

Please sign in to comment.