How can I load Raphael in an IPython laptop, avoiding some of the problems that come with require.js?

On an IPython laptop, you can expect the following code to force Raphael.js to successfully boot into the global namespace.

from IPython.display import Javascript raphael_url = "https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js" Javascript('alert(Raphael);', lib=[raphael_url]) 

However, it does not work in recent versions of IPython that use require.js. Turns out Raphael.js, which loads IPython using jQuery.getScript() , recognizes the presence of require.js and does not by itself insert itself into the global namespace. In fact, if javascript code is first executed that removes the window.define object, Raphael no longer implements require.js, and it inserts itself into the global namespace as I would like. In other words, the above code works after doing the following:

 Javascript('window.define = undefined;') 

Thus, the only way I can get Raphael to boot in the latest IPython laptop is to remove (or delay) window.define .

Having identified the problem, I am not sufficiently aware that require.js knows which piece of software acts against the protocol. Does Raphael use a poor require.js test for existence? Should IPython use require.js directly instead of jQuery.getScript() when it loads the user-provided javascript libraries? Or is there a way I have to use the require.js user who will give me a Raphael object without any special hacks? (If the answer to the last question is yes, is there a way I can also support older versions of IPython laptops that don't use require.js?)

+6
source share
1 answer

You won’t like the first part of my answer, but the loading and requirement of the javascript library in IPython-notebook-webapp has not yet been resolved, so at the moment I would suggest not building a lot under the assumption that you can load such a library and rely more on custom.js .

However, if raphael is not in the global namespace, it’s smart enough to cache it and give you a link to it. Then in the callback you can simply assign to global:

require (['raphael'], function (raph) {window.raphael = raph;})

Or something like that should do the trick.

+4
source

Source: https://habr.com/ru/post/956382/


All Articles