Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Cookies Example #790

Merged
merged 1 commit into from

2 participants

@charleslaw

Had this sitting around on my computer. It sets and gets a cookie. I wrote it a couple years ago to test a bug with pyjamas where the path wasn't being escaped. I cleaned it up a little & tested it with the latest pyjs code and it works. Feel free to let me know if I should change anything.

@xtfxme
Owner

neat :) LGTM, thanks much Charles.

@xtfxme xtfxme merged commit 442ca20 into pyjs:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 23, 2013
  1. @charleslaw

    add cookies example

    charleslaw authored
This page is out of date. Refresh to see the latest.
View
6 examples/cookiemonster/README
@@ -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.
+
View
53 examples/cookiemonster/__main__.py
@@ -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)
View
64 examples/cookiemonster/cookiemonster.py
@@ -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()
+
Something went wrong with that request. Please try again.