10 ms·
PyPy.js: Python in the web browser
- tony-allan 8y agoPity it's not Python3.
- rtkwe 8y agoYeah weird. I would expect Python2 to be dying off more by now. Wonder if we're headed to an environment where there's essentially 2 separate languages as Python 3 continues to change and grow?
- ealhad 8y agohttps://pythonclock.org/ https://pythonclock.org/
- jchw 8y agoI think we're roughly already there. I think the pivotal moment where Python 2 could've died out rapidly passed and they kept officially supporting it for too long.
- da_chicken 8y agoAs far as I know, permanent EOL for Python 2 is still Jan 1, 2020 (https://pythonclock.org/ https://pythonclock.org/). Python 3 was first released December of 2008. That's an awfully long tail.
- collinmanderson 8y agoFascinating. I always thought Python 2.7 support would end July 3, 2020 (exactly 5+5 years from when 2.7 was released), but for some reason they recently decided to go with Jan 1st. > Specifically, 2.7 will receive bugfix support until January 1, 2020. All 2.7 development work will cease in 2020. > I've updated the PEP to say 2.7 is completely dead on Jan 1 2020. The final release may not literally be on January 1st, but we certainly don't want to support 2.7 through all of 2020. https://www.python.org/dev/peps/pep-0373/#maintenance-releases https://www.python.org/dev/peps/pep-0373/#maintenance-releas... https://mail.python.org/pipermail/python-dev/2018-March/152355.html https://mail.python.org/pipermail/python-dev/2018-March/1523...
- duhi88 8y agoWell this project has pretty much died off. No update in over a year, and nothing significant in 3-4.
- detaro 8y agoIt's a pretty old side-project, based on an interpreter that itself took quite a while to support Python 3 well due to lack of funding, so it's not that weird it hasn't been updated.
- saganus 8y agoI still get surprises like starting to learn Google Cloud Functions and realizing that up until July of this year they only supported Python 2. I have no idea why would a project of this caliber would start by using Python 2 instead of Python 3. Edit: When I started reading about GCF, all docs said I could only use Python 2. Later I found that they seem to be on the way to change this. But still, I was very surprised that Python 2 was even an option to begin with.
- jchw 8y agoAssuming the work here was primarily the asm.js based emitter, maybe it can be ported to PyPy3 without needing to go from scratch.
- costrouc 8y agoWill web assembly have any effect on a future web based python interpreter? Is there one in the works?
- aylmao 8y agoSomeone posted this on another thread: https://github.com/iodide-project/pyodide https://github.com/iodide-project/pyodide What I gather is that it's compiling CPython to WASM and ships it along with some other utilities. It looks pretty cool.
- tyingq 8y agoWASM plans on adding GC, polymorphic inline cache, direct DOM access, etc. That would allow for Python without having to download the whole runtime. Whenever that happens, it might become more mainstream. Similar for other languages.
- deleted 8y ago[deleted]
- fulafel 8y agoThere have been js/asm.js CPython builds demoed since early days of emscripten, and emscripten is the main WebAssembly toolchain, so it's "just worked" since the beginning.
- nautilus12 8y agoWhy use this instead of jupyter? Some educational applications maybe?
- ealhad 8y agoThis runs directly in the browser, whereas Jupyter needs a server.
- antoineMoPa 8y agoI think jupyter works with a backend server to execute python (right?).
- Waterluvian 8y agoCorrect. It's just a view to a server running python.
- duhi88 8y agoThis is pretty old, and hasn't seen an update in over a year. I never understand why links links this get posted with no discussion to start it off with. Also, wouldn't this be better implemented with WASM?
- anarchy8 8y ago> This is pretty old, and hasn't seen an update in over a year So pretty much like every Python project then. What's happening to the Python community?
- digitaLandscape 8y agoIt's stable and mature? If you've been writing JavaScript your whole career you might not know what that looks like.
- Waterluvian 8y agoSuch a snarky response, but I can't deny it. I see a JS package that hasn't been updated in a year and I expect that it won't even build with my environment because webpack evolution. I see a python package that hasn't been updated in a year and it doesn't faze me. Not a whole lot that can break in terms of building and integration with my python project.
- andrewmcwatters 8y agoPerspective is everything. Now and again I use C libraries that haven't been updated in 15 years. Why? There isn't a need. Everything builds perfectly, and some of those libraries are still the go-to solutions in my field.
- daveFNbuck 8y agoIts front page describes it as an experiment, and it has 98 open bugs. Both PyPy and Python have released new versions since the last update of this project, so it's at least missing important updates.
- klhugo 8y agoI tried to do a little something with Emscripten and WebAssembly, but I stopped on the sockets implementation. It relies on WebSockets. I wonder how well we could emulate real sockets using WebRTC to make WebAssembly apps more sophisticated.
- IshKebab 8y agoLast I looked Emscripten already used WebRTC's data channel to emulate UDP.
- VectorLock 8y agoI'd love there to be a client-side webapp development system using Python with feature and output parity to JavaScript. If it was a product, I would buy it.
- rkeene2 8y agoI've been thinking about building this for Tcl. Tcl has been available in the browser in various forms for decades, but one thing that makes Tcl uniquely suited to a web application style development is its threading model. In Tcl, you access a thread by sending messages to it, in which a Tcl interpreter inside its event loop processes. My plan was to have a client-side Tcl program in the browser that automatically instantiates a second thread, but the thread is a remote thread running on the server, so you would be able to access the server just as if it were another thread. Something like (client-side): thread::send $::tcl-web::server -async { do stuff } placeToStoreResult ... vwait placeToStoreResult do stuff with result $placeToStoreResult
- typon 8y agoBut then you have to code in... Tcl...
- adamwiggins 8y agoGreat project. My team used it for a live-coding Python notebook: https://github.com/inkandswitch/livebook#readme https://github.com/inkandswitch/livebook#readme To my surprise it was faster than Jupyter / CPython for some tasks (e.g. loading and parsing large CSVs).
- dunpeal 8y agoShouldn't be too surprising, given how PyPy is generally faster than CPython by about x7-8 factor: http://speed.pypy.org/ http://speed.pypy.org/ I'd expect a much larger difference in tasks that are pure Python (loading a CSV is done primarily through the csv module, i.e. all the hot loops are in C).
- icebraining 8y agoYes, but PyPy is usually compiled to machine code (and emits machine code), not to JS.
- make3 8y agoI love Python but js is close to an order of magnitude faster
- icebraining 8y agoHave you tried PyPy?
- 8y ago
- deleted 8y ago[deleted]
- parabx 8y agothis is pretty neat! I was wondering, are there any projects to transpile python to js, but targeting the server side? It would be interesting to port a flask application, for example, to run on node and benefit from the jit, or maybe use express and call flask through its wsgi interface.
- Skunkleton 8y agoWhy not just run these in their native environment?
- icebraining 8y agoYou're better off running it on the original version of PyPy, which already uses a JIT.
- msoad 8y agoJeff Atwood law of Javascript everything that can be written in JS will be written in JS!!
- jokoon 8y agoseems brython is more appropriate, mature and usable.
- fhood 8y ago>>> print "hello" hello >>>import json >>> Dope, everything I need.
- nichochar 8y agoThis is awesome. They mention that it is a "compliant" implementation of python. How does one go about proving this?
- tathougies 8y agoThere's no python standard, but my guess is that-- since this is based on the same source code that native PyPy is, with a different compiler backend -- they are making the claim based on the fact that native PyPy is widely considered to be similar enough to CPython for many workloads. Thus -- assuming the correctness of the RPython -> JavaScript transformation (which they have not proven, and would be a monumental task) -- you should expect this version to work like the native PyPy.
- inetknght 8y ago* >>> import sys >>> sys.version '2.7.9 (?, Jul 03 2015, 17:08:29)\n[PyPy 2.6.0]' * Why, exactly, does this have such a garbage-ancient version of Python?
- Alupis 8y agoDidn't we decide a long while back that fully featured programming languages were dangerous in the browser, especially if they could do disk I/O? Even if sandboxed? A la Java Applets and Adobe Flash? import os for subdir, dirs, files in os.walk('./'): for file in files: print file os.remove(file) Perhaps the environment is "fake", and these files don't really exist, even in some sandbox... otherwise, seems like this might pose a security risk.
- Klathmon 8y agoI don't know about PyPy.js specifically, but most systems like this provide a virtual filesystem that maps back to things like indexedDB and localstorage which are browser APIs. There is no way that pypy.js is able to access local files on your machine outside of the browser sandbox.
- chillydawg 8y agoI bet there's a few cheeky hackers around the world who may disagree with that statement. Always assume it's broken.
- russx2 8y agoNo, not in this case. It's running in JS land in the browser so any exploits to break out of the browser sandbox using this could be done simpler with raw JS.
- star-techate 8y ago1. I'll copy&paste this to test it! 2. uh ok I'll paste it into an editor and remove the added indentation and then copy&paste that to test it 3. ... ok. I'll look at the code and reformat every single line and copy&paste that the oldest complaint abides
- lucb1e 8y agoThis is Javascript, not real Python. If you're so afraid that this is dangerous, why do you post malicious code? And if you're so clever to post that code, why don't you try similar code to see if it really has access to your files? import os os.listdir('/') The answer, by the way, is no.
- lucb1e 8y agoThe speed is surprisingly reasonable. Testing with a simple loop, I get about 1.3-1.4 million loops per second in python3, 2 million loops per second in python2, 14.5 million loops in pypy, and 0.61 million loops in the browser. My code basically calls int(time.time()) until its value changes, doing i+=1 for every time it did not change (n=10 for every platform (python2/3/pypy/browser)). My browser is Firefox 61, full code here: https://hastebin.com/turenofise.py https://hastebin.com/turenofise.py
- tim333 8y agoThough trying to add the numbers for 1 to a million in a loop javascript was about 10 - 20 times faster when I tried it.
- deleted 8y ago[deleted]
- sametmax 8y agoYou would use sum(range()) in a Python though. Remember that many things you do manually in JS have a better, automatic and faster way in the stlib. Besides, if calculations are important, you would use numpy.
- tim333 8y agoAh yes. sum(range()) in Python vs for(i=1...) in js was much closer, about 20% quicker in javascript.
- gengkev 8y agoWon't this calculation overflow to +Infinity in JavaScript after a while? On the other hand, Python will try to compute the actual result using arbitrary precision integers, which is that's slower.
- deleted 8y ago[deleted]
- wietze 8y agoThe most important import doesn't work, unfortunately: import antigravity
- veganjay 8y agoThere is a similar Python interpreter here: http://www.codeskulptor.org/ http://www.codeskulptor.org/ It is used by a class taught on Coursera (Introduction to Interactive Programming in Python) From what I can see: - PyPy.js benefit: runs faster - codeskulptor benefit: can use GUI components
- guilingmaster 8y agohttps://pastebin.com/NGUVe7zi https://pastebin.com/NGUVe7zi
- hathawsh 8y agoI'm curious about the virtual filesystem: Welcome to PyPy.js! >>> import os >>> os.listdir('/') ['tmp', 'home', 'dev', 'lib'] >>> f = open('/what', 'w') >>> f.write('hey') >>> f.close() >>> os.listdir('/') ['tmp', 'home', 'dev', 'lib', 'what'] >>> open('/what').read() 'hey' What is in the stack that makes that work?
- mattigames 8y agoThe Emscripten File System API, "The API is inspired by the Linux/POSIX File System API, with each presenting a very similar interface." https://kripken.github.io/emscripten-site/docs/api_reference/Filesystem-API.html https://kripken.github.io/emscripten-site/docs/api_reference...
- hathawsh 8y agoThanks!
- kjeetgill 8y agoI believe emscripten provides a virtual filesystem interface. Since it's essentially translating/running c it's either hooking through glibc or the syscall interfaces.
- deleted 8y ago[deleted]
- matmann2001 8y ago>>> import os >>> for root, dirs, files in os.walk('/'): ... for d in dirs: ... print os.path.join(root, d) + '/' ... for f in files: ... print os.path.join(root, f) ... /tmp/ /home/ /dev/ /lib/ /home/web_user/ /dev/shm/ /dev/null /dev/tty /dev/tty1 /dev/random /dev/urandom /dev/stdin /dev/stdout /dev/stderr /dev/shm/tmp/ /lib/pypyjs/ /lib/pypyjs/lib_pypy/ /lib/pypyjs/lib-python/ /lib/pypyjs/lib_pypy/encodings/ /lib/pypyjs/lib_pypy/UserDict.py /lib/pypyjs/lib_pypy/__future__.py /lib/pypyjs/lib_pypy/_abcoll.py /lib/pypyjs/lib_pypy/_structseq.py /lib/pypyjs/lib_pypy/_weakrefset.py /lib/pypyjs/lib_pypy/abc.py /lib/pypyjs/lib_pypy/base64.py /lib/pypyjs/lib_pypy/code.py /lib/pypyjs/lib_pypy/codecs.py /lib/pypyjs/lib_pypy/codeop.py /lib/pypyjs/lib_pypy/copy.py /lib/pypyjs/lib_pypy/copy_reg.py /lib/pypyjs/lib_pypy/genericpath.py /lib/pypyjs/lib_pypy/getopt.py /lib/pypyjs/lib_pypy/linecache.py /lib/pypyjs/lib_pypy/os.py /lib/pypyjs/lib_pypy/posixpath.py /lib/pypyjs/lib_pypy/pwd.py /lib/pypyjs/lib_pypy/re.py /lib/pypyjs/lib_pypy/repr.py /lib/pypyjs/lib_pypy/sre_compile.py /lib/pypyjs/lib_pypy/sre_constants.py /lib/pypyjs/lib_pypy/sre_parse.py /lib/pypyjs/lib_pypy/stat.py /lib/pypyjs/lib_pypy/struct.py /lib/pypyjs/lib_pypy/traceback.py /lib/pypyjs/lib_pypy/types.py /lib/pypyjs/lib_pypy/warnings.py /lib/pypyjs/lib_pypy/weakref.py /lib/pypyjs/lib_pypy/encodings/__init__.py /lib/pypyjs/lib_pypy/encodings/aliases.py /lib/pypyjs/lib_pypy/encodings/ascii.py /lib/pypyjs/lib_pypy/encodings/base64_codec.py /lib/pypyjs/lib_pypy/encodings/hex_codec.py /lib/pypyjs/lib_pypy/encodings/latin_1.py /lib/pypyjs/lib_pypy/encodings/raw_unicode_escape.py /lib/pypyjs/lib_pypy/encodings/string_escape.py /lib/pypyjs/lib_pypy/encodings/unicode_escape.py /lib/pypyjs/lib_pypy/encodings/unicode_internal.py /lib/pypyjs/lib_pypy/encodings/utf_16.py /lib/pypyjs/lib_pypy/encodings/utf_8.py /lib/pypyjs/lib-python/2.7/
- ipsun4 8y agoTried Fibonacci, and the thing crashed for 100. I'm not sure that a wasm python library can do any heavy lifting.
- cutler 8y agoFile "<console>", line 2 return f'{fname} {surname} is {age}.' ^ SyntaxError: invalid syntax So this is a subset of Python or something like that?
- Nicksil 8y agoI believe this feature was introduced in Python 3.6. This implementation may be 3.6 < Edit: Just checked the site and see it uses PyPy which states -- on their site -- the python version is 3.5.3
- zorkw4rg 8y agoI can't do: a, *b = range(3) which should be 3.5 i think.
- Nicksil 8y agoOther comments in this thread point to the implementation likely being Python 2.x.
- Zopieux 8y agoOr you know, just import sys and print sys.version instead of reverse-engineering it from the syntactic features introduced over time.
- trumped 8y agois that interpreter faster then the default one?
- mfoy_ 8y agoWould it be sane to use this to run Python modules in node.js?
- kodablah 8y agoIf the Python module was large and complex, it does something that would be incredibly difficult to replicate in nodejs either given the current ecosystem or costs of a rewrite, is not available in Rust/C/C++ that could be WASM'd (not mentioning others here due to large WASM overhead), you accept the large binary sizes of these things (i.e. you're not embedding the Python interpreter/runtime and putting on npm), and you're willing to put a node-friendly layer on top that makes the lower level calls...then it might be sane. But I'd consider modernizing to an updated emscripten that does WASM, see if you can avoid some emscripten runtime (i.e. WASM standalone compilation). For all other cases, stay in Node or use a lib from a language more suited to WASM compilation. Now, if it's an option between this and a native Node module, that depends on your opinion of Node FFI and native Node modules.
- jononor 8y agoIf you have significant amounts of Python code to reuse in another language, I would recommend: A) Run it as a subprocess B) Call it over HTTP request/response C) Use a message broker like AMQP
- imh 8y agoReally amazing. I use python every day at work, and most of the crucial libraries have tight inner loops written in C or Cython. I wonder what the web assembly future holds for those languages, and inter-language FFI.
- slezyr 8y ago>>> os.getlogin() 'root' Nice joke :)
- amasad 8y agoIt's really awesome to see how far this has come. I believe we at Repl.it were the first to try something like this in production. We emscriptined CPython to JavaScript and contributed quite a bit to the project in the process (including the initial virtual filesystem implementation). https://github.com/replit-archive/empythoned https://github.com/replit-archive/empythoned I deployed this at scale at Codecademy were millions of users were using it to learn Python but hit a lot of problems with it. For one, if you're in a 3rd world country then the bundle size is a non-starter (this is potentially solved with WASM being a binary format). Even if you managed to download the bundle lots of old computers would run out of memory trying to parse the JS (again probably solved by the binary AST format of WASM). Because of this and a few other issues (the dev experience of emscripten was really hard) we had to move away to running code on remote containers. Now that I'm back working on Repl.it full time I'm excited to play again with the tech and see what we can do with it this time around.
- flanbiscuit 8y agoI just wanted to say how much I love Repl.it and use it all the time. I use it as a testing scratchpad mostly but I know it's much powerful than that. I appreciate its simplicity. Thanks for a great product!
- amasad 8y agoThanks so much :)
- jumasheff 8y agoThank you for your work at Codecademy -- a place where I learned how to program. I'm working through my 4th year as a developer :)
- askvictor 8y agoIs there an up-to-date/maintained comparison of python-in-the-browser systems? As well as this, there's brython and skulpt, and it would be nice to be able to compare various implemented features, quirks and speed.
- deleted 8y ago[deleted]