Jac*_*son 63 javascript python brython transcrypt rapydscript
是否可以集成Python和JavaScript?例如,假设您希望能够在JavaScript中定义类并从Python中使用它们(反之亦然).如果是这样,最好的方法是什么?我不仅对这是否可行感兴趣,而且如果有人在"严肃"的项目或产品中做到了这一点.
我猜这可能是使用Jython和Rhino的例子,但我很好奇是否有人真的这样做过,以及是否有其他平台的解决方案(特别是CPython).
Dav*_*d Z 20
这是一个围绕SeaMonkey Javascript解释器的Python包装器... http://pypi.python.org/pypi/python-spidermonkey
Tob*_*ler 14
这个问题并不完全年轻,但有一些替代方案:
小智 9
有两个项目允许python对象和javascript对象之间的"明显"转换,具有从int或float到Number和str或unicode到String的"明显"转换:PyV8和,正如一位作者已经提到的:python-spidermonkey.
实际上有两个pyv8的实现 - 原始实验是由sebastien louisel,第二个(在积极开发中)是由飞行员刘.
我对这些项目的兴趣是将它们连接到睡衣,一个python-to-javascript编译器,以创建一个JIT python加速器.
所以那里有很多 - 这取决于你想做什么.
如果您只是对在javascript和python之间共享复杂数据类型感兴趣,请查看jsonpickle.它包装了标准的Python JSON库,但在序列化和反序列化Python类和其他数据类型方面有一些聪明之处.
相当多的Google App Engine项目都使用了这个库. Joose和FirePython都纳入jsonpickle.
小智 6
许多示例已经过时多年并且涉及复杂的设置。您可以尝试一下JSPyBridge(完全公开:我是作者)。
它是一个双向桥接器,可让您使用 Python 中的 JavaScript 代码,反之亦然。这意味着Python代码可以调用JS回调,JS代码也可以调用Python回调。
使用 ES6 导入系统从 JS 访问 Python,numpy + matplotlib 示例:
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()
Run Code Online (Sandbox Code Playgroud)
通过 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.
Run Code Online (Sandbox Code Playgroud)
从Python访问JS:
from javascript import require, globalThis
chalk, fs = require("chalk"), require("fs")
print("Hello", chalk.red("world!"), "it's", globalThis.Date().toLocaleString())
fs.writeFileSync("HelloWorld.txt", "hi!")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
103763 次 |
| 最近记录: |