<jupyter_start><jupyter_text>Let’s embrace WebAssembly![Presentation made at EuroPython 2018 - Edinburgh (by <NAME>)](https://www.youtube.com/watch?v=u2kKxmb9BWs&index=18&list=PL8uoeex94UhFrNUV2m5MigREebUms39U5)This Notebook is:- a compacted version of original https://github.com/almarklein/rocket_rust_py- best seen in RISE mode:   WASM has a compact binary format And a human readable counterpart:```wasm(module    (type $print (func (param i32))    (func $main        (i32.const 42)        (call $print)    )    (start $main))```  It's safeBecause browsers.  Use case 1: Compile a subset of Python to WASM<jupyter_code># in RISE mode, click <Shift>+<Enter> to execute a cell
def find_prime(nth):
    n = 0
    i = -1       
    while n < nth:
        i = i + 1        
        if i <= 1:
            continue  # nope
        elif i == 2:
            n = n + 1
        else:
            gotit = 1
            for j in range(2,  i//2+1):
                if i % j == 0:
                    gotit = 0
                    break
            if gotit == 1:
                n = n + 1    
    return i
%time find_prime(1000)<jupyter_output><empty_output><jupyter_text>Compiling 'find_prime()' to  WASM Note: * The python-to-wasm compiler is just a POC!* Assumes a (reliable) wasm-to-native compiler<jupyter_code># in RISE mode, click <Shift>+<Enter> to execute a cell
from ppci import wasm
from ppci.lang.python import python_to_wasm

def main():
    print(find_prime(1000))

m = python_to_wasm(main, find_prime)
# WASM (somewhat) readable machine code
m.show()
# WASM binary format
m.show_bytes()
# WASM interface
m.show_interface()<jupyter_output><empty_output><jupyter_text>Run in Browser<jupyter_code>wasm.run_wasm_in_notebook(m)<jupyter_output><empty_output><jupyter_text>Run in NodeJS<jupyter_code>wasm.run_wasm_in_node(m)<jupyter_output><empty_output><jupyter_text>Compile 'find_prime()' to WASM then to Machine code<jupyter_code># this doesn't currently work on a Python 32 bit, when run on a Windows 64 bit
@wasm.wasmify
def find_prime2(nth):
    n = 0
    i = -1       
    while n < nth:
        i = i + 1        
        if i <= 1:
            continue  # nope
        elif i == 2:
            n = n + 1
        else:
            gotit = 1
            for j in range(2,  i//2+1):
                if i % j == 0:
                    gotit = 0
                    break
            if gotit == 1:
                n = n + 1    
    return i
%time find_prime2(1000)<jupyter_output><empty_output><jupyter_text>Use case 2: Python as a platform to bind and run WASM modules    ... and allow that code to call into Python functions   the Rocket game from [github.com/aochagavia](https://github.com/aochagavia/rocket_wasm)     -->              The rocket game is in a single binary WASM file (58 KB)<jupyter_code>from ppci import wasm

m = wasm.Module(open(r'wasm/rocket.wasm', 'rb'))
m
m.show_interface()<jupyter_output><empty_output><jupyter_text>rocket wasm controlled per javascript  abstract of rocket.html file  same rocket wasm controlled per python<jupyter_code># abstract of rocket_qt.py (do not run)
class PythonRocketGame:
    
    # ...
    
    def wasm_sin(self, a:float) -> float:
        return math.sin(a)
    
    def wasm_cos(self, a:float) -> float:
        return math.cos(a)
    
    def wasm_Math_atan(self, a:float) -> float:
        return math.atan(a)
    
    def wasm_clear_screen(self) -> None:
        # ...
    
    def wasm_draw_bullet(self, x:float, y:float) -> None:
        # ...
    
    def wasm_draw_enemy(self, x:float, y:float) -> None:
        # ...
    
    def wasm_draw_particle(self, x:float, y:float, a:float) -> None:
        # ...
    
    def wasm_draw_player(self, x:float, y:float, a:float) -> None:
       # ...
    
    def wasm_draw_score(self, score:float) -> None:
        # ...<jupyter_output><empty_output><jupyter_text>Run Rocket in Python with Qt<jupyter_code>from rocket_qt import QtRocketGame
game = QtRocketGame()
# you may have to switch to the QT window appearing on the side of your browser session
game.run()<jupyter_output><empty_output><jupyter_text>This game is not that hard to play ...  Let's make an AI!  Game in rust compiled in WASM AI in C compiled in WASM Glue in Python<jupyter_code># let's write the AI in C
print(open('wasm/ai2.c', 'rt').read())
# use https://wasdk.github.io/WasmFiddle/ to convert ai.c in ai2.wasm
from ppci import wasm
ai2 = wasm.Module(open('wasm/ai2.wasm', 'rb'))
ai2.show_interface()
from rocket_ai import AiRocketGame
game = AiRocketGame(ai2)
game.run()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10953523892646738, 'fraction_numerical': 0.0170194195941523, 'mean_word_length': 2.673076923076923, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 52, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '410947', 'license': 'MIT', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1586, 'n_words': 445, 'path': 'docs/WASM_almar_klein_demo/EuroPython2018_talk_compacted.ipynb', 'repo_name': 'larssp/winpython_afterdoc', 'size': '4588'}