[pypy-svn] r38850 - pypy/dist/pypy/translator/js/examples
guido at codespeak.net
guido at codespeak.net
Wed Feb 14 16:57:38 CET 2007
Author: guido
Date: Wed Feb 14 16:57:37 2007
New Revision: 38850
Added:
pypy/dist/pypy/translator/js/examples/guestbook.py
pypy/dist/pypy/translator/js/examples/guestbook_client.py
Log:
Stupid guestbook example for the 'webapps_with_pypy.txt' doc.
Added: pypy/dist/pypy/translator/js/examples/guestbook.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/examples/guestbook.py Wed Feb 14 16:57:37 2007
@@ -0,0 +1,95 @@
+#!/usr/bin/env python
+
+""" the mandatory guestbook example
+
+ accompanies the pypyp/doc/js/webapps_with_pypy.txt document, serves as
+ a simple example to show how to go about writing a pypy web application
+"""
+
+import autopath
+
+from pypy.translator.js.lib import server
+from pypy.translator.js.lib.support import callback
+from pypy.translator.js.main import rpython2javascript
+
+import py
+import shelve
+
+class ExportedMethods(server.ExportedMethods):
+ """ this provides the methods that are exposed to the client ('AJAX' stuff)
+ """
+ def __init__(self):
+ super(ExportedMethods, self).__init__()
+ self._db = shelve.open('messages')
+ self._db.setdefault('messages', [])
+
+ # callback makes that a method gets exposed, it can get 2 arguments,
+ # 'ret' for specifying the return value, and 'args' for specifying the
+ # argument types
+ @callback(retval=[str])
+ def get_messages(self):
+ return self._db['messages']
+
+ @callback(retval=str)
+ def add_message(self, name='', message=''):
+ text = '%s says: %s' % (name, message)
+ m = self._db['messages']
+ m.append(text)
+ self._db['messages'] = m
+ return text
+
+exported_methods = ExportedMethods()
+
+FUNCTION_LIST = ['init_guestbook', 'add_message']
+def guestbook_client():
+ """ compile the rpython guestbook_client code to js
+ """
+ import guestbook_client
+ return rpython2javascript(guestbook_client, FUNCTION_LIST)
+
+class Handler(server.Handler):
+ """ a BaseHTTPRequestHandler subclass providing the HTTP methods
+ """
+ # provide the exported methods
+ exported_methods = exported_methods
+
+ # a simple html page
+ def index(self):
+ html = """
+ <html>
+ <head>
+ <title>Guestbook</title>
+ <script type="text/javascript" src="/guestbook.js"></script>
+ </head>
+ <body onload="init_guestbook()">
+ <h2>Guestbook</h2>
+ <div id="messages">
+ <!-- this will be filled from javascript -->
+ </div>
+ <form action="." method="post"
+ onsubmit="add_message(this); return false">
+ name: <input type="text" name="name" id="name" /><br />
+ message:<br />
+ <textarea name="message" id="message"></textarea><br />
+ <input type="submit" />
+ </form>
+ </body>
+ </html>
+ """
+ return 'text/html', html
+ index.exposed = True
+
+ # the (generated) javascript
+ def guestbook_js(self):
+ if hasattr(self.server, 'source'):
+ source = self.server.source
+ else:
+ source = guestbook_client()
+ self.server.source = source
+ return "text/javascript", source
+ guestbook_js.exposed = True
+
+if __name__ == '__main__':
+ addr = ('', 8008)
+ httpd = server.create_server(server_address=addr, handler=Handler)
+ httpd.serve_forever()
Added: pypy/dist/pypy/translator/js/examples/guestbook_client.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/examples/guestbook_client.py Wed Feb 14 16:57:37 2007
@@ -0,0 +1,35 @@
+""" rpython guestbook client-side code
+
+ this code can be tested in CPython, but will also be converted to
+ JavaScript to provide the client-side functionality for the guestbook
+ example
+"""
+
+from pypy.translator.js.modules import dom
+from pypy.translator.js.examples.guestbook import exported_methods
+
+def add_html_message(text=''):
+ doc = dom.window.document
+ div = doc.getElementById('messages')
+ msgdiv = doc.createElement('div')
+ msgdiv.style.border = '1px solid black'
+ msgdiv.style.margin = '1em'
+ msgdiv.appendChild(doc.createTextNode(text))
+ div.appendChild(msgdiv)
+
+def _init_callback(messages):
+ for message in messages:
+ add_html_message(message)
+
+def init_guestbook():
+ exported_methods.get_messages(_init_callback)
+
+def _add_message_callback(message):
+ add_html_message(message)
+
+def add_message():
+ doc = dom.window.document
+ name = doc.getElementById('name').value
+ message = doc.getElementById('message').value
+ exported_methods.add_message(name, message, _add_message_callback)
+
More information about the Pypy-commit
mailing list