OMG: pyodide & webassembly

72 views
Skip to first unread message

Edward K. Ream

unread,
Jan 2, 2019, 11:05:49 AM1/2/19
to leo-e...@googlegroups.com
We can program, right now, web pages using Python!

Imagine my surprise. While studying the python sources in live-py-plugin/plugin/PySrc/code_tracer.py I ran across JS code!

def web_main():
 display
()
 document
.getElementById('source').addEventListener('input', display)

Huh? Here is the top-level code:

if __name__ == '__main__':
     main
()
elif IS_PYODIDE:
     web_main
()

Googling pyodide yields this page. From the readme file at the bottom:

The Python scientific stack, compiled to WebAssembly. It provides transparent conversion of objects between Javascript and Python. When inside a browser, this means Python has full access to the Web APIs.

OMG: WebAssembly is a web standard and is supported in all major browsers!! From the MDN page:

QQQ
WebAssembly is a new type of code that can be run in modern web browsers—it is a low-level assembly-like language with a compact binary format that runs with near-native performance and provides languages such as C/C++ with a compilation target so that they can run on the web. It is also designed to run alongside JavaScript, allowing both to work together.

WebAssembly has huge implications for the web platform—it provides a way to run code written in multiple languages on the web at near native speed, with client apps running on the web that previously couldn’t have done so.
QQQ

From the wikipedia page:

QQQ
WebAssembly is a web standard that defines a binary format and a corresponding assembly-like text format for executable code in Web pages. It is meant to enable executing code nearly as quickly as running native machine code. It was envisioned to complement JavaScript to speed up performance-critical parts of web applications and later on to enable web development in languages other than JavaScript. WebAssembly does not attempt to replace JavaScript, but to complement it. It is developed at the World Wide Web Consortium (W3C) with engineers from Mozilla, Microsoft, Google, and Apple.
QQQ

This looks like the way around the lack of browser support for python.

Edward

Matt Wilkie

unread,
Jan 2, 2019, 2:02:13 PM1/2/19
to leo-editor

This [WebAssembly] looks like the way around the lack of browser support for python.

Oh yes please!

My current home project is to scrape our personal local ISP account web page for bandwidth vs quota usage stats and report. In python it's pretty small, and I even managed to get a Flexx reporting page built. However it doesn't work on our phones because the script relies on Beautiful Soup module which Flexx can't help with (I think). I've been looking at Kivy's Python-for-android and Buildozer stack to build an app version with steadily sinking spirit. It's a multi-gigabyte download just to get started.

Matt
((starts reading up on web assembly and pyiodide))

Edward K. Ream

unread,
Jan 3, 2019, 10:48:02 AM1/3/19
to leo-editor
On Wednesday, January 2, 2019 at 1:02:13 PM UTC-6, Matt Wilkie wrote:

This [WebAssembly] looks like the way around the lack of browser support for python.

Oh yes please!

It won't happen immediately, but in the meantime I highly recommend two videos:

This is a pretty good overview of a graduate level course in compilers and optimization!

A good overview of the present state of wasm (WebAssembly)

The Python devs will decide what to do when wasm is ready (threads, gc), but the following ideas come to my mind:

1. Just as JS is not a very good assembly language for JS, Python is a poor assembly language for Python ;-) Wasm will work for Python as well as JS. This might be the knell for PYPY.  Or not. There is already a PYPY back end for asm.js, the precursor to wasm, but imo the whole PYPY project looks a bit suspect now.

2. One can easily imagine CPython converted to WasmPython. I suspect this will happen relatively soon.  One can also imagine Python's C API converted to a Wasm API.  There might be a performance boost available in doing so, because of a better "impedance match" between the WasmPython interp and wasm extension modules.  Or not.

3. WasmPython will surely be based on Python 3, not 2.  This should speed the transition to 3, a very good thing.

Edward
Reply all
Reply to author
Forward
0 new messages