[pypy-svn] r36097 - pypy/dist/pypy/translator/js/tutorial

fijal at codespeak.net fijal at codespeak.net
Mon Jan 1 16:06:35 CET 2007


Author: fijal
Date: Mon Jan  1 16:06:34 2007
New Revision: 36097

Added:
   pypy/dist/pypy/translator/js/tutorial/
   pypy/dist/pypy/translator/js/tutorial/__init__.py   (contents, props changed)
   pypy/dist/pypy/translator/js/tutorial/step1.py   (contents, props changed)
   pypy/dist/pypy/translator/js/tutorial/step2.py   (contents, props changed)
Log:
Added two simple steps how to start, this is intermediate checkin.


Added: pypy/dist/pypy/translator/js/tutorial/__init__.py
==============================================================================

Added: pypy/dist/pypy/translator/js/tutorial/step1.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/tutorial/step1.py	Mon Jan  1 16:06:34 2007
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+"""
+
+This is simple all-in-one self-containing server,
+which just shows almost-empty HTML page
+
+"""
+
+# here we import server, which is derivative of
+# BaseHTTPServer from python standard library
+from pypy.translator.js.examples import server
+
+# We create handler, which will handle all our requests
+class Handler(server.TestHandler):
+
+    def index(self):
+        # provide some html contents
+        return "<html><head></head><body><p>Something</p></body></html>"
+    # this line is necessary to make server show something,
+    # otherwise method is considered private-only
+    index.exposed = True
+
+if __name__ == '__main__':
+    # let's start our server,
+    # this will create running server instance on port 8000 by default,
+    # which will run until we press Control-C
+    server.start_server(handler=Handler)

Added: pypy/dist/pypy/translator/js/tutorial/step2.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/tutorial/step2.py	Mon Jan  1 16:06:34 2007
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+"""
+In this example, we'll show how to add javascript to our simple
+server from previous example
+"""
+
+from pypy.translator.js.examples import server
+import sys
+
+# here we have virtual script "source.js" which we generate
+# on-the-fly when requested
+HTML = """
+<html>
+<head>
+  <script src="source.js"/>
+  <title>pypy.js tutorial</title>
+</head>
+<body onload="show()">
+</body>
+</html>
+"""
+
+from pypy.translator.js.main import rpython2javascript
+# here we import rpython -> javascript conversion utility
+
+from pypy.translator.js.modules import browser
+# and here we import functions from modules that we want to use
+
+# this is function which will be translated into javascript,
+# we can put it in a different module if we like so
+def show():
+    browser.alert("Alert")
+
+class Handler(server.TestHandler):
+
+    def index(self):
+        return HTML
+    index.exposed = True
+
+    # here we generate javascript, this will be accessed when
+    # asked for source.js
+    def source_js(self):
+        # this returns content-type (if not text/html)
+        # and generated javascript code
+        # None as argument means current module, while "show" is the
+        # name of function to be exported (under same name)
+        return "text/javascript", rpython2javascript(None, ["show"])
+    source_js.exposed = True
+
+# server start, same as before
+if __name__ == '__main__':
+    server.start_server(handler=Handler)



More information about the Pypy-commit mailing list