the future of python on the web - fossee

43
The future of Python on the Web

Upload: others

Post on 28-Apr-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The future of Python on the Web - FOSSEE

The future of Python on the Web

Page 2: The future of Python on the Web - FOSSEE

My data journey

2

Page 4: The future of Python on the Web - FOSSEE

4

Page 5: The future of Python on the Web - FOSSEE

5

Page 6: The future of Python on the Web - FOSSEE

6

Page 7: The future of Python on the Web - FOSSEE

7

Page 8: The future of Python on the Web - FOSSEE

8

Page 9: The future of Python on the Web - FOSSEE

Lean Data Practices

9https://www.mozilla.org/en-US/about/policy/lean-data/

Page 10: The future of Python on the Web - FOSSEE

10

universal (potentially)specific

vs.

18GB / day 2TB / day

Page 11: The future of Python on the Web - FOSSEE

Mozilla Confidential

Communicating about Data Science

11

Page 12: The future of Python on the Web - FOSSEE

The lifecycle of data science

1212

Exploration

ExplanationCollaboration

Exploration and Explanation in Computational Notebooks

Page 13: The future of Python on the Web - FOSSEE

13

Page 14: The future of Python on the Web - FOSSEE

Iodide modelJupyter-like model

14

Architecture

Adapted from: https://jupyter.readthedocs.io/en/latest/architecture/how_jupyter_ipython_work.html#notebooks

Browser

Server Kernel

Browser

Kernel

Server

Data

Data

Data

DataUI

UI Data

Remote Compute (optional)

Kernel

Page 15: The future of Python on the Web - FOSSEE

15

Page 16: The future of Python on the Web - FOSSEE

16

iomd

● Human readable and editable

● Easy for programs to support

● Diffable with standard tools

● See Matlab cell mode, R Markdown, Jupytext (and many others)

%% md# This is a markdown header

%% jsel = document.getElementById(“foo”)

%% pyfrom js import elel.text = “Hello World!”

Page 17: The future of Python on the Web - FOSSEE

17

Javascript

PROS CONS

FAST: Some of the best compiler technology of any dynamic language Legacy “rough edges”

Familiar to many programmers Not familiar to many data scientists

Large selection of user interface and visualization tools Lacks a mature data science ecosystem

Page 18: The future of Python on the Web - FOSSEE

18

��What if we could bring Python to the browser?

Page 19: The future of Python on the Web - FOSSEE

19

Convert Python to Javascript

Transpiling

Python

def fib(n): if n == 1: return 0 elif n == 2: return 1 else: return fib(n - 1) + fib(n - 2)

Javascript

export var fib = function(n) { if (n == 1) return 0; else if (n == 2) return 1; else return fib(n - 1) + fib(n - 2)};

transcrypt, pyjs

Page 20: The future of Python on the Web - FOSSEE

● Small

● Fast

● Server-side "ahead of time"

● Subtly different semantics

● Covering all of CPython's functionality is a lot of work

● Keeping up with CPython's progress is a lot of work

● No support for C extensions (Numpy, Scipy, etc.)

Convert Python to Javascript

Transpiling

20

Pros Cons

Page 21: The future of Python on the Web - FOSSEE

21

Rewrite the Python interpreter and VM in Javascript

Interpreter Porting

C

