Skip to content

Instantly share code, notes, and snippets.

@FND
Last active November 2, 2017 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save FND/1cdf410b479a627aedef76c3465ea359 to your computer and use it in GitHub Desktop.
Save FND/1cdf410b479a627aedef76c3465ea359 to your computer and use it in GitHub Desktop.
complate-wsgi sandbox
$ python3 -m venv venv
$ . venv/bin/activate

$ pip install js2py gunicorn

$ gunicorn app:app

http://localhost:8000

import js2py
class BufferedStream:
def __init__(self):
self._buffer = []
def write(self, msg):
self._buffer.append(msg.encode("utf-8"))
def __iter__(self):
return iter(self._buffer)
def transpile(filepath):
with open(filepath) as fh:
return js2py.eval_js(fh.read())
render = transpile("./views.js")
def app(environ, start_response):
start_response("200 OK", [("Content-Type", "text/html")])
stream = BufferedStream()
view = render(stream)
return stream
var render = (function() {
return function render(stream) {
stream.write("<h1>Hello World</h1>");
stream.write("<p>lorem ipsum dolor sit amet</p>");
};
}());
@FND
Copy link
Author

FND commented Oct 28, 2017

Using Js2Py because PyExecJS can only pass JSON-serializable objects between the Python and JavaScript environments. v8eval requires compiling V8, PyV8 seems abandoned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment