Created
January 16, 2019 08:44
-
-
Save elbakramer/aae835249d4da5b9e5ddf91b1305a96a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
import json | |
class Plotly(object): | |
@staticmethod | |
def newPlot(data, layout=None, config=None): | |
scriptUrl = 'https://cdn.plot.ly/plotly-latest.min.js' | |
divId = str(uuid.uuid1()) | |
divTag = '<div id="%(divId)s"></div>' % { 'divId': divId } | |
if not isinstance(data, list): | |
data = list(data) | |
arguments = [divId, data, layout, config] | |
arguments = [json.dumps(arg) for arg in arguments if arg] | |
arguments = ','.join(arguments) | |
script = """ | |
(function() { | |
var onLoad = function() { | |
(function() { | |
Plotly.newPlot(%(arguments)s); | |
})(); | |
(function() { | |
var gd = document.getElementById("%(divId)s"); | |
var onResize = function() { Plotly.Plots.resize(gd); }; | |
window.addEventListener("resize", onResize); | |
})(); | |
}; | |
if (typeof Plotly === "object") { | |
onLoad(); | |
} else { | |
var st = document.createElement("style"); | |
var css = ` | |
.plotly .modebar-btn { | |
display: inline-block; | |
} | |
.plotly .modebar-btn svg { | |
width: 1em; | |
height: 1em; | |
} | |
`; | |
st.type = "text/css"; | |
if (st.styleSheet) { | |
st.styleSheet.cssText = css; | |
} else { | |
st.appendChild(document.createTextNode(css)); | |
} | |
document.body.appendChild(st); | |
var sc = document.createElement("script"); | |
sc.type = "text/javascript"; | |
sc.src = "%(scriptUrl)s"; | |
sc.onload = onLoad; | |
sc.onerror = function(err) { alert(err); } | |
document.body.appendChild(sc); | |
} | |
})(); | |
""" % { 'divId': divId, 'arguments': arguments, 'scriptUrl': scriptUrl } | |
scriptTag = '<script>{script}</script>'.format(script=script) | |
print('%angular') | |
print(divTag) | |
print(scriptTag) | |
print('%text') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment