Loading JavaScript library in Python using PyV8

二次信任 提交于 2019-12-12 12:23:07

问题


I am trying to use some features of the leaflet.js library in my Python code. And in order to execute JS commands in the code I am using PyV8. But I am unable to load the leaflet.js library into PyV8 context. When I do this,

ctxt.eval(open("leaflet.js").read())

I get the error:

ReferenceError: window is not defined (  @ 9 : 68 )  -> of t.coords[c]&&(u[c]=t.coords[c]);this.fire("locationfound",u)}})}(window,doc

I even tried to load the jquery library before thinking maybe leaflet is dependent on it. But then I get the error,

TypeError: Cannot read property 'defaultView' of undefined (  @ 2 : 68 )  -> :!1},k=db.setDocument=function(a){var b,e=a?a.ownerDocument||a:t,g=e.defaultVi

Is there a way I can successfully load JS library?


回答1:


You can only use libraries that are not depending on the DOM, since it is obviously not available server side. One example of such a universally usable library is mustache.js.

Edit: Looks like there is a way, apparently leaflet's can still be useful without the DOM - one simply has to somewhat recreate DOM first, as this nice fellow shows here: https://github.com/rclark/server-side-leaflet. Note: not sure whether this works together with PyV8, you'll have to just try.

Edit2: To extend a little bit on the previous point: What you get with PyV8 is a pure non-browser-based javascript runtime environment. It has no idea about what 'window' or 'document' is, since V8 is not concerned about UI. Leaflet needs the DOM to operate, so it needs to 'think' it lives inside a browser. rclark's server side leaflet needs node.js specific extensions, so it won't work in PyV8.

There's this javascript DOM implementation that might work in PyV8: https://github.com/andreasgal/dom.js/. You'd have to load all the js sources provided there into PyV8 in the right order - note that this library only provides instructions for spidermonkey, not V8. I.e. this is going to be tricky to get right.

So, as I see it you have the following options (in order of increasing complexity and/or skill required):

  1. Someone has apparently solved this problem for python in the following thread, but apparently using some rectangular approximation using OTR - see his github link in the comments to the accepted answer. I'd check this out and decide whether the approximation is good enough. Lookup country for GPS coordinates without Internet access
  2. Switch to a browser based environment and use leaflet directly.
  3. Switch to node.js and use rclark's port.
  4. Try to somehow get a DOM into your PyV8, possibly using andreasgal and then use leaflet from there.
  5. Adapt leaflet for your needs such that it doesn't have any DOM dependencies anymore (start with shielding anything using window and/or document in something like if (typeof(window) === 'object' && window !== null){...}. If you get this working, I'd make a pull request to the leaflet project since many others might profit from your work.


来源:https://stackoverflow.com/questions/23098477/loading-javascript-library-in-python-using-pyv8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!