/
select-kernel.tsx
371 lines (338 loc) · 9.28 KB
/
select-kernel.tsx
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
help users selecting a kernel
*/
import { React, Component, Rendered } from "../app-framework"; // TODO: this will move
import {
Map as ImmutableMap,
List,
OrderedMap /*, List as ImmutableList*/
} from "immutable";
import * as misc from "smc-util/misc";
const { Icon, /* Markdown, /*Space,*/ Loading } = require("../r_misc"); // TODO: import types
const {
Button,
Col,
Row,
ButtonGroup,
/* MenuItem,
DropdownButton, */
Alert
} = require("react-bootstrap"); // TODO: import types
import { TKernel } from "./util";
const { COLORS } = require("smc-util/theme");
const row_style: React.CSSProperties = {
marginTop: "5px",
marginBottom: "5px"
};
const main_style: React.CSSProperties = {
padding: "20px 10px",
overflowY: "auto",
overflowX: "hidden"
};
interface IKernelSelectorProps {
actions: any;
site_name: string;
kernel?: string;
kernel_info?: any;
default_kernel?: string;
kernel_selection?: ImmutableMap<string, string>;
kernels_by_name?: OrderedMap<string, ImmutableMap<string, string>>;
kernels_by_language?: OrderedMap<string, List<string>>;
closestKernel?: TKernel;
}
interface IKernelSelectorState {}
export class KernelSelector extends Component<
IKernelSelectorProps,
IKernelSelectorState
> {
constructor(props: IKernelSelectorProps, context: any) {
super(props, context);
this.state = {};
}
// render_select_button() {
// const disabled = this.state.selected_kernel == null;
// const msg = disabled
// ? "Select a kernel"
// : `Use ${this.kernel_name(this.state.selected_kernel!)}`;
// return (
// <Button
// key={"select"}
// bsStyle={disabled ? "default" : "primary"}
// disabled={disabled}
// onClick={() =>
// this.props.actions.select_kernel(this.state.selected_kernel)
// }
// >
// {msg}
// </Button>
// );
// }
// the idea here is to not set the kernel, but still render the notebook.
// looks like that's not easy, and well, probably incompatible with classical jupyter.
/*
<Row style={row_style} className={"pull-right"}>
{this.close_button()}
</Row>
close_button() {
return (
<Button
key={"close"}
bsStyle={"default"}
onClick={() => this.props.actions.select_kernel(null)}
>
{"View without kernel"}
</Button>
);
}
*/
kernel_name(name: string) {
return this.kernel_attr(name, "display_name");
}
kernel_attr(name: string, attr: string): string {
if (this.props.kernels_by_name == null) return "";
const k = this.props.kernels_by_name.get(name);
if (k == null) return "";
return k.get(attr, name);
}
render_suggested_link(cocalc) {
if (cocalc == null) return;
const url: string | undefined = cocalc.get("url");
const descr: string | undefined = cocalc.get("description", "");
if (url != null) {
return (
<a href={url} target={"_blank"}>
{descr}
</a>
);
} else {
return descr;
}
}
render_kernel_button(
name: string,
size?: string,
show_icon: boolean = true
): Rendered {
const lang = this.kernel_attr(name, "language");
let icon: Rendered | undefined = undefined;
if (lang != null && show_icon) {
if (["python", "r", "sagemath", "octave", "julia"].indexOf(lang) >= 0) {
icon = <Icon name={`cc-icon-${lang}`} />;
}
// TODO do other languages have icons?
}
return (
<Button
key={`kernel-${lang}-${name}`}
onClick={() => this.props.actions.select_kernel(name)}
bsSize={size}
>
{icon} {this.kernel_name(name)}
</Button>
);
}
render_suggested() {
if (
this.props.kernel_selection == null ||
this.props.kernels_by_name == null
)
return;
const entries: Rendered[] = [];
this.props.kernel_selection
.sort((a, b) => this.kernel_name(a).localeCompare(this.kernel_name(b)))
.map((name, lang) => {
const cocalc: ImmutableMap<
string,
any
> = this.props.kernels_by_name!.getIn(
[name, "metadata", "cocalc"],
null
);
if (cocalc == null) return;
const prio: number = cocalc.get("priority", 0);
if (prio < 10) return;
entries.push(
<Row key={lang} style={row_style}>
<Col sm={6}>{this.render_kernel_button(name)}</Col>
<Col sm={6}>
<div>{this.render_suggested_link(cocalc)}</div>
</Col>
</Row>
);
});
if (entries.length == 0) return;
return (
<Row style={row_style}>
<h4>Suggested kernels</h4>
<Col>{entries}</Col>
</Row>
);
}
// render_all_selected_link() {
// if (this.props.kernels_by_name == null) return;
// const name = this.state.selected_kernel;
// if (name == null) return;
// const cocalc: ImmutableMap<string, any> = this.props.kernels_by_name.getIn(
// [name, "metadata", "cocalc"],
// null
// );
// return this.render_suggested_link(cocalc);
// }
render_all_langs(): Rendered[] | undefined {
if (this.props.kernels_by_language == null) return;
const label: React.CSSProperties = {
fontWeight: "bold",
color: COLORS.GRAY_D
};
const all: Rendered[] = [];
this.props.kernels_by_language.forEach((names, lang) => {
const kernels = names.map(name =>
this.render_kernel_button(name, "small", false)
);
all.push(
<Row key={lang} style={row_style}>
<Col sm={2} style={label}>
{misc.capitalize(lang)}
</Col>
<Col sm={10}>
<ButtonGroup>{kernels}</ButtonGroup>
</Col>
</Row>
);
return true;
});
return all;
}
render_all() {
if (this.props.kernels_by_language == null) return;
return (
<Row style={row_style}>
<h4>All kernels</h4>
<Col>{this.render_all_langs()}</Col>
</Row>
);
}
render_last() {
if (this.props.default_kernel == null) return;
return (
<Row style={row_style}>
<h4>Quick selection</h4>
<div>
Your most recently selected kernel is{" "}
{this.render_kernel_button(this.props.default_kernel)}.
</div>
</Row>
);
}
render_top() {
if (this.props.kernel == null || this.props.kernel_info == null) {
let msg: Rendered;
// kernel, but no info means it is not known
if (this.props.kernel != null && this.props.kernel_info == null) {
msg = (
<>
Your notebook kernel <code>"{this.props.kernel}"</code> does not
exist on {this.props.site_name}.
</>
);
} else {
msg = <>This notebook has no kernel.</>;
}
return (
<Row style={row_style}>
<strong>{msg}</strong> A working kernel is required in order to
evaluate the code in the notebook. Based on the programming language
you want to work with, you have to select one.
</Row>
);
} else {
return (
<Row style={row_style}>
<strong>Select a new kernel.</strong> Your currently selected one is{" "}
{this.render_kernel_button(this.props.kernel, "small")}.
</Row>
);
}
}
render_unknown() {
const closestKernel = this.props.closestKernel;
if (this.props.kernel_info != null || closestKernel == null) return;
const closestKernelName = closestKernel.get("name");
if (closestKernelName == null) return;
return (
<Row style={row_style}>
<Alert bsStyle={"danger"}>
<h4>Unknown Kernel</h4>
<div>
A similar kernel might be{" "}
{this.render_kernel_button(closestKernelName)}.
</div>
</Alert>
</Row>
);
}
render_footer(): Rendered {
return (
<Row style={{ ...row_style, ...{ color: COLORS.GRAY } }}>
<strong>Note:</strong> You can always change the selected kernel later
in the »Kernel« menu or by clicking on the kernel information at the top
right.
</Row>
);
}
render_close_button(): Rendered | undefined {
if (this.props.kernel == null || this.props.kernel_info == null) return;
return (
<Button
style={{ float: "right" }}
onClick={() => this.props.actions.hide_select_kernel()}
>
Close
</Button>
);
}
render_body(): Rendered {
if (
this.props.kernels_by_name == null ||
this.props.kernel_selection == null
) {
return (
<Row style={row_style}>
<Loading />
</Row>
);
} else {
return (
<>
{this.render_top()}
{this.render_unknown()}
{this.render_last()}
{this.render_suggested()}
{this.render_all()}
<hr />
{this.render_footer()}
</>
);
}
}
render_head(): Rendered {
return (
<Row style={row_style}>
<h3>
{"Select a Kernel"}
{this.render_close_button()}
</h3>
</Row>
);
}
render(): Rendered {
return (
<div style={main_style} className={"smc-vfill"}>
<Col md={8} mdOffset={2}>
{this.render_head()}
{this.render_body()}
</Col>
</div>
);
}
}