-
Notifications
You must be signed in to change notification settings - Fork 379
/
NotebookTestScript.dib
449 lines (316 loc) · 9.87 KB
/
NotebookTestScript.dib
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!meta
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}}
#!markdown
# Verify execution count.
The next code cell should have empty square brackets on the left, e.g., "`[ ]`". Execute the cell and upon completion there should be a number there; probably "`[1]`". Execute the cell again and verify that the number in the brackets increments by one, e.g., "`[2]`".
#!csharp
1+1
#!markdown
# Verify Completions.
The next code cell contains the code `Console`. At the end type, `.`. The completions list with properties and methods should be displayed.
#!csharp
Console
#!markdown
# Execute the following cell.
You'll notice a counter that appears and starts incrementing. After a second or two, stop the cell. Your output should look similar to:
```
5
Command cancelled
```
N.b., the old values (e.g., `1`, `2`, `3`, `4`, etc.) should no longer be displayed, but overwritten.
#!csharp
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
var output = string.Empty.Display("text/plain");
var counter = 1;
while (!KernelInvocationContext.Current.CancellationToken.IsCancellationRequested)
{
await Task.Delay(500, KernelInvocationContext.Current.CancellationToken);
output.Update($"{counter++}");
}
#!markdown
# Execute the next two cells.
#!csharp
var x = 123;
#!html
<div id="output">The value is not yet set.</div>
#!markdown
# Execute the next two cells. After execution, the output immediately above each should read:
```
The value is 123.
```
And the output below the next cell should display the same value.
#!javascript
#!share --from csharp x
document.getElementById("output").innerText = `The value is ${x}.`;
console.log(`The value is ${x}.`);
#!javascript
#!set --value @csharp:x --name y
y++;
document.getElementById("output").innerText = `The value is ${y}.`;
console.log(`The value is ${y}.`);
#!markdown
# Execute the next cell. After execution, the output immediately below should be:
```
javascript
```
#!javascript
const jsKernel = kernel.root.findKernelByName('javascript');
console.log(jsKernel.name);
#!markdown
# Execute the next cell. After execution, the output immediately below should be:
```
[".NET","csharp","fsharp","html","http","javascript","kql","mermaid","pwsh","sql","value","webview"]
```
#!javascript
kernels = Array.from(kernel.root.findKernels(s => true).map(k => k.name)).sort();
return kernels;
#!markdown
# Execute the next cell. After execution, the output immediately below should be:
```
1234
```
And the `POLYGLOT NOTEBOOK: VARIABLES` window should contain a variable with name `csharpVarFromJs`, value `1234` and kernel Name `csharp`
#!javascript
await kernel.root.send({ commandType: 'SubmitCode', command: { code: 'var csharpVarFromJs = 1234; Console.WriteLine(csharpVarFromJs);', targetKernelName: 'csharp' } });
#!markdown
# Execute the next cell. After execution, the output immediately below should be:
```
2345
```
And the `POLYGLOT NOTEBOOK: VARIABLES` window should contain a variable with name `csharpVarFromJsUsingApi`, value `1234` and kernel Name `csharp`
#!javascript
let command = new polyglotNotebooks.KernelCommandEnvelope(polyglotNotebooks.SubmitCodeType, { code: 'var csharpVarFromJsUsingApi = 2345; Console.WriteLine(csharpVarFromJsUsingApi);', targetKernelName: 'csharp' });
await kernel.root.send(command);
#!markdown
# Execute the next cell, the output should be displayed as HTML like so:
<details class="dni-treeview" open=""><summary><span class="dni-code-hint"><code>{ Name = Developer, Salary = 42 }</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Name</td><td><div class="dni-plaintext"><pre>Developer</pre></div></td></tr><tr><td>Salary</td><td><div class="dni-plaintext"><pre>42</pre></div></td></tr></tbody></table></div></details>
Next, change the renderer using the `...` element, choose the json and the output should look like so:
``` json
{"Name":"Developer","Salary":42}
```
#!csharp
var value = new { Name = "Developer", Salary = 42 };
value.Display("text/html", "application/json");
#!markdown
# Execute the next cell, the output should be displayed like so:
31.1
#!csharp
10m + 21.1m
#!markdown
# Execute the next cell. It should fail with the message:
```
Error: Value 'undefinedVariableName' not found in kernel pwsh
2
```
#!csharp
#!share --from pwsh undefinedVariableName
1+1
#!markdown
# Execute the next cell and verify the following error appears:
```
input.fsx (1,11)-(1,12) parse error Unexpected token '+' or incomplete expression
```
#!fsharp
let x = 1 +
#!markdown
# Complex console output
#!markdown
The output should look like:
<span style="color:#00FF00">this is green</span>
this is white
<span style="color:#FF0000">this is red</span>
#!csharp
Console.Write("\x1b[38;2;0;255;0m");
Console.WriteLine("this is green");
Console.Write("\x1b[0m");
"this is white".Display();
Console.Error.Write("\x1b[38;2;255;0;0m");
Console.Error.WriteLine("this is red");
Console.Error.Write("\x1b[0m");
#!markdown
# vscode kernel and user input prompts
This cell should prompt the user for input. Type something into the input field. Whatever you typed should be displayed in the output.
#!csharp
var input = await Microsoft.DotNet.Interactive.Kernel.GetInputAsync("give me data");
input
#!fsharp
let input = (task {
return! Microsoft.DotNet.Interactive.Kernel.GetInputAsync ("give me data")
}).Result
input
#!markdown
This should prompt you for input.
#!value
#!value --from-value @input:wat --name value_from_input
#!markdown
This should output the thing you typed into the prompt.
#!fsharp
#!share --from value value_from_input
value_from_input
#!markdown
Now check the variable explorer. Is the value there in the `value` kernel?
#!markdown
This should prompt you for input. Type something into the input field. Whatever you typed should be displayed in the output.
#!csharp
#!set --name csharpVarFromInput --value @input:"Please enter a value"
csharpVarFromInput
#!markdown
# Run mermaid markdown:
Execute the markadown in the cell, this should produce a graphic visualization.
#!mermaid
mindmap
root((.NET Interactive))
Commands
SubmitCode
Cancel
SendValue
Events
CommandSucceeded
CommandFailed
ValueProduced
Kernels
Kernel
CompositeKernel
ProxyKernel
Directives
Connect
SQL
KQL
Jupyter
#!markdown
# Check this manual scenario:
1. `Ctrl+Shift+P` => "Polyglot Notebook: Create new blank notebook"
2. Select "Create as .dib"
3. Select "F#"
4. You should have a new empty notebook named something like "Untitled-1.dib" with a single cell of type "F# (Polyglot Notebook)".
5. Set the cell contents to `1+1` and execute.
6. Verify output of `2`.
7. Save to disk.
8. Open that file in notepad.
9. The contents should look like this:
```
#!meta
{"kernelInfo":{"defaultKernelName":"fsharp","items":[{"name":"fsharp","languageName":"F#","aliases":["f#","F#"]},{"name":"vscode","aliases":["frontend"]}]}}
#!fsharp
1+1
```
#!markdown
# Execute the above scenario again with the following changes:
2. Select "Create as .ipynb"
4. File should be named something like "Untitled-1.ipynb".
(new steps)
9. The contents should look like this:
``` json
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div class=\"dni-plaintext\"><pre>2</pre></div><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"1+1"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (F#)",
"language": "F#",
"name": ".net-fsharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "fsharp",
"items": [
{
"aliases": [
"f#",
"F#"
],
"languageName": "F#",
"name": "fsharp"
},
{
"aliases": [
"frontend"
],
"languageName": null,
"name": "vscode"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
```
#!markdown
# Open the two previously saved notebooks via the following:
1. `Ctrl+Shift+P` => "Polyglot Notebook: Open notebook"
2. Select the "Untitled-1.dib"/"Untitled-1.ipynb" from before.
3. Verify that it contains a single F# cell with the contents "1+1" and that it can execute.
#!markdown
# Test adding cell to notebook (VSCode only)
#!csharp
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
var _ = Kernel.Root.SendAsync(new SendEditableCode("fsharp","#!share --from csharp x\nx"));
#!markdown
The code below will add 2 markdown cells
#!csharp
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
for(var i = 0; i<2;i++){
var _ = await Kernel.Root.SendAsync(new SendEditableCode("markdown",$"# markdown cell {i}"));
}