CPython on the Web

A

azakai

Hello, I hope this will be interesting to people here: CPython running
on the web,

http://syntensity.com/static/python.html

That isn't a new implementation of Python, but rather CPython 2.7.1,
compiled from C to JavaScript using Emscripten and LLVM. For more
details on the conversion process, see http://emscripten.org

This is a work in progress, main issues right now are that the code
isn't optimized (so don't expect good performance), and importing non-
static modules doesn't work. Otherwise, though, it seems to run
properly, in particular it runs all the examples in
http://wiki.python.org/moin/SimplePrograms that don't rely on
importing modules or receiving input from the user (with perhaps some
minor formatting errors). The demo runs fine on recent versions of
Firefox, Chrome and Safari, but has problems on IE9 and Opera
(hopefully those will be resolved soon).

The idea is that by compiling CPython itself, all the features of the
language are immediately present, and at the latest version, unlike
writing a new implementation which takes time and tends to lag behind.
As to why run it on the web, there could be various uses, for example
it could allow a simple learning environment for Python, which since
it's on the web can be entered immediately without any download (and
would run even in places where Python normally can't, like say an
iPad).

Feedback would be very welcome!

- azakai
 
K

Katie T

The idea is that by compiling CPython itself, all the features of the
language are immediately present, and at the latest version, unlike
writing a new implementation which takes time and tends to lag behind.
As to why run it on the web, there could be various uses, for example
it could allow a simple learning environment for Python, which since
it's on the web can be entered immediately without any download (and
would run even in places where Python normally can't, like say an
iPad).

It looks pretty neat ! - most solutions I've seen involve running
Python in a sandbox environment on the server as opposed to on the
client desktop.

Katie
 
P

python

Azakai,

WOW! That's incredible!! Thank you for sharing your work with the
community.

1. Are there plans to support IE 7 or 8?

2. I'm not sure what you mean by non-static modules? Can we use modules
such as json, pickle/cPickle, StringIO/cStringIO?

3. Is there a virtual file system we can take advantage of so calls to
open() would work?

Malcolm
 
O

Octavian Rasnita

From: "Katie T" <[email protected]>
Subject: Re: CPython on the Web

It looks pretty neat ! - most solutions I've seen involve running
Python in a sandbox environment on the server as opposed to on the
client desktop.

Katie
--


I don't understand what can be this program used for. Can anyone explain please?

Ok, I understand that it can be used for learning, which is pretty useless because I doubt that a Python newbie will start using Python and learning Python that way.

Then, how can the Python programs run on the "desktop"?
I suspect that the Python code is somehow translated to Javascript in order to run on the browser. Am I right?

If yes, then how can run a Python code that access a database or one that create a web server, or a WxPython GUI run?

If it can run just simple things that prints things in the browser, then why not writing that code directly in JS?


As you can see, there are many things I don't understand. :)

Thank you.

BTW. I have tried that page, and it appeared a JS error window telling that the JS scripts run too slow and it asked me if I want to continue.
I have executed the default Python script, but nothing happend. Nothing was printed. I use Internet Explorer.

Octavian
 
A

azakai

1. Are there plans to support IE 7 or 8?

I think it might run slowly there, but otherwise sure, it should run -
the code is intended to be valid JavaScript (if it isn't, that's a
bug). Currently though a minor issue prevents it from running on IE, I
have been told (I don't have a Windows machine to test on myself),
http://code.google.com/p/emscripten/issues/detail?id=22
2. I'm not sure what you mean by non-static modules? Can we use modules
such as json, pickle/cPickle, StringIO/cStringIO?

Sorry, I should have been more clear. There isn't support for
dlopen(), which opens dynamically linked libraries. That means that
you can import libraries like sys, which are already linked into
python. But loading a module that exists as a separate file won't work
yet (but hopefully soon).
3. Is there a virtual file system we can take advantage of so calls to
open() would work?

No, not yet, the libc implementation used just has stubs for input/
output stuff so far. Work in progress ;)

- azakai
 
A

azakai

Then, how can the Python programs run on the "desktop"?
I suspect that the Python code is somehow translated to Javascript in order to run on the browser. Am I right?

To clarify, in this demo, CPython itself - the C implementation of
Python - was translated from C to JavaScript (or more specifically, C
to LLVM, and LLVM to JavaScript). So your web browser is running the
same CPython that you would run on your computer normally.

