def __hrepr_resources__(cls, H): return [ H.javascript( export="plotly", src="https://cdn.plot.ly/plotly-latest.min.js", ), H.javascript( """ function make_plot(element, data) { return plotly.newPlot(element, data); } """, require="plotly", export="make_plot", ), ]
def test_javascript_tag_req_list(): assert sht( H.javascript("xxx='hello';", require=["abc", "d/ef"], export="xxx") ) == H.script( "define('xxx', ['abc', 'd/ef'], (abc, ef) => {", "xxx='hello';", "\nreturn xxx;});", "require(['xxx'], _ => {});", ).fill( resources=_reqjs )
def test_javascript_tag_lazy(): assert sht( H.javascript("xxx='hello';", require="abc", export="xxx", lazy=True) ) == H.script( "define('xxx', ['abc'], (abc) => {", "xxx='hello';", "\nreturn xxx;});", "", ).fill( resources=_reqjs )
def __hrepr_resources__(cls, H): return [ H.javascript( src= "https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.17.0/cytoscape.min.js", export="cytoscape", ), H.javascript( """ function make_graph(element, options) { cytoscape({ container: element, elements: options.data, style: options.style, layout: {name: options.layout} }); } """, require="cytoscape", export="make_graph", ), ]
def test_javascript_tag_req_dict(): assert sht( H.javascript( "xxx='hello';", require={"abc": "A", "d/ef": "B"}, export="xxx" ) ) == H.script( "define('xxx', ['abc', 'd/ef'], (A, B) => {", "xxx='hello';", "\nreturn xxx;});", "require(['xxx'], _ => {});", ).fill( resources=_reqjs )
def test_constructed_element(): assert sht( H.div["chapeau"](id="melon", constructor="fou", options={"x": 1},) ) == H.inline( H.div["chapeau"](id="melon"), sht( H.javascript( "new fou(document.getElementById('melon'), {\"x\": 1});", require="fou", lazy=False, ) ), )
def __hrepr_resources__(cls, H): js_requires = { name: { "url": x, "varname": name } if isinstance(x, str) else x for name, x in cls.js_requires.items() } reqs = [ H.javascript(export=name, src=data["url"]) for name, data in js_requires.items() ] if cls.js_code: main = H.javascript( cls.js_code, require={ name: data["varname"] for name, data in js_requires.items() }, ) else: main = H.javascript(src=cls.js_source) return [*reqs, main(export=cls.js_constructor)]
def test_constructed_special_element(): assert sht( H.atom( id="melon", type="cool", constructor="fou", options={"x": 1}, export="everywhere", ) ) == H.inline( H.span["hreprt-cool"](id="melon"), sht( H.javascript( "let everywhere = new fou(document.getElementById('melon'), {\"x\": 1});", require="fou", export="everywhere", lazy=False, ) ), )
def call(*args): code = self._getcode(method_name, args) if self._return_hrepr: return H.javascript(code) else: self._session.queue(command="eval", value=code)
def test_javascript_tag(): assert sht(H.javascript(export="blah", src="thing.js")) == H.script( 'requirejs.config({paths: {"blah": "thing.js?noext"}});' ).fill(resources=_reqjs)
def test_javascript_tag_noexport(): assert sht(H.javascript("xxx='hello';", require="abc")) == H.script( "require(['abc'], (abc) => {", "xxx='hello';", "});", ).fill(resources=_reqjs)