pyjs is a Rich Internet Application (RIA) Development Platform for both Web and Desktop. With pyjs you can write your JavaScript-powered web applications entirely in Python.
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)
there are two projects that allow an "obvious" transition between python objects and javascript objects, with "obvious" translations from int or float to Number and str or unicode to String: PyV8 and, as one writer has already mentioned: python-spidermonkey.
there are actually two implementations of pyv8 - the original experiment was by sebastien louisel, and the second one (in active development) is by flier liu.
my interest in these projects has been to link them to pyjamas, a python-to-javascript compiler, to create a JIT python accelerator.
so there is plenty out there - it just depends what you want to do.
相当多的 Google App Engine 项目都使用了这个库。 Joose 和 FirePython 都包含 jsonpickle。
If your just interested in sharing complex data types between javascript and python, check out jsonpickle. It wraps the standard Python JSON libraries, but has some smarts in serializing and deserializing Python classes and other data types.
Quite a few Google App Engine projects have used this library. Joose and FirePython both incorporate jsonpickle.
import { py, python } from 'pythonia'
const np = await python('numpy')
const plot = await python('matplotlib.pyplot')
// Fixing random state for reproducibility
await np.random.seed(19680801)
const [mu, sigma] = [100, 15]
// Inline expression evaluation for operator overloading
const x = await py`${mu} + ${sigma} * ${np.random.randn(10000)}`
// the histogram of the data
const [n, bins, patches] = await plot.hist$(x, 50, { density: true, facecolor: 'g', alpha: 0.75 })
console.log('Distribution', await n) // Always await for all Python access
await plot.show()
python.exit()
通过 CommonJS(没有顶级等待):
const { py, python } = require('pythonia')
async function main() {
const np = await python('numpy')
const plot = await python('matplotlib.pyplot')
...
// the rest of the code
}
main().then(() => python.exit()) // If you don't call this, the process won't quit by itself.
Many of the examples are years out of date and involve complex setup. You can give JSPyBridge a try (full disclosure: I'm the author).
It's a bi-directional bridge that lets you use JavaScript code from Python, and vice-versa. That means that Python code can call JS callbacks, and JS code can call Python callbacks.
Access Python from JS, numpy + matplotlib example, with the ES6 import system:
import { py, python } from 'pythonia'
const np = await python('numpy')
const plot = await python('matplotlib.pyplot')
// Fixing random state for reproducibility
await np.random.seed(19680801)
const [mu, sigma] = [100, 15]
// Inline expression evaluation for operator overloading
const x = await py`${mu} + ${sigma} * ${np.random.randn(10000)}`
// the histogram of the data
const [n, bins, patches] = await plot.hist$(x, 50, { density: true, facecolor: 'g', alpha: 0.75 })
console.log('Distribution', await n) // Always await for all Python access
await plot.show()
python.exit()
Through CommonJS (without top level await):
const { py, python } = require('pythonia')
async function main() {
const np = await python('numpy')
const plot = await python('matplotlib.pyplot')
...
// the rest of the code
}
main().then(() => python.exit()) // If you don't call this, the process won't quit by itself.
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.
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).
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:
Many of these projects mentioned above are dead or dying, lacking activity and interest from author side. Interesting to follow how this area develops.
For the record, in era of plugin based implementations, KDE camp had an attempt to solve this with plugin and non-language specific way and created the Kross https://en.wikipedia.org/wiki/Kross_(software) - in my understanding it never took off even inside the community itself.
During this chicken and egg -problem time, javascript-based implementions are definately way to go. Maybe in the future we seee pure and clean, full Python support natively in browsers.
It allows for total compatibility of all Python code with Node.js, including the binary modules. Unlike the other solutions - and there are plenty - this one does not spawn a separate Python process. Both Python and Node.js co-exist in the same shared memory process and transition between the two is very fast. You can pass objects and functions. It is made for using Python libraries from Node.js, your main program must be Node.js.
发布评论
评论(14)
pyjs 怎么样?
从上述网站:
How about pyjs?
From the above website:
这是一个围绕 SeaMonkey Javascript 解释器的 Python 包装器... http://pypi.python.org /pypi/python-spidermonkey
Here's something, a Python wrapper around the SeaMonkey Javascript interpreter... http://pypi.python.org/pypi/python-spidermonkey
这个问题并不新鲜,但已经出现了一些替代方案:
This question is not exactly young, but there have come up some alternatives:
有两个项目允许 python 对象和 javascript 对象之间的“明显”转换,从 int 或 float 到 Number 以及 str 或 unicode 到 String 的“明显”转换: PyV8 并且,正如一位作家已经提到的: python-蜘蛛猴。
实际上 pyv8 有两种实现 - 最初的实验是由 sebastien louisel 进行的,第二个实验(正在积极开发中)是由 flier liu 进行的。
我对这些项目的兴趣是将它们链接到 pyjamas(一个 python-to-javascript 编译器),以创建 JIT python 加速器。
所以有很多东西——这只是取决于你想做什么。
there are two projects that allow an "obvious" transition between python objects and javascript objects, with "obvious" translations from int or float to Number and str or unicode to String: PyV8 and, as one writer has already mentioned: python-spidermonkey.
there are actually two implementations of pyv8 - the original experiment was by sebastien louisel, and the second one (in active development) is by flier liu.
my interest in these projects has been to link them to pyjamas, a python-to-javascript compiler, to create a JIT python accelerator.
so there is plenty out there - it just depends what you want to do.
如果您只是对在 javascript 和 python 之间共享复杂数据类型感兴趣,请查看 jsonpickle 。 它包装了标准的 Python JSON 库,但在序列化和反序列化 Python 类和其他数据类型方面具有一些智能。
相当多的 Google App Engine 项目都使用了这个库。 Joose 和 FirePython 都包含 jsonpickle。
If your just interested in sharing complex data types between javascript and python, check out jsonpickle. It wraps the standard Python JSON libraries, but has some smarts in serializing and deserializing Python classes and other data types.
Quite a few Google App Engine projects have used this library. Joose and FirePython both incorporate jsonpickle.
许多示例已经过时多年并且涉及复杂的设置。 您可以尝试 JSPyBridge (全面披露:我是作者)。
它是一个双向桥接器,可让您使用 Python 中的 JavaScript 代码,反之亦然。 这意味着Python代码可以调用JS回调,JS代码也可以调用Python回调。
从 JS 访问 Python,numpy + matplotlib 示例,使用 ES6 导入系统:
通过 CommonJS(没有顶级等待):
从 python 访问 JS:
Many of the examples are years out of date and involve complex setup. You can give JSPyBridge a try (full disclosure: I'm the author).
It's a bi-directional bridge that lets you use JavaScript code from Python, and vice-versa. That means that Python code can call JS callbacks, and JS code can call Python callbacks.
Access Python from JS, numpy + matplotlib example, with the ES6 import system:
Through CommonJS (without top level await):
Access JS from python:
PyExecJS 能够使用 PyV8、Node、JavaScriptCore、SpiderMonkey、JScript 中的每一个。
PyExecJS is able to use each of PyV8, Node, JavaScriptCore, SpiderMonkey, JScript.
使用 Js2Py 将 JavaScript 翻译为 Python,这是唯一可用的工具:)
Use Js2Py to translate JavaScript to Python, this is the only tool available :)
有一个基于 JavaScriptCore(来自 WebKit)的桥,但它相当不完整:
http://code.google.com/p/pyjscore/
There's a bridge based on JavaScriptCore (from WebKit), but it's pretty incomplete:
http://code.google.com/p/pyjscore/
您可能还想查看 PyPy 项目 - 他们有一个 Python 到(任何东西)的编译器,包括 Python 到 Javascript、C 和 llvm。 这允许您用 Python 编写代码,然后根据需要将其编译为 Javascript。
http://codespeak.net/pypy
另外,请查看信息丰富的博客:
http://morepypy.blogspot.com/
但不幸的是,您无法通过这种方式将 Javascript 转换为 Python。 总的来说,它似乎运行得很好,他们曾经有一个 Javascript(由编译的 Python 制成)版本的 Bub'n'Bros 在线游戏(尽管服务器已经关闭了一段时间)。
http://bub-n-bros.sourceforge.net
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
我前一段时间在玩 Pyjon,似乎设法直接用 Python 编写 Javascript 的 eval 并运行简单的程序......尽管它不是 JS 的完整实现,而是一个实验。
在此处获取:
http://code.google.com/p/pyjon/
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/
上面提到的许多项目已经死亡或垂死,缺乏作者方面的活动和兴趣。 关注这个领域的发展很有趣。
根据记录,在基于插件的实现时代,KDE 阵营尝试通过插件和非语言特定的方式来解决这个问题,并创建了 Kross https://en.wikipedia.org/wiki/Kross_(software) - 据我了解,即使在社区内部,它也从未成功。
在这个先有鸡还是先有蛋的问题时期,基于 javascript 的实现绝对是最佳选择。 也许将来我们会在浏览器中看到纯粹、干净、完整的 Python 支持。
Many of these projects mentioned above are dead or dying, lacking activity and interest from author side. Interesting to follow how this area develops.
For the record, in era of plugin based implementations, KDE camp had an attempt to solve this with plugin and non-language specific way and created the Kross https://en.wikipedia.org/wiki/Kross_(software) - in my understanding it never took off even inside the community itself.
During this chicken and egg -problem time, javascript-based implementions are definately way to go. Maybe in the future we seee pure and clean, full Python support natively in browsers.
您还可以使用 XPCOM,例如在基于
XUL
的应用程序中,例如 Firefox、Thunderbird 或 Komodo。You could also use
XPCOM
, say inXUL
based apps like Firefox, Thunderbird or Komodo.我还推荐您 pymport。
它允许所有 Python 代码与 Node.js 完全兼容,包括二进制模块。 与其他解决方案(还有很多)不同,这个解决方案不会生成单独的 Python 进程。 Python 和 Node.js 共存于同一个共享内存进程中,并且两者之间的转换非常快。 您可以传递对象和函数。 它是为使用 Node.js 中的 Python 库而设计的,您的主程序必须是 Node.js。
然后简单地用 JavaScript 编写:
(我是这个模块的作者)
I would also recommend you pymport.
It allows for total compatibility of all Python code with Node.js, including the binary modules. Unlike the other solutions - and there are plenty - this one does not spawn a separate Python process. Both Python and Node.js co-exist in the same shared memory process and transition between the two is very fast. You can pass objects and functions. It is made for using Python libraries from Node.js, your main program must be Node.js.
then simply in JavaScript:
(I am the author of this module)