That CPython executes Python by compiling it into bytecode, etc., and
that is exactly the same with CPython normally and CPython on the web
in this demo. So actual Python code is not translated into JavaScript
(which is the approach pyjamas takes), instead the entire interpreter
is.
If yes, then how can run a Python code that access a database or one that create a web server, or a WxPython GUI run?

By implementing whatever library functions and system calls CPython
needs, in the browser. For example, if the CPython code calls printf()
to print stuff, then we need to implement printf() in JavaScript, and
so forth.

Obviously there are limitations of the JS environment, so not
everything can be done.
BTW. I have tried that page, and it appeared a JS error window telling that the JS scripts run too slow and it asked me if I want to continue.
I have executed the default Python script, but nothing happend. Nothing was printed. I use Internet Explorer.

I've been told it doesn't run properly on IE, we have a bug open on
that, sorry. It will work on Firefox, Chrome and Safari right now.

- azakai
 
G

Gerry Reno

Hello, I hope this will be interesting to people here: CPython running
on the web,

http://syntensity.com/static/python.html

That isn't a new implementation of Python, but rather CPython 2.7.1,
compiled from C to JavaScript using Emscripten and LLVM. For more
details on the conversion process, see http://emscripten.org

This is a work in progress, main issues right now are that the code
isn't optimized (so don't expect good performance), and importing non-
static modules doesn't work. Otherwise, though, it seems to run
properly, in particular it runs all the examples in
http://wiki.python.org/moin/SimplePrograms that don't rely on
importing modules or receiving input from the user (with perhaps some
minor formatting errors). The demo runs fine on recent versions of
Firefox, Chrome and Safari, but has problems on IE9 and Opera
(hopefully those will be resolved soon).

The idea is that by compiling CPython itself, all the features of the
language are immediately present, and at the latest version, unlike
writing a new implementation which takes time and tends to lag behind.
As to why run it on the web, there could be various uses, for example
it could allow a simple learning environment for Python, which since
it's on the web can be entered immediately without any download (and
would run even in places where Python normally can't, like say an
iPad).

Feedback would be very welcome!

- azakai

Ok, visiting this page:

http://syntensity.com/static/python.html

I do not see anything happen when I click 'execute' button. I'm running
Firefox 3.6.3.

Here is what I see both before and after clicking 'execute':
=====================================

This is CPython, the standard Python <http://www.python.org>
implementation, compiled from C to JavaScript using Emscripten
<http://emscripten.org>, running in your browser (without any plugins).

