Best way to integrate Python and JavaScript?

2019-01-05 09:29发布

Is it possible to integrate Python and JavaScript? For example, imagine you wanted to be able to define classes in JavaScript and use them from Python (or vice versa). If so, what's the best way? I'm interested not only if this is possible but if anyone has done it within a "serious" project or product.

I'm guessing it would be possible using Jython and Rhino, for one example, but I'm curious whether or not anyone's ever actually done this, and if there are solutions for other platforms (especially CPython).

12条回答
做个烂人
2楼-- · 2019-01-05 09:51

There's a bridge based on JavaScriptCore (from WebKit), but it's pretty incomplete: http://code.google.com/p/pyjscore/

查看更多
SAY GOODBYE
3楼-- · 2019-01-05 09:51

another possibility is to use XPCOM, say in XUL based apps (like firefox, thunderbird, komodo etc.)

查看更多
叛逆
4楼-- · 2019-01-05 09:52

I was playing with Pyjon some time ago and seems manage to write Javascript's eval directly in Python and ran simple programs... Although it is not complete implementation of JS and rather an experiment. Get it here:

http://code.google.com/p/pyjon/

查看更多
三岁会撩人
5楼-- · 2019-01-05 09:54

Use Js2Py to translate JavaScript to Python, this is the only tool available :)

查看更多
一夜七次
6楼-- · 2019-01-05 09:56

You might also want to check out the PyPy project - they have a Python to (anything) compiler, including Python to Javascript, C, and llvm. This allows you to write your code in Python and then compile it into Javascript as you desire.

http://codespeak.net/pypy

Also, check out the informative blog:

http://morepypy.blogspot.com/

Unfortunately though, you can't convert Javascript to Python this way. It seems to work really well overall, they used to have a Javascript (made from compiled Python) version of the Bub'n'Bros game online (though the server has been down for a while).

http://bub-n-bros.sourceforge.net

查看更多
Anthone
7楼-- · 2019-01-05 10:08

This question is not exactly young, but there have come up some alternatives:

  • "Skulpt is an entirely in-browser implementation of Python."
  • Brython - "A Python 3 implementation for client-side web programming"
  • RapydScript - "Python-like JavaScript without the extra overhead or quirks"
  • Transcrypt - "Lean and mean Python 3.6 to JavaScript compiler with multiple inheritance, sourcemaps, static type checking and selective operator overloading." (also on Github)
查看更多
登录 后发表回答