Note: Development is sleeping at the moment. Discuss this here: pypyjs/pypyjs#213
This git repository contains some example usage of PyPy.js
It used the git repro https://github.com/pypyjs/pypyjs-release (and the no-JIT) as a git submodule.
Please fork and contribute own examples/snippets!
Here some small snippets for copy&paste into PyPyJS-Editor.
Display some browser information from JavaScript object navigator:
import js
navigator = js.globals["navigator"]
for js_string in dir(navigator):
attr_name = str(js_string) # FIXME
attr = getattr(navigator, attr_name, "-")
if not isinstance(attr, (js.String, js.Array, js.Boolean)):
continue
print "%20s: %s" % (attr_name, attr)Create html buttons and bind the click event with jQuery:
import js # https://github.com/pypyjs/pypy/tree/pypyjs/pypy/module/js
jquery = js.globals["$"]
for i in range(10):
jquery("#editor").before('<button id="button_%i">Click me %i!</button>' % (i,i))
@js.Function
def click_handler(event):
print "You clicked on button with ID:",
print event.target.id
jquery("button").click(click_handler)
print "Please click on new button above!"Display information about a js object:
from __future__ import print_function
import js # https://github.com/pypyjs/pypy/tree/pypyjs/pypy/module/js
def pprint_js_object(obj):
print("Object:", obj, type(obj), obj.__doc__)
print("dir:", dir(obj))
print("Existing object attributes:")
for attr_name in obj:
attr_name = str(attr_name) # convert js.String https://github.com/pypyjs/pypy/issues/2
print("%30s:" % attr_name, end="")
try:
value = getattr(obj, attr_name)
except Exception as err:
print("[Error: %s]" % err)
else:
value = str(value) # evaluate js.Handle
value = repr(value) # e.g. escape newlines
if len(value)>70:
value = "%s..." % value[:70]
print(value)
window = js.globals.window
pprint_js_object(window)
print(" --- END --- ")Some system information:
import os
methods=(
"getlogin", "getuid", "getgid",
"getpid", "getpgrp", "getppid",
"geteuid", "getegid",
)
for method in methods:
func = getattr(os, method)
doc = func.__doc__.split("\n\n")[-1].strip()
result = func()
print "%12s: %-10s %s" % (method, result, doc)Manipulating the virtual file system:
import os
LIB_PATH = "/lib/pypyjs/lib_pypy/"
with open(os.path.join(LIB_PATH, "my_module.py"), "w") as f:
f.write("x='hi'")
import my_module
print my_module.x # hi
print "my_module.__file__:", my_module.__file__ # /lib/pypyjs/lib_pypy/my_module.py
import os
for filename in os.listdir(LIB_PATH): # Will list my_module.py and my_module.pyc
if filename.startswith("my"):
filepath = os.path.join(LIB_PATH, filename)
print "*", filename, os.stat(filepath)(Currently there is no public API for manipulating the virtual filesystem, see also: pypyjs/pypyjs#132 )
Render Mandelbrot fractal in browser via PyPy.js
- /mandelbrot.html
- /mandelbrot-nojit.html (without PyPy JIT)
WIP:
A minimal console with the aim to use as few external dependencies. A full featured console, that used jq-console is here: pypyjs.org
There is a /simple_http_server.py for test this repro at home.
This is needed for 'mandelbrot' example and you can better see file requests.
Just do it e.g.:
~$ git clone https://github.com/pypyjs/pypyjs-examples.git ~$ cd pypyjs-examples ~/pypyjs-examples$ git submodule init ~/pypyjs-examples$ git submodule update ~/pypyjs-examples$ python simple_http_server.py
The server worked with Python 2 and 3 and starts at http://127.0.0.1:8000.
In short: PyPy compiled to JavaScript
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.
More info: http://pypyjs.org/
- PyPyBox - create 2D graphics in pure Python
| pypyjs | Main repository to built a PyPyJS release |
| pypy | Fork of PyPy with support for compiling to javascript |
| pypyjs-release | Latest release build of PyPyJS, as a handy git submodule |
| pypyjs-release-nojit | Latest release build of PyPyJS, without a JIT |
| pypyjs-examples | Examples/snippets usage of pypyjs-release and pypyjs-release-nojit |
| pypyjs.github.io | source for pypyjs.org website use pypyjs-release and pypyjs-release-nojit |