Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CPython on the Web

75 views
Skip to first unread message

azakai

unread,
Jan 2, 2011, 2:26:50 AM1/2/11
to
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

Katie T

unread,
Jan 2, 2011, 8:29:03 AM1/2/11
to azakai, pytho...@python.org
On Sun, Jan 2, 2011 at 7:26 AM, azakai <alonm...@gmail.com> wrote:
> 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
--
CoderStack
http://www.coderstack.co.uk/perl-jobs
The Software Developer Job Board

pyt...@bdurham.com

unread,
Jan 2, 2011, 8:42:08 AM1/2/11
to azakai, pytho...@python.org
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

Octavian Rasnita

unread,
Jan 2, 2011, 8:52:45 AM1/2/11
to Katie T, azakai, pytho...@python.org
From: "Katie T" <ka...@coderstack.co.uk>
Subject: Re: CPython on the Web


> On Sun, Jan 2, 2011 at 7:26 AM, azakai <alonm...@gmail.com> wrote:

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


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


azakai

unread,
Jan 2, 2011, 2:19:56 PM1/2/11
to
On Jan 2, 5:42 am, pyt...@bdurham.com wrote:
>
> 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

azakai

unread,
Jan 2, 2011, 2:29:09 PM1/2/11
to
On Jan 2, 5:52 am, "Octavian Rasnita" <orasn...@gmail.com> wrote:
>
> 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

Gerry Reno

unread,
Jan 2, 2011, 4:01:53 PM1/2/11
to pytho...@python.org

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

azakai

unread,
Jan 2, 2011, 5:53:47 PM1/2/11
to
On Jan 2, 1:01 pm, Gerry Reno <gr...@verizon.net> wrote:
>
> 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

Gerry Reno

unread,
Jan 2, 2011, 6:14:39 PM1/2/11
to pytho...@python.org
On 01/02/2011 05:53 PM, azakai wrote:
> On Jan 2, 1:01 pm, Gerry Reno <gr...@verizon.net> wrote:
>
>> 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)?
>
>

Errors when using Firefox 3.6.3:

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


Regards,
Gerry

pyt...@bdurham.com

unread,
Jan 2, 2011, 7:58:42 PM1/2/11
to Gerry Reno, pytho...@python.org
Azakai/Gerry,

> Errors when using Firefox 3.6.3:

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

I'm on Windows 7 Pro 64-bit.

Malcolm

azakai

unread,
Jan 2, 2011, 8:00:52 PM1/2/11
to

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

azakai

unread,
Jan 2, 2011, 9:06:09 PM1/2/11
to

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

Gerry Reno

unread,
Jan 2, 2011, 8:55:48 PM1/2/11
to pytho...@python.org
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

Wolfgang Strobl

unread,
Jan 3, 2011, 2:26:20 AM1/3/11
to
azakai <alonm...@gmail.com>:

>On Jan 2, 4:58 pm, pyt...@bdurham.com wrote:
>> Azakai/Gerry,
>>
>> > Errors when using Firefox 3.6.3:
>>
>> I'm running Firefox 3.6.1.3 and the interpreter is running fine.

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.


--
Wir danken für die Beachtung aller Sicherheitsbestimmungen

azakai

unread,
Jan 3, 2011, 3:10:44 PM1/3/11
to

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

Diez B. Roggisch

unread,
Jan 3, 2011, 3:13:29 PM1/3/11
to
azakai <alonm...@gmail.com> writes:

> 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

Gerry Reno

unread,
Jan 3, 2011, 3:23:51 PM1/3/11
to pytho...@python.org
On 01/03/2011 03:10 PM, azakai wrote:
> On Jan 2, 5:55 pm, Gerry Reno <gr...@verizon.net> wrote:
>
>> 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

Gerry Reno

unread,
Jan 3, 2011, 3:35:01 PM1/3/11
to pytho...@python.org
On 01/03/2011 03:13 PM, Diez B. Roggisch wrote:
>
> 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

Diez B. Roggisch

unread,
Jan 3, 2011, 5:55:41 PM1/3/11
to
Gerry Reno <gr...@verizon.net> writes:

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

Gerry Reno

unread,
Jan 3, 2011, 6:19:20 PM1/3/11
to pytho...@python.org

>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

astar

unread,
Jan 3, 2011, 7:00:59 PM1/3/11
to
On Jan 2, 4:58 pm, pyt...@bdurham.com wrote:
> Azakai/Gerry,
>
> > Errors when using Firefox 3.6.3:
>


firefox 3.6.13 openbsd i386 4.8 -current
error console has some errors:

editor not defined
module not define
too much recursion

nothing interested happened on the web page, but wonderful project
anyway

azakai

unread,
Jan 3, 2011, 7:55:19 PM1/3/11
to
On Jan 3, 12:13 pm, de...@web.de (Diez B. Roggisch) wrote:
> A fun hack. Have you bothered to compare it to the PyPy javascript
> backend - perfomance-wise, that is?
>

