Slashdot Mirror


Python Converted To JavaScript, Executed In-Browser

lkcl writes "Two independent projects, Skulpt and Pyjamas, are working to bring Python to the web browser (and the JavaScript command-line) the hard way: as JavaScript. Skulpt already has a cool Python prompt demo on its homepage; Pyjamas has a gwtcanvas demo port and a GChart 2.6 demo port. Using the 64-bit version of Google v8 and PyV8, Pyjamas has just recently and successfully run its Python regression tests, converted to JavaScript, at the command-line. (Note: don't try any of the above SVG demos with FF2 or IE6; they will suck.)"

36 of 176 comments (clear)

  1. Re:python sucks by master5o1 · · Score: 5, Funny

    Because it's done like this:
    <?php
    die(Python);
    ?>

    --
    signature is pants
  2. GWT for Python? by mcvos · · Score: 2, Interesting

    It's not entirely clear to me what Skulpt is exactly, but Pyjamas is apparently GWT ported to Python, which sounds like a really cool idea. Now if somebody did the same thing with Ruby and Scala, I'd be really happy.

    Javascript is just way too stupid to program manually, but currently we're in the odd situation where we're writing server stuff in Ruby, and browser stuff in Java. That's just wrong.

    1. Re:GWT for Python? by lkcl · · Score: 3, Informative

      you're looking for RubyJS. sadly, funding has not been forthcoming in order to carry RubyJS forward. the compiler is excellent; the insights into the technical issues behind dynamic language translation were very useful (even to python translator developers) - but martin ran out of time/money/enthusiasm due to the lack of interest shown, so he only got as far as creating HTML and Button for RWT (Ruby Web Toolkit).

    2. Re:GWT for Python? by loufoque · · Score: 2, Interesting

      Javascript is just way too stupid to program manually

      Not more than Ruby or Python.
      Don't confuse languages with libraries.

    3. Re:GWT for Python? by loufoque · · Score: 2

      Eh, a language is only as good as its standard library.

      So C and C++, ones of the most used languages in the world, are worse than fuck since their standard library is ridiculously small?

      It's not like all the best and more important software is written in those languages... (virtually all operating systems and major applications)

  3. Re:Why, God, why???? by mcvos · · Score: 2, Insightful

    What a waste of time and energy. The only thing worse than Python is, well, Javascript.

    That is exactly the whole point that you're so obviously missing here: nobody sane should have to write Javascript, and yet it's the only thing that's supported by browsers. So converting code from other languages to Javascript is the only sensible solution at the moment. (For the longer term, it'd be nice if they replaced Javascript with something halfway sane.)

  4. How about a Javascript - to - python convertor? by ggpauly · · Score: 2, Insightful

    In most ways (no explicit integer type being an exception) Javascript is a remarkable and beautiful language. It has libraries available on a server near you through Dojo, among others. Javascript is one of the best things about browsers.

    What browsers need is a workable CSS and DOM interface (although the DOM interface has improved in recent years). But these are not issues with Javascript per se. Cleaning up the browser programming environment is not about getting rid of Javascript.

    From TFA: """
    anyway, just thought there might be people who would be intrigued (or
    horrified enough to care what's being done in the name of computer
    science) by either of these projects.
    """

    Not horrified, but I wonder if W3C politics is creating unforeseen consequences.

    --
    Verbum caro factum est
    1. Re:How about a Javascript - to - python convertor? by s4m7 · · Score: 4, Insightful

      Cleaning up the browser programming environment is not about getting rid of Javascript.

      Maybe not, but javascript is not a good language, it's a bad language with some good features. The awful scoping mechanism is evidence of this enough. The intrinsic objects are too limited to be useful, so much so that now there are more than 4 different common framework projects to handle all the inconsistency in implementation and they're all incompatible with each other. anonymous functions are great but they make debugging a giant pain the arse.

      i would like to see a viable alternative language to javascript, just for variety's sake. It's just had layers of crap pasted on top of it since 1995 or whatever and it'd be nice to see a new approach that fits what people actually use it for these days.

      --
      This comment is fully compliant with RFC 527.
    2. Re:How about a Javascript - to - python convertor? by lkcl · · Score: 2, Interesting

      What browsers need is a workable CSS and DOM interface (although the DOM interface has improved in recent years).

      yes - it's these DOM interfaces that i used for the pure-python port of pyjamas. the first one (webkit) i literally had to create, myself (it took 8 weeks). the second one, xulrunner, used a component created by the OLPC team, called hulahop; the third one, for windows only, uses python COM (the comtypes library) and python ctypes.

      But these are not issues with Javascript per se. Cleaning up the browser programming environment is not about getting rid of Javascript.

      From TFA: """
      anyway, just thought there might be people who would be intrigued (or
      horrified enough to care what's being done in the name of computer
      science) by either of these projects.
      """

      Not horrified, but I wonder if W3C politics is creating unforeseen consequences.

      it has to be said that the attitude of the webkit developers (one in particular who can be easily identified) has been incredibly bad, towards the free software glib/gobject bindings i created for webkit's DOM model. nobody should have to put up with the kind of disrespectful treatment i was subjected to, and other contributors whom i've encouraged and trained to help out have been so intimidated by the webkit developers that they are unwilling to even make themselves known to the webkit team, in case they get treated the same way.

      it's a long story, and the webkit team will get burned for it, one way or another.

    3. Re:How about a Javascript - to - python convertor? by ggpauly · · Score: 4, Informative

      The link to Douglas Crockford's site in the parent was to an article entitled "The World's Most Misunderstood Programming Language".

      The awful scoping mechanism

      From the brief survey of Javascript on Crockford's site:
      """
      When used inside of a function, var defines variables with function-scope. The vars are not accessible from outside of the function. There is no other granularity of scope in JavaScript. In particular, there is no block-scope.
      """
      iow, use the var keyword. Programmers who do not know this ONE RULE (TM) are bitten by mysterious insects. Use a lint program if needed.

      Functions are objects in Javascript, so this effectively allows, in either functional or object styles of programming, programmers to freely and simply define their variable scoping.

      Procedural programmers used to simple block scoping (Hi COBOL fans!) may need to find a mechanism to cope with this. But I'd suggest using OO techniques if your program is complex enough that this is a problem. Javascript allows simple, non-demanding OO. If you like your OO authoritarian then Google has a Java-to-javascript translator.

      intrinsic objects are too limited to be useful, so much so that now there are more than 4 different common framework projects

      Python has one official library (and many 'unofficial' modules too), without which Python would be very limited. Javascript has many unofficial libraries. Welcome to the free world.

      btw, I use Python, and I get Twisted Python at least to some extent. Twisted's deferred abstraction is mimicked in Dojo Javascript. I use Python on the server and Javascript + Dojo on the browser. Python has less warts and more modules. Javascript has astounding power in simplicity, as in the scoping rule.

      --
      Verbum caro factum est
    4. Re:How about a Javascript - to - python convertor? by BitZtream · · Score: 2, Insightful

      i would like to see a viable alternative language to javascript, just for variety's sake. It's just had layers of crap pasted on top of it since 1995 or whatever and it'd be nice to see a new approach that fits what people actually use it for these days.

      Why? So in 2025 you can sit around talking about the new language thats fills its roll perfectly but isn't to your liking and wasn't the perfect vision you thought it would be 15 years ago? Javascript has barely changed in the last 15 years. The environment that javascript deals with has changed a lot, but the language, not so much. Any language (or more appropriately put, any environment) that has 15 years of patches for an environment that changes rapidly is going to look and feel the same, regardless of if it starts off with your precious languages or not.

      Here is reality for you developers of the world, if you think your language is better than all the rest, or some other language should go away, then you are a shitty programmer. The language does not make the programmer, the programmer does. If you find a language/environment difficult to use, you are the shitty part of the equation. Languages are just that, languages. They describe things. You are the one that has to figure out how to describe what you want, if you can't do that, the language or environment isn't the problem, you are.

      Stop blaming JS for you being a shitty developer who can only parrot things out in whatever language you learned from your CS professor.

      --
      Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager
    5. Re:How about a Javascript - to - python convertor? by s4m7 · · Score: 2, Insightful

      wow, cs rage much?

      I am perfectly capable of programming in JS. I've written a number of extremely useful classes in the language and I don't care for your tone.

      if you think your language is better than all the rest, or some other language should go away, then you are a shitty programmer.

      well, hello mr. strawman. I never declared that I wish javascript would go away, and I don't suggest any "better" language should take its place... I merely asked for an alternative that reflects how it is used in a modern sense more accurately.

      Why? [...]Javascript has barely changed in the last 15 years.

      Thank you for answering your own question. js wasn't a good language in the first place and 15 years of cobbling have only gone on to expose and underscore its various weaknesses. JS can do quite a bit, and it's undoubtedly useful. that doesn't change the fact that it's fundamentally a poor structure for doing what people do with it now: namely DOM scripting and asynchronous requests. I don't disagree that in 15 years whatever we develop today probably won't be a good fit for what we're doing with it then: this however is no excuse for a complete lack of progress.

      If you find a language/environment difficult to use, you are the shitty part of the equation.

      well that's an extremely lazy way of looking at things. Yes languages are just that, languages, and since there's no excuse for being a "bad programmer" and expecting your tools and vocabulary to be up to the task you're attempting to address, we should all still be programming in assembler using punchcards by your logic. God forbid we make use of decades worth of brilliant thought on the topic of how programming languages ought to be structured and keep things moving forward.

      --
      This comment is fully compliant with RFC 527.
    6. Re:How about a Javascript - to - python convertor? by shutdown+-p+now · · Score: 2, Interesting

      Here is reality for you developers of the world, if you think your language is better than all the rest, or some other language should go away, then you are a shitty programmer.

      I don't know of any "silver bullet" languages, though I definitely have favorites - and I do not wish all others to go away.

      What I do wish, however, is for badly designed languages which don't have any redeeming qualities to go away. And, yes, JS is on that list. Though, admittedly, way below PHP.

      On a side note, an example of a badly designed language with a redeeming quality is Common Lisp - it's ugly as hell, but nothing else matches it in terms of sheer power.

  5. Re:Pointless by chrysalis · · Score: 3, Informative

    There is already a Ruby VM that runs in a browser: Hotruby : http://hotruby.yukoba.jp/

    John Resig even blogged about it ages ago: http://ejohn.org/blog/ruby-vm-in-javascript/

    Also, JS.class, while not a Ruby VM, is pretty cool and actually useful: http://blog.jcoglan.com/2009/06/08/jsclass-21-an-improved-pacakge-manager-proper-hashes-and-lots-of-ruby-19-goodness/ - http://ajaxian.com/archives/jsclass-21-released

    --
    {{.sig}}
  6. Re:Why, God, why???? by timeOday · · Score: 5, Insightful

    The only thing worse than Python is, well, Javascript.

    I'd be interested to hear what you like better, and why? Personally I'm still sad that Java (not Javascript) didn't win on the Web - a cross-platform, general purpose language that is at least a reasonable choice for most anything. To make programming faster, you can always use higher-level libraries or code-building environments on top of it, or compile some other syntax to java bytecode.

    Now instead the Web is a big mish-mash of fundamentally incompatible technologies. And if anybody does pull off the one-runtime-for-anything vision, it looks like it will be Microsoft.

  7. Re:Now I've heard everything by lkcl · · Score: 4, Informative

    Just when you thought things could not get any crazier, there's this story. Let's hope it's an early April Fool.

    nope. it's not. and i didn't mention in the article that pyjamas-desktop can run the python as a desktop app, either. including the GWT Canvas ported code, under the MSHTML engine. after all, the IE/MSHTML gwtcanvas is just creating VML nodes, so perhaps it shouldn't come as a surprise that it works, but it's still pretty cool all the same.

    There's no way one could simulate more than about 12% of Python's complex OO semantics in JavaScript.

    wrong. sorry. javascript is a drastically underestimated language. dreadful to work with if you don't know what you're doing, but incredibly powerful at the same time.

    a javascript implementation of "type" - not the 1-arg version but the full 3-arg version - was initially implemented in 85 lines of javascript (it's a bit more, now).

    we use that functionality to dynamically create, classes, supporting multiple inheritance and more.

    pyjamas has also implemented decorators _and_ properties; kees is currently working on "yield" after skulpt's developers started working on it. not "yield by cheating and using FF built-in support for yield", but "yield" as in doing it the hard way, by analysing the state of the function and adjusting / jumping to the correct point in the function on each loop.

    you really should take a look at the regression tests

    Python itself already has a hard and slow slog trying to perform all its tricks.

    To add yet another layer of translation or simulation sounds like a lose-lose proposition. Slower and hopelessly inexact.

    slower, yes - "hopelessly" inexact: no. for GUI purposes, _if_ you've designed the application correctly (i.e. along MVC / client-server lines), then the "-O" option which switches off all the python "strict" compatibility, is perfectly sufficient.

    so, "letting things fall through" to javascript, and allowing int(width) + "px" to succeed, and [1,2,3] + [4] to fail, is "good enough for most purposes".

    Not to mention many of the more useful Python modules have a considerable C component, making them completely unusable as JavaScript.

    wrong again. sorry. two reasons. three.

    1) pygtkweb showed that it is perfectly possible to make a seamless / transparent JSONRPC service that ships function call arguments over to a server, for execution server-side, starting from e.g. "import md5"

    2) pyjamas' implementations of datetime, md5, urllib, time and a few others is growing as users contribute to them. thus, the pyjamas GUI library fits the *users* needs, on an ongoing basis.

    3) if you _really_ can't do without the full semantics of python, but want the benefits of full HTML, CSS, NPAPI plugins etc., use one of the pyjamas-desktop ports. there's MSHTML, XULRunner and PyWebkitGTK to choose from.

  8. A practical use by slthytove · · Score: 4, Interesting

    As a high school computer science teacher, I can see a practical application of this. Currently, I think Python is one of the best languages to learn basic programming concepts with, in that it is relatively straightforward, powerful, and there's not much "voodoo" to prevent students from diving right into programming. It's possible to do some really cool things in Python with not much code.

    However, one of the problems is that it can be difficult to give students a way to show off their code. Since it's an interpreted language, they can't just give an .exe or a .app file to someone else to show it off - they need to say, "Oh, go and install Python, and these libraries, etc." Yes, there are solutions such as py2exe and py2app, but getting these set up can be quite a task in itself. By running Python inside JavaScript, you basically open up the whole web-connected world as a potential audience to these budding programmers. It's much easier to say, "Hey, check out this link!" than "Hey, download Python (but get the right version) and this graphics library, then download my .py file, and open up a command prompt and type python blahblah.py!"

    1. Re:A practical use by ggpauly · · Score: 3, Insightful

      Why not teach Javascript itself?

      It's a simple but powerful general-purpose object-oriented language. It's also a functional programming language with C-like syntax, closures, and lambdas.

      A browser is the interpreter.

          **** Please read http://javascript.crockford.com/javascript.html before modding this down. ****

      --
      Verbum caro factum est
  9. Google Translate Extended by aoheno · · Score: 3, Funny

    Announcing onelanguage dot com, an extension of Google Translate, ushering in an era where programmers need only learn one language and have it converted to another language of choice.

    FAQ:

    Q. How accurate is it?
    A. Same as Google Translate. You have to learn the output language to know which parts of the language you already know result in garbage-out. We suggest you use our new Pidgin versions of Python, PHP, Java, and Ruby.

    --
    Her lips were softer than a duck's bill, but her quacks ...
    1. Re:Google Translate Extended by lkcl · · Score: 4, Interesting

      i know you're joking, but there really _is_ a Visual Studio "universal translator" plugin available - i've seen it demo'd, converting c++ to java, to B, to ruby, to c#. it all used CLR as the intermediary. i heard that activestate were commissioned to add python to the mix, but, weirdly, it wasn't included in the release of the translator i saw.

  10. Re:iPhone application? by nneonneo · · Score: 3, Informative

    I have an iPod touch running iPhone OS 3.1.1, so I tried these two projects out.

    Skulpt works, but the console does not (I had to use the quick-links to test code). This is relatively easy to fix by using a textbox instead of using keyboard events. It would be very simple to write a simple webapp which evaluates Python code in the Safari browser. However, as I see it, Skulpt is still quite immature -- it doesn't implement much of the language (e.g. classes work, but can't be instantiated because Skulpt thinks you are trying to call the type object, instead of constructing it), and it doesn't do imports at all.

    Pyjamas works extremely well, though it is compiled as pure JS and thus lacks (AFAIK) an "exec" method to run arbitrary Python code.

    Given that Skulpt features a decent Python parser but lacks much of the core functionality, and Pyjamas implements a lot of functionality but lacks a parser built in JS, I think the projects would be mutually beneficial.

  11. Re:python sucks by the_humeister · · Score: 3, Interesting
  12. Re:iPhone application? by lkcl · · Score: 4, Informative

    Pyjamas works extremely well, though it is compiled as pure JS and thus lacks (AFAIK) an "exec" method to run arbitrary Python code.

    i'm working on it. last week i back-ported skulpt's parser and AST code from javascript to python, and regression-tested it; now it's a matter of improving the pyjamas compiler to be able to successfully compile that python into javascript, and we're bootstrapped.

  13. In related news.... by mrops · · Score: 5, Funny

    ...they used a Perl script to convert Python to Javascript.

  14. Re:python sucks by the_humeister · · Score: 4, Interesting

    Found something better: 6502 assembler!

  15. Re:Why, God, why???? by bcboy · · Score: 4, Interesting

    I felt the same way until I watched Douglas Crockford's videos on javascript. If you hate javascript, you're doing it wrong. I now prefer it to the other languages being discussed here.

  16. Re:iPhone application? by lkcl · · Score: 2, Informative

    there are a ton of different ways, now: i've been maintaining a list on the python wiki, WebBrowserProgramming page. pypy used to have a -to-javascript back-end (now abandoned); there's titanium appcelerator (which supports IronRuby and IronPython); there's PyXPCOMExt and a few more besides.

  17. Re:Why, God, why???? by onefriedrice · · Score: 2, Interesting

    Well, haXe is a Java-like language which compiles to Javascript among other languages. It's definitely general purpose enough to use in the client as well as the back-end, and it has nice libraries which make communication between them easy. It happens to compile to Flash, too. I'd imagine that any reasonably complex project could be built just about entirely in haXe.

    --
    This author takes full ownership and responsibility for the unpopular opinions outlined above.
  18. Re:Why don't browsers just support it? by redhog · · Score: 2, Informative

    That'd be damn hard. Python isn't made for sandboxing (google python and sandboxing, or restricted execution, and you'll see). If you did the script language=python implementation the "obvbious" way and just linked in the python interpreter into mozilla, you'd have a security hole big enough for a supertanker. There is Python support for extensions though - google PyXPCOM.

    --
    --The knowledge that you are an idiot, is what distinguishes you from one.
  19. Python implementations still suck by Animats · · Score: 4, Insightful

    Python is quite a good language, but the implementations suck. This is holding back widespread use of Python. It's too slow, typically 10x to 30x slower than C. That's far worse than Java.

    There have been several attempts at other implementations. But because Guido Rossum fights formal standardization of the language, treating his CPython implementation as a de-facto standard, everyone else has a moving target to hit.

    Google (who hired Guido) likes Python, but they don't like the low performance. CPython is a "naive interpreter" (very little optimization). Worse, with the rather lame implementation the Global Interpreter Lock, not only can't it use a multi-core CPU effectively, multi-thread programs run slower on multi-core CPUs. (The threads fight over the lock in an embarrassingly inefficient way.)

    Google is doing "Unladen Swallow", which is an attempt to bolt CPython to a just-in-time compiler to a virtual machine. It's not clear how well that will work out, but the end result will have more layers than seems to be indicated. The goal is 5x faster than CPython, which won't beat Java, let alone C/C++.

    It's cute that Python to JavaScript translation is possible, but it's not going to help much on the performance front.

    For a few years, the great hope of the Python community was PyPy, but that had too many goals, was being developed in "sprints", and after five years, the European Union pulled the plug on funding after it was slower than CPython.

    Shed Skin, which is a Python to C++ hard-code compiler, is currently the lead in Python performance, but it doesn't yet implement the whole language. Still, with about two people working on it, Shed Skin is doing better than most of the bigger projects. Shed Skin does automatic type inference. Python doesn't have declarations, but with enough analysis, the compiler can figure out what types each variable can hold and generate hard types, which makes for much faster code.

    1. Re:Python implementations still suck by OrangeTide · · Score: 2, Insightful

      Saying that is bad because it is slower then Java means that Java is really slow and people are not happy with that so anything slower will make us more unhappy.

      What?

      --
      “Common sense is not so common.” — Voltaire
    2. Re:Python implementations still suck by bill_kress · · Score: 2, Interesting

      Strange assumption...

      Why on earth would you say Java was slower than everything that came before it?

      It's faster than everything but C, and even ties C in some cases.

      I believe the Language Shootout includes specs for Fortran and Basic (which were before Java), I'm also pretty sure it was faster than Pascal, Ada (Can't swear to that one, don't think it's on the shootout, but I don't remember it being specifically quick), heck hundreds of languages.

      I still don't know why Java gets crap about performance when it is the only language that seems to perform like C.

      Honestly the only thing I can think of is that like Visual Basic, it made programming possible for a LOT more people--people who didn't really know how to program and only wanted to implement a solution to a problem. They made good ideas into decent programs with very bad performance.

      I'm pretty sure that if Python ever becomes known as the language that is an "Easy" way to solve some problem, it'll have the same kind of issues.

      I've also seen some pretty crappy Ruby on Rails websites...

    3. Re:Python implementations still suck by MostAwesomeDude · · Score: 2, Informative

      Don't use threads. Use multiprocessing for concurrency and Twisted for asynchronous I/O.

      If you're going to complain about the GIL, perhaps you should go read up on why it's necessary, and perhaps share some of your amazing insights on a better, more performant way to do threading.

      PyPy is far from dead. They've got a JIT working for x86 assembly that matches NumPy in some tests.

      --
      ~ C.
    4. Re:Python implementations still suck by Sam+Douglas · · Score: 3, Informative

      I used to have the idea that Java was inherently slow. It is not. Some implementations of the Java libraries are slow, Swing is a bit sluggish on some platforms (it is largely coded in Java rather than using native toolkit functionality) but the Java language itself is quite fast. If coded well, you can get performance comparable to C, but that is also quite dependent on how well your virtual machine JIT compiles the code (or, if it does).

      I'm sure a lot of people base their preconceptions of Java being slow on programs like Azureus and Eclipse. These programs are slow because they are big and bloated, not just because they are coded in Java. On GNU/Linux systems especially (older Ubuntu releases were bad for this), the packages distributed in the repository would run use GCJ even when the much more efficient Sun runtime was installed.

      I'm not a Java fanatic, nor an IBM troll. /p.

  20. lipstick, dogs by speedtux · · Score: 2, Funny

    Compiling Python to JavaScript running in a browser really is like putting lipstick on a dog.

  21. Speed matters. Datacenters cost money. by Animats · · Score: 4, Interesting

    Because it IS easier to use and hardware is always getting cheaper.

    On the server side, 10x to 30x slower means building entire buildings full of servers. Or more expensive hardware in the cell phone. Or using another language.

    Python is actually a good general-purpose programming language, not just a "scripting language". The big problem is slow execution.

    The basic problem with CPython is that, being a naive interpreter, it has to check for the hard cases every time. "n = n + 1" ought to be a few machine instructions, maybe only one. But Python has to check for n being a float, n being a string, n being an object, etc., every time. Shed Skin, with a type inference systems, does analysis at compile time and determines that n is always an integer, then generates code for integer arithmetic only.

    There are all sorts of dynamic things one can do to a Python program while it's running. You can add a function to an object. You can replace existing functions. You can load new modules on the fly. But most of the time, for most of the objects, you don't do that. An efficient implementation needs to separate out the cases where something unusual might occur, and the far more common cases when the program is doing routine stuff that needs to go fast. The common cases can then be handled with much simpler, and faster, code.