Skip to content Skip to sidebar Skip to footer

Loading JavaScript Library In Python Using 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.j

Solution 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.

Post a Comment for "Loading JavaScript Library In Python Using PyV8"