Show More
@@ -1,155 +1,174 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2012 The IPython Development Team |
|
2 | // Copyright (C) 2008-2012 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // EngineInteract |
|
9 | // EngineInteract | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | var key = IPython.utils.keycodes; |
|
12 | var key = IPython.utils.keycodes; | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | var DirectViewWidget = function (selector, kernel, targets) { |
|
15 | var DirectViewWidget = function (selector, kernel, targets) { | |
16 | // The kernel doesn't have to be set at creation time, in that case |
|
16 | // The kernel doesn't have to be set at creation time, in that case | |
17 | // it will be null and set_kernel has to be called later. |
|
17 | // it will be null and set_kernel has to be called later. | |
18 | this.selector = selector; |
|
18 | this.selector = selector; | |
19 | this.element = $(selector); |
|
19 | this.element = $(selector); | |
20 | this.kernel = kernel || null; |
|
20 | this.kernel = kernel || null; | |
21 | this.code_mirror = null; |
|
21 | this.code_mirror = null; | |
22 | this.targets = targets; |
|
22 | this.targets = targets; | |
23 | this.create_element(); |
|
23 | this.create_element(); | |
24 | }; |
|
24 | }; | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | DirectViewWidget.prototype.create_element = function () { |
|
27 | DirectViewWidget.prototype.create_element = function () { | |
28 | this.element.addClass('cell border-box-sizing code_cell vbox'); |
|
28 | this.element.addClass('cell border-box-sizing code_cell vbox'); | |
29 | this.element.attr('tabindex','2'); |
|
29 | this.element.attr('tabindex','2'); | |
30 | this.element.css('padding-right',0); |
|
30 | this.element.css('padding-right',0); | |
|
31 | ||||
|
32 | var control = $('<div/>').addClass('dv_control').height('30px'); | |||
|
33 | var control_label = $('<span/>').html('Select engine(s) to run code on interactively: '); | |||
|
34 | control_label.css('line-height','30px'); | |||
|
35 | var select = $('<select/>').addClass('dv_select ui-widget ui-widget-content'); | |||
|
36 | select.css('font-size','85%%').css('margin-bottom','5px'); | |||
|
37 | var n = this.targets.length; | |||
|
38 | select.append($('<option/>').html('all').attr('value','all')); | |||
|
39 | for (var i=0; i<n; i++) { | |||
|
40 | select.append($('<option/>').html(this.targets[i]).attr('value',this.targets[i])) | |||
|
41 | } | |||
|
42 | control.append(control_label).append(select); | |||
|
43 | ||||
31 | var input = $('<div></div>').addClass('input hbox'); |
|
44 | var input = $('<div></div>').addClass('input hbox'); | |
32 | var input_area = $('<div/>').addClass('input_area box-flex1'); |
|
45 | var input_area = $('<div/>').addClass('input_area box-flex1'); | |
33 | this.code_mirror = CodeMirror(input_area.get(0), { |
|
46 | this.code_mirror = CodeMirror(input_area.get(0), { | |
34 | indentUnit : 4, |
|
47 | indentUnit : 4, | |
35 | mode: 'python', |
|
48 | mode: 'python', | |
36 | theme: 'ipython', |
|
49 | theme: 'ipython', | |
37 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
50 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |
38 | }); |
|
51 | }); | |
39 | input.append(input_area); |
|
52 | input.append(input_area); | |
40 | var output = $('<div></div>'); |
|
53 | var output = $('<div></div>'); | |
41 | this.element.append(input).append(output); |
|
54 | ||
|
55 | ||||
|
56 | this.element.append(control).append(input).append(output); | |||
42 | this.output_area = new IPython.OutputArea(output, false); |
|
57 | this.output_area = new IPython.OutputArea(output, false); | |
43 |
|
58 | |||
44 | }; |
|
59 | }; | |
45 |
|
60 | |||
46 |
|
61 | |||
47 | DirectViewWidget.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
62 | DirectViewWidget.prototype.handle_codemirror_keyevent = function (editor, event) { | |
48 | // This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
63 | // This method gets called in CodeMirror's onKeyDown/onKeyPress | |
49 | // handlers and is used to provide custom key handling. Its return |
|
64 | // handlers and is used to provide custom key handling. Its return | |
50 | // value is used to determine if CodeMirror should ignore the event: |
|
65 | // value is used to determine if CodeMirror should ignore the event: | |
51 | // true = ignore, false = don't ignore. |
|
66 | // true = ignore, false = don't ignore. | |
52 |
|
67 | |||
53 | var that = this; |
|
68 | var that = this; | |
54 | var cur = editor.getCursor(); |
|
69 | var cur = editor.getCursor(); | |
55 |
|
70 | |||
56 | if (event.keyCode === key.ENTER && event.shiftKey && event.type === 'keydown') { |
|
71 | if (event.keyCode === key.ENTER && event.shiftKey && event.type === 'keydown') { | |
57 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
72 | // Always ignore shift-enter in CodeMirror as we handle it. | |
58 | event.stop(); |
|
73 | event.stop(); | |
59 | that.execute(); |
|
74 | that.execute(); | |
60 | return true; |
|
75 | return true; | |
61 | } else if (event.keyCode === key.UP && event.type === 'keydown') { |
|
76 | } else if (event.keyCode === key.UP && event.type === 'keydown') { | |
62 | event.stop(); |
|
77 | event.stop(); | |
63 | return false; |
|
78 | return false; | |
64 | } else if (event.keyCode === key.DOWN && event.type === 'keydown') { |
|
79 | } else if (event.keyCode === key.DOWN && event.type === 'keydown') { | |
65 | event.stop(); |
|
80 | event.stop(); | |
66 | return false; |
|
81 | return false; | |
67 | } else if (event.keyCode === key.BACKSPACE && event.type == 'keydown') { |
|
82 | } else if (event.keyCode === key.BACKSPACE && event.type == 'keydown') { | |
68 | // If backspace and the line ends with 4 spaces, remove them. |
|
83 | // If backspace and the line ends with 4 spaces, remove them. | |
69 | var line = editor.getLine(cur.line); |
|
84 | var line = editor.getLine(cur.line); | |
70 | var ending = line.slice(-4); |
|
85 | var ending = line.slice(-4); | |
71 | if (ending === ' ') { |
|
86 | if (ending === ' ') { | |
72 | editor.replaceRange('', |
|
87 | editor.replaceRange('', | |
73 | {line: cur.line, ch: cur.ch-4}, |
|
88 | {line: cur.line, ch: cur.ch-4}, | |
74 | {line: cur.line, ch: cur.ch} |
|
89 | {line: cur.line, ch: cur.ch} | |
75 | ); |
|
90 | ); | |
76 | event.stop(); |
|
91 | event.stop(); | |
77 | return true; |
|
92 | return true; | |
78 | } else { |
|
93 | } else { | |
79 | return false; |
|
94 | return false; | |
80 | }; |
|
95 | }; | |
81 | }; |
|
96 | }; | |
82 |
|
97 | |||
83 | return false; |
|
98 | return false; | |
84 | }; |
|
99 | }; | |
85 |
|
100 | |||
86 |
|
101 | |||
87 | // Kernel related calls. |
|
102 | // Kernel related calls. | |
88 |
|
103 | |||
89 |
|
104 | |||
90 | DirectViewWidget.prototype.set_kernel = function (kernel) { |
|
105 | DirectViewWidget.prototype.set_kernel = function (kernel) { | |
91 | this.kernel = kernel; |
|
106 | this.kernel = kernel; | |
92 | } |
|
107 | } | |
93 |
|
108 | |||
94 |
|
109 | |||
95 | DirectViewWidget.prototype.execute = function () { |
|
110 | DirectViewWidget.prototype.execute = function () { | |
96 | this.output_area.clear_output(true, true, true); |
|
111 | this.output_area.clear_output(true, true, true); | |
97 | this.element.addClass("running"); |
|
112 | this.element.addClass("running"); | |
98 | var callbacks = { |
|
113 | var callbacks = { | |
99 | 'execute_reply': $.proxy(this._handle_execute_reply, this), |
|
114 | 'execute_reply': $.proxy(this._handle_execute_reply, this), | |
100 | 'output': $.proxy(this.output_area.handle_output, this.output_area), |
|
115 | 'output': $.proxy(this.output_area.handle_output, this.output_area), | |
101 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
116 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), | |
102 | }; |
|
117 | }; | |
103 | var code = '%(widget_var)s.execute("""'+this.get_text()+'""")'; |
|
118 | var target = this.element.find('.dv_select option:selected').attr('value'); | |
|
119 | if (target === 'all') { | |||
|
120 | target = '"all"'; | |||
|
121 | } | |||
|
122 | var code = '%(widget_var)s.execute("""'+this.get_text()+'""",targets='+target+')'; | |||
104 | console.log(code); |
|
123 | console.log(code); | |
105 | console.log(this.get_text()); |
|
124 | console.log(this.get_text()); | |
106 | var msg_id = this.kernel.execute(code, callbacks, {silent: false}); |
|
125 | var msg_id = this.kernel.execute(code, callbacks, {silent: false}); | |
107 | this.clear_input(); |
|
126 | this.clear_input(); | |
108 | this.code_mirror.focus(); |
|
127 | this.code_mirror.focus(); | |
109 | }; |
|
128 | }; | |
110 |
|
129 | |||
111 |
|
130 | |||
112 | DirectViewWidget.prototype._handle_execute_reply = function (content) { |
|
131 | DirectViewWidget.prototype._handle_execute_reply = function (content) { | |
113 | this.element.removeClass("running"); |
|
132 | this.element.removeClass("running"); | |
114 | // this.dirty = true; |
|
133 | // this.dirty = true; | |
115 | } |
|
134 | } | |
116 |
|
135 | |||
117 | // Basic cell manipulation. |
|
136 | // Basic cell manipulation. | |
118 |
|
137 | |||
119 |
|
138 | |||
120 | DirectViewWidget.prototype.select_all = function () { |
|
139 | DirectViewWidget.prototype.select_all = function () { | |
121 | var start = {line: 0, ch: 0}; |
|
140 | var start = {line: 0, ch: 0}; | |
122 | var nlines = this.code_mirror.lineCount(); |
|
141 | var nlines = this.code_mirror.lineCount(); | |
123 | var last_line = this.code_mirror.getLine(nlines-1); |
|
142 | var last_line = this.code_mirror.getLine(nlines-1); | |
124 | var end = {line: nlines-1, ch: last_line.length}; |
|
143 | var end = {line: nlines-1, ch: last_line.length}; | |
125 | this.code_mirror.setSelection(start, end); |
|
144 | this.code_mirror.setSelection(start, end); | |
126 | }; |
|
145 | }; | |
127 |
|
146 | |||
128 |
|
147 | |||
129 | DirectViewWidget.prototype.clear_input = function () { |
|
148 | DirectViewWidget.prototype.clear_input = function () { | |
130 | this.code_mirror.setValue(''); |
|
149 | this.code_mirror.setValue(''); | |
131 | }; |
|
150 | }; | |
132 |
|
151 | |||
133 |
|
152 | |||
134 | DirectViewWidget.prototype.get_text = function () { |
|
153 | DirectViewWidget.prototype.get_text = function () { | |
135 | return this.code_mirror.getValue(); |
|
154 | return this.code_mirror.getValue(); | |
136 | }; |
|
155 | }; | |
137 |
|
156 | |||
138 |
|
157 | |||
139 | DirectViewWidget.prototype.set_text = function (code) { |
|
158 | DirectViewWidget.prototype.set_text = function (code) { | |
140 | return this.code_mirror.setValue(code); |
|
159 | return this.code_mirror.setValue(code); | |
141 | }; |
|
160 | }; | |
142 |
|
161 | |||
143 | container.show(); |
|
162 | container.show(); | |
144 | var widget = $('<div/>') |
|
163 | var widget = $('<div/>') | |
145 | // When templating over a JSON string, we must use single quotes. |
|
164 | // When templating over a JSON string, we must use single quotes. | |
146 | var targets = '%(targets)s'; |
|
165 | var targets = '%(targets)s'; | |
147 | targets = $.parseJSON(targets); |
|
166 | targets = $.parseJSON(targets); | |
148 | var eiw = new DirectViewWidget(widget, IPython.notebook.kernel, targets); |
|
167 | var eiw = new DirectViewWidget(widget, IPython.notebook.kernel, targets); | |
149 | element.append(widget); |
|
168 | element.append(widget); | |
150 | element.css('padding',0); |
|
169 | element.css('padding',0); | |
151 | setTimeout(function () { |
|
170 | setTimeout(function () { | |
152 | eiw.code_mirror.refresh(); |
|
171 | eiw.code_mirror.refresh(); | |
153 | eiw.code_mirror.focus(); |
|
172 | eiw.code_mirror.focus(); | |
154 | }, 1); |
|
173 | }, 1); | |
155 |
|
174 |
@@ -1,72 +1,79 b'' | |||||
1 | """Widget for interacting with an IPython parallel engine. |
|
1 | """Widget for interacting with an IPython parallel engine. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Brian Granger |
|
5 | * Brian Granger | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008-2012 The IPython Development Team |
|
9 | # Copyright (C) 2008-2012 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | import os |
|
19 | import os | |
20 | import uuid |
|
20 | import uuid | |
21 |
|
21 | |||
22 | from IPython.core.display import display, Javascript |
|
22 | from IPython.core.display import display, Javascript | |
23 | from IPython.core.displaypub import publish_pretty |
|
23 | from IPython.core.displaypub import publish_pretty | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Code |
|
27 | # Code | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | import os, sys |
|
31 | import os, sys | |
32 | from IPython.core.display import Javascript |
|
32 | from IPython.core.display import Javascript | |
33 |
|
33 | |||
34 | from .widget import JavascriptWidget |
|
34 | from .widget import JavascriptWidget | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | class DirectViewWidget(JavascriptWidget): |
|
37 | class DirectViewWidget(JavascriptWidget): | |
38 |
|
38 | |||
39 | def __init__(self, dv): |
|
39 | def __init__(self, dv): | |
40 | self.dv = dv |
|
40 | self.dv = dv | |
41 | self.targets = self.dv.targets |
|
41 | self.targets = self.dv.targets | |
42 | super(DirectViewWidget,self).__init__() |
|
42 | super(DirectViewWidget,self).__init__() | |
43 |
|
43 | |||
44 | def render(self): |
|
44 | def render(self): | |
45 | jscode = self.load_file(u'directview.js') |
|
45 | jscode = self.load_file(u'directview.js') | |
46 | data = { |
|
46 | data = { | |
47 | 'widget_var': self.widget_var, |
|
47 | 'widget_var': self.widget_var, | |
48 | 'targets' : self.encode_json(self.targets) |
|
48 | 'targets' : self.encode_json(self.targets) | |
49 | } |
|
49 | } | |
50 | jscode = jscode % data |
|
50 | jscode = jscode % data | |
51 | return jscode |
|
51 | return jscode | |
52 |
|
52 | |||
53 | def execute(self,code): |
|
53 | def execute(self, code, targets='all'): | |
54 | ar = self.dv.execute(code,block=False,targets='all') |
|
54 | if targets == 'all': | |
|
55 | targets = self.targets | |||
|
56 | ar = self.dv.execute(code,block=False,targets=targets) | |||
55 | ar.wait() |
|
57 | ar.wait() | |
56 | metadata = ar.metadata |
|
58 | metadata = ar.metadata | |
57 | for md in metadata: |
|
59 | if isinstance(metadata, (list,tuple)): | |
58 |
|
|
60 | for md in metadata: | |
59 | publish_pretty(md['stdout'],{'engine_id':md['engine_id']}) |
|
61 | self.publish_md(md) | |
60 | if md['stderr']: |
|
62 | elif isinstance(metadata, dict): | |
61 | publish_pretty(md['stderr'],{'engine_id':md['engine_id']}) |
|
63 | self.publish_md(metadata) | |
62 | if md['pyerr']: |
|
64 | ||
63 | publish_pretty(md['pyerr'],{'engine_id':md['engine_id']}) |
|
65 | def publish_md(self, md): | |
64 |
|
66 | if md['stdout']: | ||
|
67 | publish_pretty(md['stdout'],{'engine_id':md['engine_id']}) | |||
|
68 | if md['stderr']: | |||
|
69 | publish_pretty(md['stderr'],{'engine_id':md['engine_id']}) | |||
|
70 | if md['pyerr']: | |||
|
71 | publish_pretty(md['pyerr'],{'engine_id':md['engine_id']}) | |||
65 |
|
72 | |||
66 |
|
73 | |||
67 | def interact(dv): |
|
74 | def interact(dv): | |
68 | w = DirectViewWidget(dv) |
|
75 | w = DirectViewWidget(dv) | |
69 | w.interact() |
|
76 | w.interact() | |
70 | return w |
|
77 | return w | |
71 |
|
78 | |||
72 |
|
79 |
General Comments 0
You need to be logged in to leave comments.
Login now