Skip to content

Commit 8e5db4d

Browse files
author
Philip Guo
committed
bub
1 parent 17e742a commit 8e5db4d

3 files changed

Lines changed: 70 additions & 43 deletions

File tree

‎v3/js/opt-ipy.js‎

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,33 +73,34 @@ var updater = {
7373
},
7474

7575
showMessage: function(message) {
76-
// empty trace!
77-
if (message.trace.length == 0) {
78-
myVisualizer = null;
79-
$('#vizDiv').empty();
80-
$(document).unbind('keydown');
81-
return;
82-
}
83-
84-
myVisualizer = new ExecutionVisualizer('vizDiv',
85-
message,
86-
{embeddedMode: true, jumpToEnd: true});
87-
88-
// set keyboard bindings
89-
// VERY IMPORTANT to clear and reset this every time or
90-
// else the handlers might be bound multiple times
91-
$(document).unbind('keydown');
92-
$(document).keydown(function(k) {
93-
if (k.keyCode == 37) { // left arrow
94-
if (myVisualizer.stepBack()) {
95-
k.preventDefault(); // don't horizontally scroll the display
96-
}
76+
if (message.type == 'wholetrace') {
77+
payload = message.payload
78+
myVisualizer = new ExecutionVisualizer('vizDiv',
79+
payload,
80+
{embeddedMode: true, jumpToEnd: true});
81+
82+
// set keyboard bindings
83+
// VERY IMPORTANT to clear and reset this every time or
84+
// else the handlers might be bound multiple times
85+
$(document).unbind('keydown');
86+
$(document).keydown(function(k) {
87+
if (k.keyCode == 37) { // left arrow
88+
if (myVisualizer.stepBack()) {
89+
k.preventDefault(); // don't horizontally scroll the display
90+
}
91+
}
92+
else if (k.keyCode == 39) { // right arrow
93+
if (myVisualizer.stepForward()) {
94+
k.preventDefault(); // don't horizontally scroll the display
95+
}
96+
}
97+
});
9798
}
98-
else if (k.keyCode == 39) { // right arrow
99-
if (myVisualizer.stepForward()) {
100-
k.preventDefault(); // don't horizontally scroll the display
101-
}
99+
else if (message.type == 'clear') {
100+
myVisualizer = null;
101+
$('#vizDiv').empty();
102+
$(document).unbind('keydown');
103+
return;
102104
}
103-
});
104105
}
105106
};

‎v3/opt-ipy-server.py‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99

1010
from tornado.options import define, options
1111

12-
define("port", default=8888, help="run on the given port", type=int)
12+
import json
1313

1414

15-
# TODO: ugly clunky global
16-
current_full_trace = None
15+
define("port", default=8888, help="run on the given port", type=int)
1716

1817

1918
class Application(tornado.web.Application):
19+
# singleton
20+
current_full_trace = None
21+
2022
def __init__(self):
2123
handlers = [
2224
(r"/js/(.*)",
@@ -26,8 +28,12 @@ def __init__(self):
2628
tornado.web.StaticFileHandler,
2729
{"path": os.path.join(os.path.dirname(__file__), 'css/')}),
2830
(r"/", MainHandler),
29-
(r"/post", PostHandler),
3031
(r"/chatsocket", ChatSocketHandler),
32+
# respond to HTTP POST requests:
33+
(r"/wholetrace", WholeTraceHandler),
34+
(r"/append", AppendHandler),
35+
(r"/replace", ReplaceHandler),
36+
(r"/clear", ClearHandler),
3137
]
3238
tornado.web.Application.__init__(self, handlers)
3339

@@ -37,14 +43,31 @@ def get(self):
3743
self.render("opt-ipy.html")
3844

3945

40-
class PostHandler(tornado.web.RequestHandler):
46+
class WholeTraceHandler(tornado.web.RequestHandler):
4147
def post(self):
4248
message = self.request.body
43-
#logging.info("got message %r", message)
44-
global current_full_trace
45-
current_full_trace = message
46-
if current_full_trace:
47-
ChatSocketHandler.send_updates(current_full_trace)
49+
dat = json.loads(message)
50+
Application.current_full_trace = dat
51+
52+
js_msg=dict(payload=Application.current_full_trace, type='wholetrace')
53+
ChatSocketHandler.send_updates(json.dumps(js_msg))
54+
55+
56+
class AppendHandler(tornado.web.RequestHandler):
57+
def post(self):
58+
pass
59+
60+
61+
class ReplaceHandler(tornado.web.RequestHandler):
62+
def post(self):
63+
pass
64+
65+
66+
class ClearHandler(tornado.web.RequestHandler):
67+
def post(self):
68+
Application.current_full_trace = None
69+
js_msg=dict(type='clear')
70+
ChatSocketHandler.send_updates(json.dumps(js_msg))
4871

4972

5073
class ChatSocketHandler(tornado.websocket.WebSocketHandler):
@@ -56,9 +79,11 @@ def allow_draft76(self):
5679

5780
def open(self):
5881
ChatSocketHandler.waiters.add(self)
59-
global current_full_trace
60-
if current_full_trace:
61-
ChatSocketHandler.send_updates(current_full_trace)
82+
# when a new connection is made, send the entire trace to only
83+
# THIS browser
84+
if Application.current_full_trace:
85+
js_msg=dict(payload=Application.current_full_trace, type='wholetrace')
86+
self.write_message(json.dumps(js_msg))
6287

6388
def on_close(self):
6489
ChatSocketHandler.waiters.remove(self)

‎v3/opt-ipy.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242

4343

4444
# If you're hosting the server remotely, then change this address:
45-
POST_ADDR = "http://localhost:8888/post"
45+
SERVER_ADDR = "http://localhost:8888/"
46+
47+
assert SERVER_ADDR[-1] == '/'
4648

4749

4850
import os, sys, urllib2, json
@@ -104,7 +106,7 @@ def run_str_and_broadcast(self, stmt_str):
104106
self.pop_last()
105107

106108
self.check_rep()
107-
urllib2.urlopen(POST_ADDR, json_output)
109+
urllib2.urlopen(SERVER_ADDR + 'wholetrace', json_output)
108110

109111

110112
# called right before a statement gets executed
@@ -146,8 +148,7 @@ def opt_clear(self, params):
146148

147149
ip.meta.opt_history = OptHistory() # just create a new one!
148150

149-
empty_msg = dict(code='', trace=[])
150-
urllib2.urlopen(POST_ADDR, json.dumps(empty_msg))
151+
urllib2.urlopen(SERVER_ADDR + 'clear', 'blub') # need a non-empty POST body
151152

152153

153154
def load_ipython_extension(ipython):

0 commit comments

Comments
 (0)