Gerry already gave a complete and accurate answer to the status of
this project in comparison to PyPy and pyjamas. Regarding performance,
this hack is not currently fast, primarily because the code is not
optimized yet.

But through a combination of optimizations on the side of Emscripten
(getting all LLVM optimizations to work when compiling to JS) and on
the side of the browsers (optimizing accesses on typed arrays in JS,
etc.), then I hope the code will eventually run quite fast, even
comparably to C.

- azakai

azakai

unread,
Jan 3, 2011, 8:59:10 PM1/3/11
to

Hmm, I think this might be possible with the HTML5 File API. Would
definitely be useful here.

- azakai

MrJean1

unread,
Jan 3, 2011, 11:28:30 PM1/3/11
to
FireFox 3.6.13 on MacOS X Tiger (10.4.11) fails:

Error: too much recursion
Error: Modules is not defined
Source File: http://synthensity.com/static/python.html

/Jean

On Jan 2, 11:26 pm, Wolfgang Strobl <ne...@mystrobl.de> wrote:
> azakai <alonmozi...@gmail.com>:


>
> >On Jan 2, 4:58 pm, pyt...@bdurham.com wrote:
> >> Azakai/Gerry,
>
> >> > Errors when using Firefox 3.6.3:
>
> >> I'm running Firefox 3.6.1.3 and the interpreter is running fine.
>
> 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.
>
> --

> Wir danken f r die Beachtung aller Sicherheitsbestimmungen

MrJean1

unread,
Jan 3, 2011, 11:34:33 PM1/3/11
to
FYI,

The example

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

works fine in Safari 4.1.3 on MacOS X Tiger (10.4.11).

/Jean

John Nagle

unread,
Jan 4, 2011, 1:11:20 AM1/4/11
to
On 1/1/2011 11:26 PM, azakai wrote:
> 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

It's a cute hack, but it's about 1000 times slower than CPython.

Try

def cnt(n) :
j = 0
for i in xrange(n) :
j = j + 1
return(j)

print(cnt(1000000))

with this. It will take 30 seconds or so to count to a million.

John Nagle

azakai

unread,
Jan 4, 2011, 2:13:43 AM1/4/11
to
On Jan 3, 10:11 pm, John Nagle <na...@animats.com> wrote:
> On 1/1/2011 11:26 PM, azakai wrote:
>
> > 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, seehttp://emscripten.org

>
>     It's a cute hack, but it's about 1000 times slower than CPython.
>
> Try
>
> def cnt(n) :
>      j = 0
>      for i in xrange(n) :
>          j = j + 1
>      return(j)
>
> print(cnt(1000000))
>
> with this.  It will take 30 seconds or so to count to a million.
>
>                                         John Nagle

Yes, as I said, "the code isn't optimized (so
don't expect good performance)" :)

It can get much faster with more work.

- azakai

John Nagle

unread,
Jan 4, 2011, 3:02:47 AM1/4/11
to
On 1/3/2011 11:13 PM, azakai wrote:
> On Jan 3, 10:11 pm, John Nagle<na...@animats.com> wrote:
>> On 1/1/2011 11:26 PM, azakai wrote:
>>
>>> 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, seehttp://emscripten.org
>>
>> It's a cute hack, but it's about 1000 times slower than CPython.

>


> Yes, as I said, "the code isn't optimized (so
> don't expect good performance)" :)
>
> It can get much faster with more work.

Yea, right.

You're three deep in interpreters. Performance is going
to suck.

John Nagle

Carl Banks

unread,
Jan 4, 2011, 6:17:34 AM1/4/11
to
On Jan 3, 4:55 pm, azakai <alonmozi...@gmail.com> wrote:
> But through a combination of optimizations on the side of Emscripten
> (getting all LLVM optimizations to work when compiling to JS) and on
> the side of the browsers (optimizing accesses on typed arrays in JS,
> etc.), then I hope the code will eventually run quite fast, even
> comparably to C.

This is a very cool idea. It's quite fascinating to view the
Javascript "machine code" for a complete CPython interpreter.

I'm sure with a little work you'll be able to improve its performance,
but I think "comparably to C" is going to be a tall order.

If you can get this to work reasonably well, and manage to get it
successfully deployed it somewhere, I'd recommend petitioning to have
this be considered an official platform.


Carl Banks

gry

unread,
Jan 4, 2011, 12:38:58 PM1/4/11
to
On Jan 4, 1:11 am, John Nagle <na...@animats.com> wrote:
> On 1/1/2011 11:26 PM, azakai wrote:
>
> > 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, seehttp://emscripten.org

On loading, I "get script stack space quota is exhausted" under
firefox 3.5.12, under linux.
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.12) Gecko/20100907
Fedora/3.5.12-1.fc12 Firefox/3.5.12

Gerry Reno

unread,
Jan 4, 2011, 12:48:43 PM1/4/11
to pytho...@python.org


It's a Firefox bug apparently fixed in Firefox 4.x.

Some versions of Firefox 3.6.x do work but most do not.


Regards,
Gerry

0 new messages