-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial.py
64 lines (49 loc) · 1.63 KB
/
tutorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from moksha.common.lib.helpers import get_moksha_appconfig
from moksha.wsgi.middleware import make_moksha_middleware
from tw2.core.middleware import make_middleware
import datetime
import moksha.hub.api.producer
import moksha.wsgi.widgets.api
import tw2.jqplugins.gritter
import flask.templating
from flask import Flask
app = Flask(__name__)
simple_template = """
<html>
<head></head>
<body>
Really?
{{notification_widget.display()}}
{{moksha_socket.display()}}
</body>
</html>
"""
class PopupNotification(moksha.wsgi.widgets.api.LiveWidget):
topic = "*"
onmessage = "$.gritter.add({'title': 'Received', 'text': json});"
resources = moksha.wsgi.widgets.api.LiveWidget.resources + \
tw2.jqplugins.gritter.gritter_resources
backend = "websocket"
# Don't actually produce anything when you call .display() on this widget.
inline_engine_name = "mako"
template = ""
@app.route("/")
def hello():
config = get_moksha_appconfig()
socket = moksha.wsgi.widgets.api.get_moksha_socket(config)
return flask.templating.render_template_string(
simple_template,
notification_widget=PopupNotification,
moksha_socket=socket,
)
class HelloWorldProducer(moksha.hub.api.producer.PollingProducer):
frequency = datetime.timedelta(seconds=2)
def poll(self):
self.send_message('hello_world', "Hello World!")
if __name__ == "__main__":
# Load development.ini
config = get_moksha_appconfig()
# Wrap the inner wsgi app with our middlewares
app.wsgi_app = make_moksha_middleware(app.wsgi_app, config)
app.wsgi_app = make_middleware(app.wsgi_app)
app.run(debug=True)