Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Basic dynamic text widget complete

  • Loading branch information...
commit cd3c247426dcdb99a907f608b2337aa38114a808 1 parent e06befe
@gregjurman gregjurman authored
View
14 setup.py
@@ -1,14 +1,16 @@
from setuptools import setup, find_packages
setup(
- name='',
- version='',
- description='',
- author='',
- author_email='',
- url='',
+ name='tw2.dyntext',
+ version='0.0.1',
+ description='Dynamic Text widget for TW2. Allows text to be pulled from JSON objects live.',
+ author='Greg Jurman',
+ author_email='gdj2214@rit.edu',
+ url='http://github.com/gregjurman/tw2.dyntext',
install_requires=[
"tw2.core",
+ "tw2.jquery",
+ "mako"
## Add other requirements here
# "Genshi",
],
View
2  tw2/dyntext/__init__.py
@@ -1 +1 @@
-from widgets import *
+from widgets import DynamicTextWidget
View
20 tw2/dyntext/samples.py
@@ -7,8 +7,26 @@
"""
import widgets
+import json
+import random
+import webob
-class DemoDyntext(widgets.Dyntext):
+class DemoDynamicTextWidget(widgets.DynamicTextWidget):
# Provide default parameters, value, etc... here
# default = <some-default-value>
+ data_url = '/dynamic_text_demo'
+
pass
+
+ @classmethod
+ def request(cls, req):
+ lottery_numbers = ", ".join([str(random.randint(1,100)) for x in range(5)])
+
+ resp = webob.Response(request=req, content_type="application/json")
+ resp.body = json.dumps({'text': lottery_numbers})
+
+ return resp
+
+import tw2.core as twc
+mw = twc.core.request_local()['middleware']
+mw.controllers.register(DemoDynamicTextWidget, 'dynamic_text_demo')
View
12 tw2/dyntext/static/dyntext.js
@@ -0,0 +1,12 @@
+function setupPollingDynText(text_widget, data_url, interval)
+{
+ $.getJSON(data_url, function(data) {
+ var new_text = data.text;
+
+ $("#"+text_widget).text(new_text);
+ });
+
+ setTimeout( function () {
+ setupPollingDynText(text_widget, data_url, interval);
+ }, interval );
+}
View
1  tw2/dyntext/templates/dyntext.html
@@ -1 +0,0 @@
-<div xmlns:py="http://genshi.edgewall.org/" py:attrs="attrs"></div>
View
5 tw2/dyntext/templates/dyntext.mak
@@ -0,0 +1,5 @@
+<%namespace name="tw" module="tw2.core.mako_util"/>
+<span id="${w.attrs['id']}">${w.initial_text}</span>
+<script type="text/javascript">
+ setupPollingDynText("${w.selector}", ${w.j('data_url')}, ${w.j('interval')});
+</script>
View
27 tw2/dyntext/widgets.py
@@ -1,21 +1,32 @@
import tw2.core as twc
+# tw2.jquery imports
+from tw2.jquery import jquery_js
+import json
-class Dyntext(twc.Widget):
- template = "genshi:tw.dyntext.templates.dyntext"
+class DynamicTextWidget(twc.Widget):
+ template = "mako:tw2.dyntext.templates.dyntext"
# declare static resources here
# you can remove either or both of these, if not needed
resources = [
twc.JSLink(modname=__name__, filename='static/dyntext.js'),
- twc.CSSLink(modname=__name__, filename='static/dyntext.css'),
+ jquery_js
]
- @classmethod
- def post_define(cls):
- pass
- # put custom initialisation code here
+ data_url = twc.Param('(str) URL where JSON data should be pulled from', default=None)
+
+ interval = twc.Param('(int) Polling interval in milliseconds', default=5000)
+
+ initial_text = twc.Param('Initial text to place in the widget', default='')
+
+ def j(cls, attr):
+ return json.dumps(getattr(cls, attr))
def prepare(self):
- super(Dyntext, self).prepare()
+ if self.data_url is None:
+ raise ValueError, "data_url must be a string"
+
+ super(DynamicTextWidget, self).prepare()
# put code here to run just before the widget is displayed
+ self.selector = self.attrs['id'].replace(':', '\\\\:')
Please sign in to comment.
Something went wrong with that request. Please try again.