Permalink
Please sign in to comment.
Browse files
Merge pull request #790 from charleslaw/master
Cookies Example
- Loading branch information...
Showing
with
123 additions
and 0 deletions.
@@ -0,0 +1,6 @@ | ||
+Created by Charles Law. | ||
+ | ||
+This is an example of how to set and read cookies. It includes | ||
+some features, specifically setting the path, that were broken | ||
+in previous versions of pyjs, but are fixed. | ||
+ |
@@ -0,0 +1,53 @@ | ||
+#!/usr/bin/env python | ||
+# -*- coding: utf-8 -*- | ||
+ | ||
+ | ||
+TARGETS = [ | ||
+ 'cookiemonster.py', | ||
+] | ||
+ | ||
+ | ||
+PACKAGE = { | ||
+ 'title': 'cookiemonster', | ||
+ 'desc': 'cookie get and set example', | ||
+} | ||
+ | ||
+ | ||
+def setup(targets): | ||
+ '''Setup example for translation, MUST call util.setup(targets).''' | ||
+ util.setup(targets) | ||
+ | ||
+ | ||
+def translate(): | ||
+ '''Translate example, MUST call util.translate().''' | ||
+ util.translate() | ||
+ | ||
+ | ||
+def install(package): | ||
+ '''Install and cleanup example module. MUST call util.install(package)''' | ||
+ util.install(package) | ||
+ | ||
+ | ||
+##---------------------------------------## | ||
+# --------- (-: DO NOT EDIT :-) --------- # | ||
+##---------------------------------------## | ||
+ | ||
+ | ||
+import sys | ||
+import os | ||
+ | ||
+ | ||
+examples = head = os.path.abspath(os.path.dirname(__file__)) | ||
+while os.path.split(examples)[1].lower() != 'examples': | ||
+ examples = os.path.split(examples)[0] | ||
+ if not examples: | ||
+ raise ValueError("Cannot determine examples directory") | ||
+sys.path.insert(0, os.path.join(examples)) | ||
+from _examples import util | ||
+sys.path.pop(0) | ||
+ | ||
+util.init(head) | ||
+ | ||
+setup(TARGETS) | ||
+translate() | ||
+install(PACKAGE) |
@@ -0,0 +1,64 @@ | ||
+from pyjamas.ui.RootPanel import RootPanel | ||
+from pyjamas.ui.TextArea import TextArea | ||
+from pyjamas.ui.Label import Label | ||
+from pyjamas.ui.HTML import HTML | ||
+from pyjamas.ui.Button import Button | ||
+from pyjamas.ui.VerticalPanel import VerticalPanel | ||
+from pyjamas.ui.HorizontalPanel import HorizontalPanel | ||
+from pyjamas.Cookies import setCookie, getCookie | ||
+ | ||
+ | ||
+class CookieExample: | ||
+ COOKIE_NAME = "myCookie" | ||
+ def onModuleLoad(self): | ||
+ try: | ||
+ setCookie(COOKIE_NAME, "setme", 100000) | ||
+ except: | ||
+ pass | ||
+ | ||
+ self.status=Label() | ||
+ self.text_area = TextArea() | ||
+ self.text_area.setText(r"Me eat cookie!") | ||
+ self.text_area.setCharacterWidth(80) | ||
+ self.text_area.setVisibleLines(8) | ||
+ self.button_py_set = Button("Set Cookie", self) | ||
+ self.button_py_read = Button("Read Cookie ", self) | ||
+ | ||
+ buttons = HorizontalPanel() | ||
+ buttons.add(self.button_py_set) | ||
+ buttons.add(self.button_py_read) | ||
+ buttons.setSpacing(8) | ||
+ info = r'This demonstrates setting and reading information using cookies.' | ||
+ panel = VerticalPanel() | ||
+ panel.add(HTML(info)) | ||
+ panel.add(self.text_area) | ||
+ panel.add(buttons) | ||
+ panel.add(self.status) | ||
+ | ||
+ RootPanel().add(panel) | ||
+ | ||
+ | ||
+ def onClick(self, sender): | ||
+ """ | ||
+ Run when any button is clicked | ||
+ """ | ||
+ if sender.getText() == "Set Cookie": | ||
+ #clicked the set cookie button | ||
+ text = self.text_area.getText() | ||
+ #print goes to console.log | ||
+ print "setting cookie to:", text | ||
+ #Note: this sets the cookie on the top level | ||
+ setCookie(COOKIE_NAME, text, 10000, path='/') | ||
+ else: | ||
+ cookie_text = getCookie(COOKIE_NAME) | ||
+ if cookie_text is None: | ||
+ print "No Cookie" | ||
+ else: | ||
+ print "myCookie", cookie_text | ||
+ self.status.setText(cookie_text) | ||
+ | ||
+ | ||
+if __name__ == '__main__': | ||
+ app = CookieExample() | ||
+ app.onModuleLoad() | ||
+ |
0 comments on commit
442ca20