javascript 是否可以在 chrome 中运行本机 python 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16409138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:32:32  来源:igfitidea点击:

Is it possible to run native python code in chrome?

javascriptpythongoogle-chromesandboxbrython

提问by Marco Pashkov

I was wondering if it is possible to run native python code in chromeinstead of javascript. I have seen projects like pyjsor brython, but what they do is compile python to javascript. While those are great projects a problem with this approach is always that you cannot easily debug the original python code.

我想知道是否可以在 chrome而不是 javascript 中运行本机 python 代码。我见过像pyjsbrython这样的项目,但他们所做的是将 python 编译为 javascript。虽然这些都是很棒的项目,但这种方法的一个问题始终是您无法轻松调试原始 Python 代码。

Amongst other things I stumbled upon this thread, describing how to develop custom code for chromes-sandbox. But this would probably lead to implementing a python interpreter in the sandbox, which would be an overkill.

我偶然发现了这个线程,其中描述了如何为 chromes-sandbox 开发自定义代码。但这可能会导致在沙箱中实现 python 解释器,这将是一种矫枉过正。

Any help is appreciated! (I don't want to start any python vs. javascript discussions, but only find out if this would be possible and if so, how to do it)

任何帮助表示赞赏!(我不想开始任何 python 与 javascript 的讨论,但只是想知道这是否可行,如果可行,如何做)

Kind Regards,
Marco

亲切的问候,
马可

回答by Benjamin Gruenbaum

Python is a programming language, you can't run native codeof a programming language. You can however, run programs written in python in the browser.

Python 是一种编程语言,你不能运行一种编程语言的本机代码。但是,您可以在浏览器中运行用 Python 编写的程序。

So can I run python code in the browser or not?

那么我可以在浏览器中运行 python 代码吗?

Update June 2013:The Brython project is now available letting you run Python 3 code in your browser. You can check it out at http://www.brython.info/.

2013 年 6 月更新:Brython 项目现在可以让您在浏览器中运行 Python 3 代码。您可以在http://www.brython.info/ 上查看

You can use run LLVM in JavaScript using ECMAScripten. This means you can compile c-python from C to JavaScript and just run JS in the browser. The link to the ECMAScripten wiki provides more details on how to do that.

您可以使用ECMAScripten 在 JavaScript 中运行 LLVM。这意味着您可以将 c-python 从 C 编译为 JavaScript,然后在浏览器中运行 JS。ECMAScripten wiki 的链接提供了有关如何执行此操作的更多详细信息。

(Here is a live demoof a python REPL running in the browser)

(这是在浏览器中运行的 python REPL的现场演示

There also exist python 2implementations that work in the browser.

还存在在浏览器中工作的python 2实现。

But should I?

但我应该吗?

Probably not, JavaScript and python are similar in many things, both are dynamic, both are compact and both are clever. If you know python you can learn JavaScript very quickly.

可能不是,JavaScript 和 python 在很多方面都很相似,都是动态的,都很紧凑,而且都很聪明。如果您了解 Python,您可以非常快速地学习 JavaScript。

If you like python's syntax, you might want to consider CoffeeScriptwhich has similar syntax to Python (and somewhat to Ruby) and compiles to JavaScript transparently.

如果您喜欢 Python 的语法,您可能需要考虑CoffeeScript,它具有与 Python 相似的语法(有点类似于 Ruby)并且可以透明地编译为 JavaScript。

Solutions that run python in the browser will also tend to be much slower, at least until more browsers will support asm.js (currently only firefox nightly).

在浏览器中运行 python 的解决方案也往往会慢得多,至少在更多浏览器支持 asm.js 之前(目前只有 firefox nightly)。

回答by Jacob Riplow

I believe you can create a compiler in Javascript, to run simple python code. There are probably some available programs as well that will allow this to be carried out. Although, it is not possible to run python directly through a web browser.

我相信你可以在 Javascript 中创建一个编译器来运行简单的 python 代码。可能还有一些可用的程序可以执行此操作。虽然,无法直接通过 Web 浏览器运行 python。