CPython on the Web

Discussion in 'Python' started by azakai, Jan 2, 2011.

  1. azakai

    azakai Guest

    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
    azakai, Jan 2, 2011
    #1
    1. Advertising

  2. azakai

    Katie T Guest

    On Sun, Jan 2, 2011 at 7:26 AM, azakai <> 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
    Katie T, Jan 2, 2011
    #2
    1. Advertising

  3. azakai

    Guest

    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
    , Jan 2, 2011
    #3
  4. From: "Katie T" <>
    Subject: Re: CPython on the Web


    > On Sun, Jan 2, 2011 at 7:26 AM, azakai <> 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
    Octavian Rasnita, Jan 2, 2011
    #4
  5. azakai

    azakai Guest

    On Jan 2, 5:42 am, 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, Jan 2, 2011
    #5
  6. azakai

    azakai Guest

    On Jan 2, 5:52 am, "Octavian Rasnita" <> 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
    azakai, Jan 2, 2011
    #6
  7. azakai

    Gerry Reno Guest

    On 01/02/2011 02:26 AM, 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
    >
    > 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
    Gerry Reno, Jan 2, 2011
    #7
  8. azakai

    azakai Guest

    On Jan 2, 1:01 pm, Gerry Reno <> 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
    azakai, Jan 2, 2011
    #8
  9. azakai

    Gerry Reno Guest

    On 01/02/2011 05:53 PM, azakai wrote:
    > On Jan 2, 1:01 pm, Gerry Reno <> 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
    Gerry Reno, Jan 2, 2011
    #9
  10. azakai

    Guest

    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
    , Jan 3, 2011
    #10
  11. azakai

    azakai Guest

    On Jan 2, 3:14 pm, Gerry Reno <> wrote:
    > On 01/02/2011 05:53 PM, azakai wrote:
    >
    > > On Jan 2, 1:01 pm, Gerry Reno <> 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


    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, Jan 3, 2011
    #11
  12. azakai

    Gerry Reno Guest

    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
    Gerry Reno, Jan 3, 2011
    #12
  13. azakai

    azakai Guest

    On Jan 2, 4:58 pm, 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'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
    azakai, Jan 3, 2011
    #13
  14. azakai <>:

    >On Jan 2, 4:58 pm, 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
    Wolfgang Strobl, Jan 3, 2011
    #14
  15. azakai

    azakai Guest

    On Jan 2, 5:55 pm, Gerry Reno <> 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. 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
    azakai, Jan 3, 2011
    #15
  16. azakai <> 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
    Diez B. Roggisch, Jan 3, 2011
    #16
  17. azakai

    Gerry Reno Guest

    On 01/03/2011 03:10 PM, azakai wrote:
    > On Jan 2, 5:55 pm, Gerry Reno <> 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, Jan 3, 2011
    #17
  18. azakai

    Gerry Reno Guest

    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
    Gerry Reno, Jan 3, 2011
    #18
  19. Gerry Reno <> writes:

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


    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
    Diez B. Roggisch, Jan 3, 2011
    #19
  20. azakai

    Gerry Reno Guest

    On 01/03/2011 05:55 PM, Diez B. Roggisch wrote:
    > Gerry Reno <> writes:
    >
    >
    >> 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?
    >>

    > 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
    Gerry Reno, Jan 3, 2011
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. York
    Replies:
    1
    Views:
    375
    Chris Liechti
    Sep 10, 2003
  2. Boris Boutillier

    Compiling a CPython library on Mac OSX

    Boris Boutillier, Dec 12, 2003, in forum: Python
    Replies:
    3
    Views:
    387
  3. David McNab
    Replies:
    0
    Views:
    491
    David McNab
    Apr 9, 2004
  4. Randall Smith

    jython 2 cpython bridge

    Randall Smith, May 24, 2004, in forum: Python
    Replies:
    10
    Views:
    1,805
    John Mudd
    Jun 5, 2004
  5. how does CPython work?

    , May 26, 2004, in forum: Python
    Replies:
    3
    Views:
    399
    Stefan Seefeld
    May 27, 2004
Loading...

Share This Page