* Most core language stuff should work, except for importing
non-static modules (in other words, |import sys| will work, but
other modules won't).
* Please report bugs if you find them!
* Tested on Firefox 4 and Chrome 10.
* The editor is Skywriter <https://mozillalabs.com/skywriter/>.

------------------------------------------------------------------------
*Enter some Python*:
import sys print 'Hello world! This is Python {} on
{}'.format(sys.version, sys.platform) print 'Here are some numbers:',
[2*x for x in range(5)][:4]

=====================================


So what is happening is that the whole Python interpreter has been
converted to Javascript and is running the browser, is that correct?

Ok, but the usual browser 'sandbox' constraints would still apply would
they not?

And what is the build toolchain that you need if you want to convert
your modules to be importable with this "CPython on the Web"?


Regards,
Gerry
 
A

azakai

Ok, visiting this page:

http://syntensity.com/static/python.html

I do not see anything happen when I click 'execute' button.  I'm running
Firefox 3.6.3.

I've only tested with Firefox 4. I'm surprised though that it wouldn't
work on 3.6.3. Can you see what errors appear in the error console
(control-shift-J)?

If no errors appear, it might be a failure due to limited script stack
space (which is fixed in FF4, and I guess is a problem in earlier
versions).
So what is happening is that the whole Python interpreter has been
converted to Javascript and is running the browser, is that correct?
Yes.


Ok, but the usual browser 'sandbox' constraints would still apply would
they not?

Yes, the JavaScript is limited in the usual ways. So Python is running
in a sandboxed manner.
And what is the build toolchain that you need if you want to convert
your modules to be importable with this "CPython on the Web"?

Note that loading modules isn't implemented yet, but I'll work on it
soon.

The toolchain will be to use your normal makefiles and such, but
replacing gcc with llvm-gcc or clang, so it generates LLVM bytecode
instead of a normal binary. Then one would run the generated LLVM
bytecode through Emscripten, which compiles it to JavaScript. So, the
process should be fairly simple.

- azakai
 
G

Gerry Reno

I've only tested with Firefox 4. I'm surprised though that it wouldn't
work on 3.6.3. Can you see what errors appear in the error console
(control-shift-J)?

Errors when using Firefox 3.6.3:

script stack space quota is exhausted
Module is not defined ... line 56


Regards,
Gerry
 
A

azakai

Errors when using Firefox 3.6.3:

script stack space quota is exhausted

Ah, then yeah, it's the script stack issue I was afraid of. Then
there's not really a way to run the demo on Firefox 3.6.x. It will
work on Firefox 4 though, or other recent browsers.

- azakai
 
G

Gerry Reno

I tried printing sys.path and here is the output:

['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7/',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/lib-dynload']

Now, those paths must be on your machine because they are not on my
client machine. But the interpreter is now running on MY machine. Well
in a sandbox really. So how is that going to work?


Regards,
Gerry
 
A

azakai

Azakai/Gerry,


I'm running Firefox 3.6.1.3 and the interpreter is running fine.

I'm on Windows 7 Pro 64-bit.

Malcolm

Thanks for the info. To be honest I'm surprised it works there. I
guess the error Gerry ran into depends on additional factors.

- azakai
 
W

Wolfgang Strobl

I guess that meant FIrefox 3.6.13 (without the last dot), the current
stable version.

I'm using Firefox 3.6.13 (german) on Windowx XP (32bit, german) here,
and the interpreter is running fine, too. Same for Chrome 8.0.552.224.
 
A

azakai

I tried printing sys.path and here is the output:

['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7/',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/lib-dynload']

Now, those paths must be on your machine because they are not on my
client machine.  But the interpreter is now running on MY machine.  Well
in a sandbox really.  So how is that going to work?

Yeah, those are the paths on the machine where the binary was compiled
(so, they are the standard paths on ubuntu).

Anyhow the filesystem can't (and shouldn't) be accessed from inside a
browser page. I think we will implement a minimal virtual filesystem
here, just enough for stuff to work. The actual implementation would
use HTML5 features like local storage etc.

- azakai
 
D

Diez B. Roggisch

azakai said:
Hello, I hope this will be interesting to people here: CPython running
on the web,

http://syntensity.com/static/python.html

That isn't a new implementation of Python, but rather CPython 2.7.1,
compiled from C to JavaScript using Emscripten and LLVM. For more
details on the conversion process, see http://emscripten.org

A fun hack. Have you bothered to compare it to the PyPy javascript
backend - perfomance-wise, that is?

Diez
 
G

Gerry Reno

I tried printing sys.path and here is the output:

['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7/',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/lib-dynload']

Now, those paths must be on your machine because they are not on my
client machine. But the interpreter is now running on MY machine. Well
in a sandbox really. So how is that going to work?
Yeah, those are the paths on the machine where the binary was compiled
(so, they are the standard paths on ubuntu).

Anyhow the filesystem can't (and shouldn't) be accessed from inside a
browser page.

Well, the local filesystem could be accessible with the user's
permission and this should be an option.


Regards,
Gerry
 
G

Gerry Reno

A fun hack. Have you bothered to compare it to the PyPy javascript
backend - perfomance-wise, that is?

Diez

I don't think that exists anymore. Didn't that get removed from PyPy
about 2 years ago?


Regards,
Gerry
 
D

Diez B. Roggisch

Gerry Reno said:
I don't think that exists anymore. Didn't that get removed from PyPy
about 2 years ago?

Ah, didn't know that. I was under the impression pyjamas was done with
it. Apparently, that's wrong:

http://pyjs.org/

But then I re-phrase my question: how does this relate to pyjamas/pyjs?

Diez
 
G

Gerry Reno

Ah, didn't know that. I was under the impression pyjamas was done with
it. Apparently, that's wrong:

http://pyjs.org/

But then I re-phrase my question: how does this relate to pyjamas/pyjs?

Diez
From what I've seen so far:

Pyjamas is taking your python code and converting it into javascript so
that your python code (converted to javascript) can run in a browser.

CPotW is taking the whole python interpreter and converting the
interpreter into javascript so that the python interpreter runs in the
browser. Your python code remains as python code.


Regards,
Gerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top