├── .gitmodules ├── .nojekyll ├── README.creole ├── mandelbrot-nojit.html ├── mandelbrot.html ├── mandelbrot.py ├── mandelbrot_enhanced-nojit.html ├── mandelbrot_enhanced.html ├── mandelbrot_enhanced.py ├── minimal_console.html └── simple_http_server.py /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "pypyjs-release"] 2 | path = pypyjs-release 3 | url = https://github.com/pypyjs/pypyjs-release.git 4 | [submodule "pypyjs-release-nojit"] 5 | path = pypyjs-release-nojit 6 | url = https://github.com/pypyjs/pypyjs-release-nojit.git 7 | -------------------------------------------------------------------------------- /.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pypyjs/pypyjs-examples/9fac2d37b0339e50ba30322fa77ee89a307c855c/.nojekyll -------------------------------------------------------------------------------- /README.creole: -------------------------------------------------------------------------------- 1 | 2 | === PyPy.js examples 3 | 4 | **Note:** Development is sleeping at the moment. Discuss this here: https://github.com/pypyjs/pypyjs/issues/213 5 | 6 | This git repository contains some example usage of PyPy.js 7 | 8 | It used the git repro https://github.com/pypyjs/pypyjs-release (and the no-JIT) as a git submodule. 9 | 10 | 11 | Please fork and contribute own examples/snippets! 12 | 13 | 14 | ==== Small snippets: 15 | 16 | Here some small snippets for copy&paste into [[http://pypyjs.org/editor.html|PyPyJS-Editor]]. 17 | 18 | Display some browser information from JavaScript object **navigator**: 19 | {{{ 20 | import js 21 | navigator = js.globals["navigator"] 22 | for js_string in dir(navigator): 23 | attr_name = str(js_string) # FIXME 24 | attr = getattr(navigator, attr_name, "-") 25 | if not isinstance(attr, (js.String, js.Array, js.Boolean)): 26 | continue 27 | print "%20s: %s" % (attr_name, attr) 28 | }}} 29 | 30 | 31 | Create html buttons and bind the click event with jQuery: 32 | {{{ 33 | import js # https://github.com/pypyjs/pypy/tree/pypyjs/pypy/module/js 34 | 35 | jquery = js.globals["$"] 36 | for i in range(10): 37 | jquery("#editor").before('' % (i,i)) 38 | 39 | @js.Function 40 | def click_handler(event): 41 | print "You clicked on button with ID:", 42 | print event.target.id 43 | 44 | jquery("button").click(click_handler) 45 | 46 | print "Please click on new button above!" 47 | }}} 48 | 49 | 50 | Display information about a //js// object: 51 | {{{ 52 | from __future__ import print_function 53 | import js # https://github.com/pypyjs/pypy/tree/pypyjs/pypy/module/js 54 | 55 | def pprint_js_object(obj): 56 | print("Object:", obj, type(obj), obj.__doc__) 57 | print("dir:", dir(obj)) 58 | print("Existing object attributes:") 59 | for attr_name in obj: 60 | attr_name = str(attr_name) # convert js.String https://github.com/pypyjs/pypy/issues/2 61 | print("%30s:" % attr_name, end="") 62 | try: 63 | value = getattr(obj, attr_name) 64 | except Exception as err: 65 | print("[Error: %s]" % err) 66 | else: 67 | value = str(value) # evaluate js.Handle 68 | value = repr(value) # e.g. escape newlines 69 | if len(value)>70: 70 | value = "%s..." % value[:70] 71 | print(value) 72 | 73 | window = js.globals.window 74 | pprint_js_object(window) 75 | print(" --- END --- ") 76 | }}} 77 | 78 | Some system information: 79 | {{{ 80 | import os 81 | 82 | methods=( 83 | "getlogin", "getuid", "getgid", 84 | "getpid", "getpgrp", "getppid", 85 | "geteuid", "getegid", 86 | ) 87 | for method in methods: 88 | func = getattr(os, method) 89 | doc = func.__doc__.split("\n\n")[-1].strip() 90 | result = func() 91 | print "%12s: %-10s %s" % (method, result, doc) 92 | }}} 93 | 94 | 95 | Manipulating the virtual file system: 96 | {{{ 97 | import os 98 | 99 | LIB_PATH = "/lib/pypyjs/lib_pypy/" 100 | 101 | with open(os.path.join(LIB_PATH, "my_module.py"), "w") as f: 102 | f.write("x='hi'") 103 | 104 | import my_module 105 | 106 | print my_module.x # hi 107 | print "my_module.__file__:", my_module.__file__ # /lib/pypyjs/lib_pypy/my_module.py 108 | 109 | import os 110 | for filename in os.listdir(LIB_PATH): # Will list my_module.py and my_module.pyc 111 | if filename.startswith("my"): 112 | filepath = os.path.join(LIB_PATH, filename) 113 | print "*", filename, os.stat(filepath) 114 | }}} 115 | (Currently there is no public API for manipulating the virtual filesystem, see also: https://github.com/pypyjs/pypyjs/issues/132 ) 116 | 117 | ==== Existing examples: 118 | 119 | ===== Mandelbrot 120 | 121 | Render Mandelbrot fractal in browser via PyPy.js 122 | 123 | * [[http://pypyjs.org/pypyjs-examples/mandelbrot.html|/mandelbrot.html]] 124 | * [[http://pypyjs.org/pypyjs-examples/mandelbrot-nojit.html|/mandelbrot-nojit.html]] (without PyPy JIT) 125 | 126 | WIP: 127 | 128 | * [[http://pypyjs.org/pypyjs-examples/mandelbrot_enhanced.html|/mandelbrot_enhanced.html]] 129 | * [[http://pypyjs.org/pypyjs-examples/mandelbrot_enhanced-nojit.html|/mandelbrot_enhanced-nojit.html]] 130 | 131 | 132 | ===== minimal console 133 | 134 | A minimal console with the aim to use as few external dependencies. 135 | A full featured console, that used **jq-console** is here: [[http://pypyjs.org|pypyjs.org]] 136 | 137 | * [[http://pypyjs.org/pypyjs-examples/minimal_console.html|/minimal_console.html]] 138 | 139 | 140 | ==== try out at home 141 | 142 | There is a [[http://pypyjs.org/pypyjs-examples/simple_http_server.py|/simple_http_server.py]] for test this repro at home. 143 | 144 | This is needed for 'mandelbrot' example and you can better see file requests. 145 | 146 | Just do it e.g.: 147 | {{{ 148 | ~$ git clone https://github.com/pypyjs/pypyjs-examples.git 149 | ~$ cd pypyjs-examples 150 | ~/pypyjs-examples$ git submodule init 151 | ~/pypyjs-examples$ git submodule update 152 | ~/pypyjs-examples$ python simple_http_server.py 153 | }}} 154 | The server worked with Python 2 and 3 and starts at [[http://127.0.0.1:8000]]. 155 | 156 | 157 | === What is PyPy.js ? 158 | 159 | In short: PyPy compiled to JavaScript 160 | 161 | Little bit longer: PyPy.js is an experiment in building a fast and compliant in-browser python interpreter, by compiling PyPy into javascript and retargeting its JIT to emit javascript code at runtime. 162 | 163 | More info: http://pypyjs.org/ 164 | 165 | 166 | === Links 167 | 168 | * [[http://salvatore.diodev.fr/pypybox|PyPyBox]] - create 2D graphics in pure Python 169 | 170 | 171 | === Repository Overview 172 | 173 | | [[https://github.com/pypyjs/pypyjs|pypyjs]] | Main repository to built a PyPyJS release 174 | | [[https://github.com/pypyjs/pypy|pypy]] | Fork of PyPy with support for compiling to javascript 175 | | [[https://github.com/pypyjs/pypyjs-release|pypyjs-release]] | Latest release build of PyPyJS, as a handy git submodule 176 | | [[https://github.com/pypyjs/pypyjs-release-nojit|pypyjs-release-nojit]] | Latest release build of PyPyJS, without a JIT 177 | | [[https://github.com/pypyjs/pypyjs-examples|pypyjs-examples]] | Examples/snippets usage of **pypyjs-release** and **pypyjs-release-nojit** 178 | | [[https://github.com/pypyjs/pypyjs.github.io|pypyjs.github.io]] | source for **pypyjs.org** website use **pypyjs-release** and **pypyjs-release-nojit** 179 | 180 | 181 | -------------------------------------------------------------------------------- /mandelbrot-nojit.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 |23 | Render Mandelbrot fractal with PyPy.js 24 |
25 |
30 | PyPy.js is an experiment in building a fast and compliant python environment for the web.
31 |
32 | It uses the PyPy python interpreter, compiled for the web via
33 | emscripten, with a custom JIT backend that emits asm.js
34 | code at runtime.
35 |
23 | Render Mandelbrot fractal with PyPy.js 24 |
25 |
30 | PyPy.js is an experiment in building a fast and compliant python environment for the web.
31 |
32 | It uses the PyPy python interpreter, compiled for the web via
33 | emscripten, with a custom JIT backend that emits asm.js
34 | code at runtime.
35 |
89 | PyPy.js is an experiment in building a fast and compliant python environment for the web.
90 |
91 | It uses the PyPy python interpreter, compiled for the web via
92 | emscripten, with a custom JIT backend that emits asm.js
93 | code at runtime.
94 |