static intset_add_entry( PySetObject *so, PyObject *key, Py_hash_t hash){ while (1) { if (entry->hash == hash) { PyObject *startkey = entry->key; assert(startkey != dummy); if (startkey == key) goto found_active;

Javascript

function $add(self, item){ self.$items.push(item) var value = item.valueOf() if(typeof value == "number"){ self.$numbers.push(value) }

brython, skulpt, batavia

Page 22: The future of Python on the Web - FOSSEE

● Can compile and run Python entirely in the browser

● Can embed a transpiler in the browser for a hybrid approach

● Larger download and slower startup than transpiling

● Subtly different semantics

● Covering all of CPython's functionality is a lot of work

● Keeping up with CPython's progress is a lot of work

● No support for C extensions

Rewrite Python interpreter and VM in Javascript

Interpreter Porting

22

Pros Cons

Page 23: The future of Python on the Web - FOSSEE

WebAssembly

23

Page 24: The future of Python on the Web - FOSSEE

24

Recompile the Python interpreter to WebAssembly

Compile to WebAssembly

C

static intset_add_entry( PySetObject *so, PyObject *key, Py_hash_t hash){ while (1) { if (entry->hash == hash) { PyObject *startkey = entry->key; assert(startkey != dummy); if (startkey == key) goto found_active;

WebAssembly

(func (;1839;) (type 4) (param i32 i32 i32) (result i32) (local i32 i32 i32 i32 i32 i32 i32 i32 i32 i32) ... if ;; label = @1 block ;; label = @2 block ;; label = @3 block ;; label = @4 loop ;; label = @5 block ;; label = @6 block (result i32) ;; label = @7 block ;; label = @8

PyPy.js, cpython-wasm, Pyodide

Page 25: The future of Python on the Web - FOSSEE

● It's the same as upstream CPython

● Everything that can work does work

● Supports C extensions (Numpy, Scipy etc.)

● Performance on par with native code

● Very large download sizes

● High memory usage

Recompile the Python interpreter to WebAssembly

Compile to WebAssembly

25

Pros Cons

Page 26: The future of Python on the Web - FOSSEE

26

Tradeoffs

Transpiling Porting Recompiling interpreter

Download size Small Medium Large

Memory usage Small Medium Large

Similarity to upstream Low Medium High

Easily track upstream changes

Supports C extensions ✅

Page 27: The future of Python on the Web - FOSSEE

27

The scientific Python stack, compiled to WebAssembly

Pyodide

Page 28: The future of Python on the Web - FOSSEE

28

● Upstream CPython

● numpy, pandas, matplotlib, scipy

● "pip install" pure Python wheels

Pyodide

Page 29: The future of Python on the Web - FOSSEE

29

CPython

Your Python Code

Emscripten system abstraction

Javascript interpreter

DOM APIs

Pyodide Python Extension

Page 30: The future of Python on the Web - FOSSEE

30

Page 31: The future of Python on the Web - FOSSEE

31

Accelerating Python

Input Process Output

C extension

Cython

Numba

JavaScriptConversion Conversion

Page 32: The future of Python on the Web - FOSSEE

32

Sharing arrays with zero copying Future?

Page 33: The future of Python on the Web - FOSSEE

The Web API

33

● DOM

● Graphics: Canvas, WebGL

● Audio: WebAudio, WebRTC

● Video: HTMLMediaElement

● Device: Notifications, WebBluetooth

● Storage: Client-side storage

Page 34: The future of Python on the Web - FOSSEE

34

Pyodide Demo

Page 35: The future of Python on the Web - FOSSEE

Performance

35https://github.com/serge-sans-paille/numpy-benchmarks

Page 36: The future of Python on the Web - FOSSEE

36

● Cython● Numba● PyPy● Apache Arrow● General purpose GPU● Distributed computing

Ways to get more performance

Page 37: The future of Python on the Web - FOSSEE

● Raw network sockets

● Subprocesses

● Access to the host filesystem

What doesn't work

37

Probably never Someday

● threads

● async

● SIMD

● General Purpose GPU computing

Page 38: The future of Python on the Web - FOSSEE

Monolithic Libraries

38

package Total size Loaded at import

Scipy 65MB 11MB

Pandas 50MB 43MB

Matplotlib 20MB 13MB

Numpy 20MB 11MB

* values are for native x86_64 Python

Page 39: The future of Python on the Web - FOSSEE

conda forge infrastructure for package building

39

Future Directions

Page 40: The future of Python on the Web - FOSSEE

Language interoperability

Future directions

40

PythonJavascript

Apache Arrow /

libndtype

R

OCaml

JSX

Typescript

Rust

⬛ Works today

⬛ Planned

Text in/out only

Ruby

Lua

Julia

Page 41: The future of Python on the Web - FOSSEE

Come build with us!

41

We're open source on github

http://github.com/iodide-project/

We need:● Experimenters● Designers● Programmers● Writers● Bug hunters

Page 42: The future of Python on the Web - FOSSEE

Roman YurchakKirill Smelkov

Madhur Tandon

...and many other community contributors

42

Brendan ColloranHamilton UlmerWilliam LachanceMichael DroettboomTeon BrooksJohn KarahalisRob MillerJannis Leidel...

Our team

Devin Bayly

Dhiraj Barnwal

Page 43: The future of Python on the Web - FOSSEE

iodide.io

github.com/iodide-project

43

[email protected]