Sample code to execute some Javascript code inside PyPy.js. I couldn't find any examples.
import js
js.eval("console.log(\"hi!\")") import js
js.eval("console.log(\"hi!\")") js.eval('var div = document.createElement("div"); div.style.width = "100px"; div.style.height = "100px"; div.style.background = "red"; div.style.color = "white"; div.innerHTML = "Hello"; document.body.appendChild(div);');
manages to append a div.--
Directly modifying document.body.innerHTML in any way seems to hang it. Both:
js.eval('document.body.innerHTML += "<div></div>";');
js.globals.document.body.innerHTML += "<div></div>"
Cause it to hang, though the javascript console shows it was added (or using an img tag instead).(ChromeOS v43 beta)
import js
jq = js.eval("$")
first_line = jq(jq(".container p")[0])
first_line.text("jQuery was here") import js
c = js.globals.document.createElement("canvas")
js.globals.document.body.appendChild(c)
ctx = c.getContext("2d")
ctx.fillRect(10, 10, 10, 10)
Edit: Here is a simple animation: import js
import math
import time
c = js.globals.document.createElement("canvas")
js.globals.document.body.appendChild(c)
ctx = c.getContext("2d")
def render():
ctx.clearRect(0, 0, c.width, c.height)
t = time.time()
ctx.fillRect(20+math.sin(t*10)*10, 20+math.cos(t*10)*10, 10, 10)
js.globals.window.setInterval(render, 20)
Here's a version you can paste in properly: http://pastebin.com/raw.php?i=67GadSUV >>> import js
... c = js.globals.document.createElement("canvas")
...
debug: OperationError:
debug: operror-type: SyntaxError
debug: operror-value: ('EOL while scanning string literal', ('c callback', 1, 12, "r = c.push('import js\n", 0))
debug: OperationError:
debug: operror-type: KeyError
debug: operror-value: 'r'
And then the input window hangs. Strangely enough, if I type it in instead, it's okay.(ChromeOS v43 beta-channel)
import js
c = js.globals.document.createElement("canvas")
and not the pastebin.
The pastebin works for me, but only the first time. Subsequent runs result in: <RuntimeError object at 0x15d648>
RPython traceback:
...
Fatal RPython error: import js
js.globals.console.log('hi!') import js
js.globals.console.log('hi!'*5) import js
>>> "globals" in dir(js)
True
>>> "console" in dir(js.globals)
False
>>> js
<module 'js' (built-in)>
>>> js.globals.console
<js.Object handle=14>
So apparently introspection doesn't quite work with the wrapped js. Also, if you try: "[ i for i in js.globals]" (or equivalently iterate/loop over the globals-object) - the whole repl/tab hangs.Not really an issue for running business logic in the browser, but if this was a full python repl-interface (with ipython support!) to the whole browser DOM -- that'd be a fantastic tool.
Welcome to PyPy.js!
>>> import js
>>> "console" in js.globals
True
>>> "console" in dir(js.globals)
FalseI guess these things will take a while to shake out, but I'd imagine a python-in-browser-api PEP at some point.