Compiling trivial python program to javascript using cython and emscripten on mac
Asked Answered
H

3

5

I am trying to generate javascript from python using cython and emscripten.

hello.py:

print 'Hello world.'

Then I compile this to c using cython

>>> cython --embed hello.py -v

This generates a hello.c file which I compile with

>>> gcc hello.c -I/usr/include/python2.7/ -lpython2.7

This works for gcc or clang. When I execute ./a.out I get the expected output

>>> ./a.out
>>> Hello world

next I want to compile hello.c to javascript using emscripten

>>> emcc hello.c -I/usr/include/python2.7/ -lpython2.7

I get

>>> WARNING  emcc: -I or -L of an absolute path encountered. 
>>> If this is to a local system header/library, it may cause problems 
>>> (local system files make sense for compiling natively on your system, 
>>> but not necessarily to JavaScript)
>>> clang: warning: argument unused during compilation: '-nostdinc++'

It still generates a a.out.js file which I try to run in node.js

>>> node a.out.js

I get a reference error

>>> ReferenceError: _Py_SetProgramName is not defined

I tried changing the generated javscript a little bit, but basically I think all the _Py_ functions are not defined.

Does anyone have any experience with this, or any suggested fixes?

Holofernes answered 12/6, 2013 at 11:15 Comment(9)
Whoa. Python to Javascript via C? Would be interesting to know what your usecase is?Cavour
physics simulation written in python with numpy that I'd love get onto browsers. Also I'd love to write the vector manipulations with numpy and visualizations with Three.js, but thats very wishful thinking!Holofernes
Numpy will be very tough to port to javascript. It uses some native libraries written in FORTRAN.Cavour
Advice is to run the simulation on the server and update the visualisation via a websocket. I've done an identical thing in the past where WebGL was just a presentation frontend to a physics engine running on the server using python, sockJS and a tornado reactor as a server. Worked pretty well.Cavour
Perhaps, your probably right, thanks for the advice.Holofernes
If your code is online somewhere (github,bitbucket), I'd love to take a look, it sounds interesting.Cavour
Its a sim of a quantum optics experiment. I think two weeks and ill get it on github.Holofernes
Did you ever get this to work, @EoinMurray?Paba
Not really, I had included numpy and simpy in the simulation and the have fortran dependencies so I gave up on compiling the python. On the other hand I did end up re-writing most of it in javascript and webGL, the result is [here](eoinmurray.io/icarusjs)Holofernes
C
6

You will need to compile the embedabble python library -lpython2.7 to javacsript too so that it is available for your javacsript program.

Thankfully, the work to do this has already been done in empythoned. Which provides an embedded python compiled to Javascript.

You should be able to use empythoned to provide the missing _Py_SetProgramName

Cavour answered 12/6, 2013 at 11:34 Comment(0)
H
2

To make it works I think you need whole Python compiled by emcc to JavaScript to have proper libraries compiled into code that node.js can handle. Otherwise binary libraries you have remain intact. You cannot mix that.

In fact emcc informs you about it with the warning if you read it carefully.

You need to find out how to cross-compile Python into javascript prior to compiling your own scripts. This is done already, because I saw it on repl.it.

Harvester answered 12/6, 2013 at 11:25 Comment(0)
U
0

Pyodide has finally (many years later) properly solved this problem. See https://pyodide.org/en/stable/ Many of the components of Pyodide are written partly using Cython.

Unseat answered 31/7, 2022 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.