8 ms·
PyPy.js: A fast, compliant Python implementation for the web
- raymondh 11y agoThis is pretty cool.
- bayesianhorse 11y agoBesides the big download size, it does several things very "right": Full Python data model, access to the full javascript model (albeit slowly currently). Also PyPy is the right choice as the basis for this (as opposed to CPython) because its ecosystem is generally more "pure" Python than CPython's. C Extensions would only get in the way.
- fijal 11y agoit's systematically translating the PyPy interpreter to the Javascript, so it has to get semantics right. However I'm not sure if the drawbacks can be addressed in the current JS model, notably: very slow warmup, huge download size (that's primarily PyPy fault though), the lack of good JS access.
- jychang 11y ago> However I'm not sure if the drawbacks can be addressed in the current JS model, notably: very slow warmup, huge download size Only for the first time loading it. It would be great if some big organization (looks at Google) hosts the "standard" version of the file, much like jquery, so that it would be cached and ready to be used on the local machine most of the time.
- fijal 11y agoI believe this only addresses the download size and not the crazy warmup (you still reparse it etc.)
- TazeTSchnitzel 11y agoNot quite. At least Firefox caches compiled asm.js.
- fijal 11y agoYou can't cache JITted code (so JIT produced by pypy.js) because it'll be different each time.
- TazeTSchnitzel 11y agoAh, sure.
- ggchappell 11y ago> ... access to the full javascript model .... How is the JS model accessed?
- jedberg 11y agoSo does this mean that since I'm better at Python than Javascript, I can now use this to make my web frontends in Python? Because that would be pretty sweet.
- riffraff 11y agoweren't you already able to do it with pyjamas/pyjs?
- jedberg 11y agoQuite possibly. To be honest I never looked into it.
- StavrosK 11y agoBeing a Python guy myself, the problem was never the lack of Python in the browser, it's always the lack of a Python ecosystem in the browser. If I'm going to use Python and awkwardly interface with JS, I might as well just use JS. It makes me want to gouge my eyes out, but it's not really that hard to get productive in. CoffeeScript is even better, if you aren't averse to adding another dependency on top of everything else.
- raverbashing 11y agoNo, no, no!!! Pyjamas was based on GWT, right? So, a Java Framework translated to Python. It's as nice as running a pack of deer on a road with a truck. Awful
- gdw2 11y agoPyjamas/pyjs is actually two parts: the transpiler and accompanying widget set. Only the latter is related to GWT. The transpiler can be used independently.
- pyvpx 11y agoI had the same thought. "this is cool. but what should I do with it?"
- brachi 11y agoI watched the presentation in Pycon 2015[1], and I really liked how honest/aware they are about the challenges and tradeoffs (so far, the performance it's pretty bad compared to javascript, and the download size is still an issue). At the end is answered why python can't be directly included in Firefox or other browsers(the presenter/main developer works for Mozilla) [1]https://www.youtube.com/watch?v=PiBfOFqDIAI https://www.youtube.com/watch?v=PiBfOFqDIAI
- voltagex_ 11y agoI wonder if Python could be embedded as an addon.
- RussianCow 11y agoOf course it could. But that would defeat the point, because support wouldn't be widespread enough for any real use (unless you're just using it for an intranet site or something).
- romaniv 11y agoAs usual, everyone devalues the benefits of progressive enhancement. Add-on would be a great way to get the ball rolling, as long as your website works without it too.
- RussianCow 11y agoBut we're not talking about some optional feature you can just shim in 1kb of JavaScript, we're talking about the language used to write a web app. Sure, you could use PyPy.js as a fallback, but you'd essentially create a horrible loading experience for >99.99% of your users while you wait for this addon to catch on (which won't happen, because there's no reason for an end-user to install it). For a language like Python to be natively supported on the web, at least one major browser has to support it. And that's not likely to happen given the current state of affairs.
- vog 11y ago
- btown 11y agoThis is glorious. From https://github.com/rfk/pypyjs/blob/master/CONTRIBUTING.rst https://github.com/rfk/pypyjs/blob/master/CONTRIBUTING.rst : > We have the following major components in the PyPy repo: >> An "emscripten" build platform definition, which teaches pypy's rpython toolchain how to compile things with emscripten: ./deps/pypy/rpython/translator/platform/emscripten_platform/. >> An rpython JIT backend that emits asmjs at runtime: ./deps/pypy/rpython/jit/backend/asmjs/. >> A "js" builtin module for the resulting interperter, to allow interaction with the host javascript environment: ./deps/pypy/pypy/module/js/. >Along with these wrappers to help working with the resulting interpreter: >> A wrapper to load up the compiled VM and expose it via a nice javascript API: ./lib/pypy.js. >> A script for bundling python modules into an indexed format that can be easily loaded into the browser: ./tools/module_bundler.py. In https://github.com/rfk/pypy/blob/master/rpython/jit/backend/asmjs/assembler.py https://github.com/rfk/pypy/blob/master/rpython/jit/backend/... alone, there are 2887 lines of custom Python code to emit asm.js for function and block JITing. And at 3.26 seconds to initialize from cache on a 4-year-old machine, 5MB gzipped (not too much worse than your average high-quality gif) and only 1.5x slower than CPython... this is actually approaching viable if you needed to run Python business logic in the browser. Which I never thought I would see.
- mutagen 11y agoGlorious indeed. At least three levels of abstraction, two JITs, and a host of supporting code. Can we add some more layers like an emulator or get recursive by getting something like Pyasm running inside it?
- nighthawk454 11y agoSadly, "import antigravity" doesn't work
- reuven 11y agoYes, but "import this" does! (However, it took about 10 seconds to execute. Maybe that's the network I'm on, but even so...)
- e12e 11y agoI was more disappointed (but not surprised!) that "import pip" didn't work. If it did (and it has big ramifications for how much of "python" (for some definition of "python" that includes most of the standard library) works. Eg sockets, interacting with some sort of "local" "file-system" etc) one could (this is handy if working on windows, and running python via cmd-r python.exe): import pip # pip.main expects a list of arguments, hence .split() pip.main("install ipython".split()) # Only needed first # time, as it installs ipython under site-packages. # Only works if the user has write access, obviously import IPython IPython.start_ipython()
- toyg 11y agoThat would be a total security nightmare and hopefully it's never going to happen. I'd be happy enough with serving .py files with the exact same restrictions and privileges that JS libraries have now.
- fragmede 11y ago> That would be a total security nightmare and hopefully it's never going to happen. Why? You're assuming a lot of things about a hypothetical implementation that is going to have heavy restrictions on in anyway since it's running in the javascript sandbox.
- e12e 11y agoHow is that worse than javascript loading javascript via AJAX?
- deleted 11y ago
- retrogradeorbit 11y ago6.6 Megs of binary blob download (pypy.vm.js.mem). Plus 593K of index.json. Yuck. Curious. Interesting tech. But hardly practical.
- nosir33 11y agoSample code to execute some Javascript code inside PyPy.js. I couldn't find any examples. import js js.eval("console.log(\"hi!\")")
- dag11 11y agoOr this: import js js.globals.console.log('hi!')
- catlover99 11y agoI'm loving this, my head is reeling with the glint of possibilities import js js.globals.console.log('hi!'*5)
- deleted 11y ago[deleted]
- e12e 11y agoSeems to be some issues with the interop still, eg: import js >>> "globals" in dir(js) True >>> "console" in dir(js.globals) False >>> js <module 'js' (built-in)> >>> js.globals.console <js.Object handle=14> So apparently introspection doesn't quite work with the wrapped js. Also, if you try: "[ i for i in js.globals]" (or equivalently iterate/loop over the globals-object) - the whole repl/tab hangs. Not really an issue for running business logic in the browser, but if this was a full python repl-interface (with ipython support!) to the whole browser DOM -- that'd be a fantastic tool.
- bzbarsky 11y agoThe introspection bit is because js.globals is implemented with a fancy __getattr__ but doesn't have a __dir__. In general, dir() is not reliable in the face of custom __getattr__ implementations unless the implementor goes out of the way to make it work.
- 11y ago
- estomagordo 11y agoUnfortunately, this is of little use to me. Why? Well, with my Swedish keyboard layout, some alt-combinations are crucial (mainly square brackets, []). And these keypresses do not seem to work.
- Veedrac 11y agoYou can try `vm.eval` from the JS console.
- deleted 11y ago[deleted]
- acron0 11y agoObligatory link to Atwood's Law: http://blog.codinghorror.com/the-principle-of-least-power/ http://blog.codinghorror.com/the-principle-of-least-power/ Although I appreciate that this isn't technically "written in JavaScript" but it follows the principle.
- morekozhambu 11y agoThis is super awesome.
- dlitz 11y agoIs there a Python 3 version of this?
- venti 11y agoYes, in his talk at https://www.youtube.com/watch?v=PiBfOFqDIAI&t=1355 https://www.youtube.com/watch?v=PiBfOFqDIAI&t=1355 Ryan Kelly already showed a Python 3 version of pypy.js
- techdragon 11y agoI was talking with some contributors at PyCon last month and there was a consensus that Python 3 is actually preferable. There's issues to be resolved that are more important than switching the version to 3 so the work is focused on these for now. It boils down to the argument that "if there is no existing Python code relying on Python 2 semantics, why even bother making a Python 2 version, why not skip straight to Python 3 and not introduce the switching problem later?" There's a small matter of the Python 3 version not being the prime target of PyPy but they are updating it and progressing it forward so it's not any kind of roadblock just a little less than perfect.
- bootload 11y agoIf software is eating the world, JavaScript is eating the languages.
- zo1 11y agoOnly because the browser vendors are too scared (for whatever reason, legitimate or otherwise) to add extra languages to the mix, like python.
- bootload 11y ago"vendors are too scared (for whatever reason, legitimate or otherwise) to add extra languages to the mix, like python." Tried python server-side, mod_python and got lots of issues with formatting. Not syntax or mixed tab/spaces but one-off spacing errors. I realised then, Python used this way has problems. Could be fixed though.
- zo1 11y agoOne-off spacing errors? That doesn't sound right at all... Do you have an example?
- bootload 11y agoages ago @zo1, I do see similar problems now if I shift from *nux/vim toolchain to win/idle which by default uses 'Python standard: 4 spaces!' but still throws random format error parsing on default code edits. I don't like the fact python code stops working randomly if I write it in one environment then touch (edit) in another. Never have this problem with C or JS.
- techdragon 11y agoI have only ever had issues like that when I worked on an actual Python checker where I had to keep BAD code in my tests to ensure the checks worked. I've never ( read 99.999% of the time I've performed the task ) had issues with Python formatting that weren't my own fault, everyone makes mistakes after all. I routinely edit the same Python files in Vim, Nano, Sublime Text, Atom, and PyCharm, and even jumping between all those editors, they have never "damaged" the formatting and created syntax errors in my code. I can only surmise that some of the things you used or even just your vim config was setup poorly for Python work.
- nirai 11y agoDoesn't seem to work well on chrome on a modern tablet.
- henryaj 11y agoPlease humour my lack of knowledge: is there much difference between something like this and Opal[0]? [0] - http://opalrb.org http://opalrb.org
- Veedrac 11y agoPyPy.js is not a transpiler, it is a JITted interpreter. This has correctness advantages since the major work is upfront, and potential throughput advantages, but is results in huge upfront download and warmup costs.
- acbart 11y agoI use Skulpt for a lot of my research - I'd love to see a comparison against it in terms of speed, accuracy, etc.
- deleted 11y ago[deleted]
- xiaq 11y agoThe next level is to run a Python implementation of JavaScript inside that...
- gdw2 11y agoCan the client-side python access the DOM?
- venti 11y agoyes it can: import js js.globals.document.getDocumentById("...")
- Nitramp 11y agoI think there is no point in trying to emulate different language semantics on top of JavaScript. asm.js is a weird hack that lives in a totally different world than actual browser APIs (e.g. the DOM), and you have to emulate your entire runtime and environment to get anything useful, which means your binary is going to be huge (as in this example, but similar things apply). Emulating better semantics than JS based on JS usually leaves you in this uncanny valley where you either trade off performance and size for nicer semantics (e.g. killing the null/undefined dichotomy, or 64 bit integer math), or you end up with odd semantics that don't quite fit the language you're compiling from, or a mix of both, which is a usability disaster. So the only real chance to improve on the state of JS is being a syntactical shim on top of JS and help users with better syntax, static analysis, and compile time tooling. That'd be TypeScript.
- fragmede 11y ago> That'd be TypeScript. Or CoffeeScript. Or Dart. Or this. In fact, here's a whole big long list of other languages. Most of which don't use the asm.js "weird hack". https://github.com/jashkenas/coffeescript/wiki/List-of-languages-that-compile-to-JS https://github.com/jashkenas/coffeescript/wiki/List-of-langu...
- alexweber 11y agoone weird hack to run assembler-like code in the browser! developers hate him!
- Tossrock 11y agoLooks like there are some issues to iron out... >>> from datetime import datetime >>> datetime.now() Traceback (most recent call last): File "<console>", line 1, in <module> File "/lib/pypyjs/lib_pypy/datetime.py", line 1548, in now return cls.fromtimestamp(t, tz) File "/lib/pypyjs/lib_pypy/datetime.py", line 1522, in fromtimestamp result = cls(y, m, d, hh, mm, ss, us, tz) File "/lib/pypyjs/lib_pypy/datetime.py", line 1459, in __new__ hour, minute, second, microsecond) File "/lib/pypyjs/lib_pypy/datetime.py", line 312, in _check_time_fields raise ValueError('microsecond must be in 0..999999', microsecond) ValueError: ('microsecond must be in 0..999999', 1716000)
- deleted 11y ago[deleted]
- caryhartline 11y agoThat works now.
- Tossrock 11y agoDoes it? >>> from datetime import datetime >>> datetime.now() Traceback (most recent call last): File "<console>", line 1, in <module> File "/lib/pypyjs/lib_pypy/datetime.py", line 1548, in now return cls.fromtimestamp(t, tz) File "/lib/pypyjs/lib_pypy/datetime.py", line 1511, in fromtimestamp us = _round(frac * 1e6) File "/lib/pypyjs/lib_pypy/datetime.py", line 28, in _round return int(_math.floor(x + 0.5) if x >= 0.0 else _math.ceil(x - 0.5)) ValueError: cannot convert float NaN to integer
- MBlume 11y agoI'm curious -- since the custom emitter is tied to the RPython JIT and not to the Python interpreter, could one easily plug in the existing Ruby or PHP interpreters written in RPython and get similar performance?
- pc2g4d 11y agoNot working for me: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://10.30.229.27:15871/cgi-bin/blockpage.cgi?ws-session=18446744072937828300 http://10.30.229.27:15871/cgi-bin/blockpage.cgi?ws-session=1.... This can be fixed by moving the resource to the same domain or enabling CORS." Firefox 37.0.1
- deleted 11y ago[deleted]
- hurin 11y ago>>> def foo(x,y): ... return x,y debug: OperationError: debug: operror-type: SyntaxError debug: operror-value: ('EOL while scanning string literal', ('c callback', 1, 12, "r = c.push('def foo(x,y):\n", 0)) >>>
- forgottenuser 11y agoHow is this different than Brython or Skulpt? Also python implementations that run in the browser. [1] skulpt.org [2] brython.info
- dulvac 11y agoI can imagine a way to include language extensions in the browser and have them loaded up lazily in a sandbox. And a checkbox that says "Allow websites to run code" or something. Then you could develop an extension for your favourite language and either install them manually or have some popular ones bundled with the browser. If we're going to run everything in the browser, why use javascript as the low-level language that stuff compiles to? I know portability and existing standards are a limitation, but those can be influenced by the 3 (?) major browsers if needed. I think asm.js is an awesome cute little thing, but the fact that it's taken seriously is worrisome to me. Am I completely crazy here?