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