使用PyV8在Python中加载JavaScript库 [英] Loading JavaScript library in Python using PyV8

查看:987
本文介绍了使用PyV8在Python中加载JavaScript库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Python代码中使用leaflet.js库的一些功能。为了在代码中执行JS命令,我使用了PyV8。但我无法将leaflet.js库加载到PyV8上下文中。当我这样做时,

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

我甚至试图在加载jquery库之前,也许单张依赖于它,但后来我得到错误,

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

有没有一种方法可以成功加载JS库?

Is there a way I can successfully load JS library?

推荐答案

只能使用不依赖的库在DOM上,因为它显然是不可用的服务器端。这样一个普遍可用的库的例子是mustache.js。

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.

编辑:看起来有一种方法,显然,传单在没有DOM的情况下仍然很有用 - 只需要首先稍微重新创建DOM,就像这位好友在这里展示的那样: https://github.com/rclark/server-side-leaflet 。注意:不确定这是否与PyV8一起使用,您只需要尝试一下。

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:在前一点上扩展一点: PyV8是一个纯粹的非基于浏览器的JavaScript运行时环境。它不知道'窗口'或'文档'是什么,因为V8不关心用户界面。 Leaflet需要DOM来操作,所以它需要思考它是否在浏览器中。 rclark的服务器端传单需要node.js特定的扩展名,所以它在PyV8中不起作用。

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.

这个JavaScript DOM实现可能工作在PyV8中: https://github.com/andreasgal/dom.js/ 。您必须以正确的顺序将所有提供的js源文件加载到PyV8中 - 请注意,该库仅提供spidermonkey的指示信息,而不是V8。即这是很难得到正确的。

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. 有人显然在下面的线程中解决了python的这个问题,但显然使用了一些使用OTR的矩形近似 - 在接受的答案中看到他的github链接。我会检查这一点,并确定近似是否足够好。 没有互联网接入的GPS坐标查找国家

  2. 切换到基于浏览器的环境并直接使用传单。

  3. 切换到node.js并使用rclark的端口。尝试以某种方式将DOM引入PyV8,可能使用andreasgal,然后使用传单。

  4. 为您的需要调整传单以使其不再具有任何DOM依赖关系屏蔽任何使用窗口和/或文件的东西,如 if(typeof(window)==='object'&& window!== null){...} 。如果你能做到这一点,我会向传单项目发出拉请求,因为许多其他人可能会从你的工作中获利。

  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.

这篇关于使用PyV8在Python中加载JavaScript库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