alanjds/pypyjs-examples
PyPy.js example usage of: https://github.com/pypyjs/pypyjs-release
=== PyPy.js examples
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!
==== Small snippets:
Here some small snippets for copy&paste into [[http://pypyjs.org/editor.html|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('Click me %i!' % (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 pypyjs/pypy#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 )
==== Existing examples:
===== Mandelbrot
Render Mandelbrot fractal in browser via PyPy.js
- [[http://pypyjs.org/pypyjs-examples/mandelbrot.html|/mandelbrot.html]]
- [[http://pypyjs.org/pypyjs-examples/mandelbrot-nojit.html|/mandelbrot-nojit.html]] (without PyPy JIT)
WIP:
- [[http://pypyjs.org/pypyjs-examples/mandelbrot_enhanced.html|/mandelbrot_enhanced.html]]
- [[http://pypyjs.org/pypyjs-examples/mandelbrot_enhanced-nojit.html|/mandelbrot_enhanced-nojit.html]]
===== minimal console
A minimal console with the aim to use as few external dependencies.
A full featured console, that used jq-console is here: [[http://pypyjs.org|pypyjs.org]]
- [[http://pypyjs.org/pypyjs-examples/minimal_console.html|/minimal_console.html]]
==== try out at home
There is a [[http://pypyjs.org/pypyjs-examples/simple_http_server.py|/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]].
=== What is PyPy.js ?
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/
=== Links
- [[http://salvatore.diodev.fr/pypybox|PyPyBox]] - create 2D graphics in pure Python
=== Repository Overview
| [[https://github.com/pypyjs/pypyjs|pypyjs]] | Main repository to built a PyPyJS release
| [[https://github.com/pypyjs/pypy|pypy]] | Fork of PyPy with support for compiling to javascript
| [[https://github.com/pypyjs/pypyjs-release|pypyjs-release]] | Latest release build of PyPyJS, as a handy git submodule
| [[https://github.com/pypyjs/pypyjs-release-nojit|pypyjs-release-nojit]] | Latest release build of PyPyJS, without a JIT
| [[https://github.com/pypyjs/pypyjs-examples|pypyjs-examples]] | Examples/snippets usage of pypyjs-release and pypyjs-release-nojit
| [[https://github.com/pypyjs/pypyjs.github.io|pypyjs.github.io]] | source for pypyjs.org website use pypyjs-release and pypyjs-release-nojit