13

I was wondering if it is possible to run native python code in chrome instead of javascript. I have seen projects like pyjs or 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.

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.

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)

Kind Regards,
Marco

Community
  • 1
  • 1
m3o
  • 3,881
  • 3
  • 35
  • 56

3 Answers3

16

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

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

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

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.

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

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

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.

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

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

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Thanks for your response Benjamin! I know you probably should not do that for quite some reasons, but if there's an easy way I would take that. I develop in many languages and javascript is probably one of the easiest languages to learn, but I don't like some of it's concepts(e.g.: how classes are implemented - inheritance is tough), which is why I just wanted to see if I can make something work with python. As I said, I don't want to start any discussion, but see what's out there and how I can leverage it. – m3o May 07 '13 at 00:13
  • Inheritance in JavaScript is _dead simple_ , I love how python handles class based inheritance but JavaScript is simpler imho. `var some2dPoint = {x:3,y:5}; var now3d = Object.create(some2dPoint);now3d.z=10`. Defiantly check out CoffeeScript, it does classes in a way that'll make you feel more at home Here's a tutorial on classes in CS: http://arcturo.github.io/library/coffeescript/03_classes.html . Also check out https://code.google.com/p/brython/ although I couldn't get it to work properly with an existing code base. I _really_ think you could learn to enjoy JavaScript, just me though. – Benjamin Gruenbaum May 07 '13 at 00:17
  • Also, it is worth mentioning that while you are the original poster and who I'm answering to, even if you're not interested in stuff like _why should I_ SO is a community site and I think a lot of people who'll look up running Python in the browser should be aware that it is not always the best idea. I think JS is very competent (some would say as much as python, or even more) and I think you'll really like it if you give it a try, it does things _differently_ from python but it is designed for different things. The browser is an asynchronous and dynamic environment and JS is built for _that_ – Benjamin Gruenbaum May 07 '13 at 00:19
  • it does not, yet. still doing more research – m3o May 10 '13 at 18:20
  • Actually the idea of Brython covers the same use cases as coffescript, with the difference that (1) it does not invent a new language, (2) as of 2014, it really exposes almost all Python3 standard library - which has tons of niceties in it which in javascript would require one to fetch tens of other projects to have available. – jsbueno Nov 25 '14 at 20:18
0

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.

Jacob Riplow
  • 9
  • 1
  • 6
  • There are already several Python interpreters in JavaScript, including [PyPy.js](http://pypyjs.org/) and [Skulpt](http://www.skulpt.org/). – Anderson Green Mar 13 '18 at 22:52
0

Now , it's possible . you can run python in browser

Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43