Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile Python with Brython to Javascript (to run with nodejs)?

How can I use Brython to compile Python to Javascript? I want to do this on my computer, so I can the run Javascript with nodejs, eg.

$ python hello.py
Hello world
$ brython hello.py -o hello.js
$ node hello.js
Hello world

The examples on the Brython website only explain how do this in the browser http://www.brython.info/index_en.html

like image 651
Colonel Panic Avatar asked Feb 20 '13 13:02

Colonel Panic


2 Answers

It seems they are very browser oriented, there is no command line option out of the box.

You can try to use their code youself from node.js, perhaps it will work easily. It seems the $py2js(src, module) function does the actual conversion so maybe you can just run it with the python code string as first parameter.

Another option is to use pyjs: http://pyjs.org/ which does something similar and has command line tool to do the conversion.

like image 112
Iftah Avatar answered Nov 03 '22 05:11

Iftah


It is possible to compile Python code to javascript and load it afterwards using import statement . See brython:ticket:222 for further details. You'll have to load brython js lib in advance because , in the end, Python semantics are quite different from Javascript's . You can include compiled .pyc.js code in .vfs.js files in order to speed up module import times.

Disclaimer : I'm a committer of Brython project .

like image 35
Olemis Lang Avatar answered Nov 03 '22 05:11

Olemis Lang