From dbe68c58167e4851c15320dc38607e9cb8be0937 Mon Sep 17 00:00:00 2001 From: Diego Colombo Date: Mon, 5 Jun 2023 23:21:53 +0100 Subject: [PATCH] add ipynb --- NotebookTestScript.ipynb | 986 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 986 insertions(+) create mode 100644 NotebookTestScript.ipynb diff --git a/NotebookTestScript.ipynb b/NotebookTestScript.ipynb new file mode 100644 index 0000000000..cb2c721d42 --- /dev/null +++ b/NotebookTestScript.ipynb @@ -0,0 +1,986 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Verify execution count.\n", + "\n", + "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]`\"." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "1+1" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the following cell.\n", + "\n", + "You'll notice a counter that appears and starts incrementing. After a second or two, stop the cell. Your output should look similar to:\n", + "\n", + "```\n", + "5\n", + "Command cancelled\n", + "```\n", + "\n", + "N.b., the old values (e.g., `1`, `2`, `3`, `4`, etc.) should no longer be displayed, but overwritten." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "651419f5", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "using System.Threading.Tasks;\n", + "using Microsoft.DotNet.Interactive;\n", + "\n", + "var output = string.Empty.Display(\"text/plain\");\n", + "var counter = 1;\n", + "while (!KernelInvocationContext.Current.CancellationToken.IsCancellationRequested)\n", + "{\n", + " await Task.Delay(500, KernelInvocationContext.Current.CancellationToken);\n", + " output.Update($\"{counter++}\");\n", + "}" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next two cells." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4e2e24c", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "var x = 123;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b0aa8c10", + "metadata": { + "dotnet_interactive": { + "language": "html" + }, + "polyglot_notebook": { + "kernelName": "html" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "
The value is not yet set.
" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next two cells. After execution, the output immediately above each should read:\n", + "\n", + "```\n", + "The value is 123.\n", + "```\n", + "\n", + "And the output below the next cell should display the same value." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92eda896", + "metadata": { + "dotnet_interactive": { + "language": "javascript" + }, + "polyglot_notebook": { + "kernelName": "javascript" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "#!share --from csharp x\n", + "document.getElementById(\"output\").innerText = `The value is ${x}.`;\n", + "console.log(`The value is ${x}.`);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "48045d60", + "metadata": { + "dotnet_interactive": { + "language": "javascript" + }, + "polyglot_notebook": { + "kernelName": "javascript" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "#!set --value @csharp:x --name y\n", + "y++;\n", + "document.getElementById(\"output\").innerText = `The value is ${y}.`;\n", + "console.log(`The value is ${y}.`);" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next cell. After execution, the output immediately below should be:\n", + "\n", + "```\n", + "javascript\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba1ceec1", + "metadata": { + "dotnet_interactive": { + "language": "javascript" + }, + "polyglot_notebook": { + "kernelName": "javascript" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "const jsKernel = kernel.root.findKernelByName('javascript');\n", + "console.log(jsKernel.name);" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next cell. After execution, the output immediately below should be:\n", + "\n", + "```\n", + "[\".NET\",\"csharp\",\"fsharp\",\"html\",\"javascript\",\"kql\",\"mermaid\",\"pwsh\",\"sql\",\"value\",\"vscode\",\"webview\"]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43ce1410", + "metadata": { + "dotnet_interactive": { + "language": "javascript" + }, + "polyglot_notebook": { + "kernelName": "javascript" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "kernels = Array.from(kernel.root.findKernels(s => true).map(k => k.name)).sort();\n", + "return kernels;" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next cell. After execution, the output immediately below should be:\n", + "\n", + "```\n", + "1234\n", + "```\n", + "\n", + "And the `POLYGLOT NOTEBOOK: VARIABLES` window should contain a variable with name `csharpVarFromJs`, value `1234` and kernel Name `csharp`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2237b214", + "metadata": { + "dotnet_interactive": { + "language": "javascript" + }, + "polyglot_notebook": { + "kernelName": "javascript" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "await kernel.root.send({ commandType: 'SubmitCode', command: { code: 'var csharpVarFromJs = 1234; Console.WriteLine(csharpVarFromJs);', targetKernelName: 'csharp' } });" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next cell. After execution, the output immediately below should be:\n", + "\n", + "```\n", + "2345\n", + "```\n", + "And the `POLYGLOT NOTEBOOK: VARIABLES` window should contain a variable with name `csharpVarFromJsUsingApi`, value `1234` and kernel Name `csharp`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae2fe135", + "metadata": { + "dotnet_interactive": { + "language": "javascript" + }, + "polyglot_notebook": { + "kernelName": "javascript" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "let command = new polyglotNotebooks.KernelCommandEnvelope(polyglotNotebooks.SubmitCodeType, { code: 'var csharpVarFromJsUsingApi = 2345; Console.WriteLine(csharpVarFromJsUsingApi);', targetKernelName: 'csharp' });\n", + "await kernel.root.send(command);" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next cell, the output should be displayed as HTML like so:\n", + "\n", + "| Name | Salary |\n", + " :---|---: |\n", + "| Developer | 42 |\n", + "\n", + "change the renderer using the `...` element, choose the json and the output should look like so:\n", + "\n", + "``` json\n", + "{\"Name\":\"Developer\",\"Salary\":42}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19eb0cef", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "var value = new { Name = \"Developer\", Salary = 42 };\n", + "value.Display(\"application/json\", \"text/html\");" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next cell, the output should be displayed like so:\n", + "\n", + "31.1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8563e5a", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "10m + 21.1m" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next cell. It should fail with the message:\n", + "```\n", + "Error: Value 'undefinedVariableName' not found in kernel pwsh\n", + "2\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4be9d9dc", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "#!share --from pwsh undefinedVariableName\n", + "\n", + "1+1" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the next cell and verify the following error appears:\n", + "\n", + "```\n", + "input.fsx (1,11)-(1,12) parse error Unexpected token '+' or incomplete expression\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0ad583b9", + "metadata": { + "dotnet_interactive": { + "language": "fsharp" + }, + "polyglot_notebook": { + "kernelName": "fsharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "let x = 1 +" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Complex console output" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The output should look like:\n", + "\n", + "this is green\n", + "\n", + "this is white\n", + "\n", + "this is red" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74551494", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "Console.Write(\"\\x1b[38;2;0;255;0m\");\n", + "Console.WriteLine(\"this is green\");\n", + "Console.Write(\"\\x1b[0m\");\n", + "\"this is white\".Display();\n", + "Console.Error.Write(\"\\x1b[38;2;255;0;0m\");\n", + "Console.Error.WriteLine(\"this is red\");\n", + "Console.Error.Write(\"\\x1b[0m\");" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# vscode kernel and user input prompts\n", + "This cell should prompt the user for input. Type something into the input field. Whatever you typed should be displayed in the output." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b15ef063", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "var input = await Microsoft.DotNet.Interactive.Kernel.GetInputAsync(\"give me data\");\n", + "input" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "09ed2bac", + "metadata": { + "dotnet_interactive": { + "language": "fsharp" + }, + "polyglot_notebook": { + "kernelName": "fsharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "let input = task {\n", + " return! Microsoft.DotNet.Interactive.Kernel.GetInputAsync (\"give me data\")\n", + " } \n", + "input.Result" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This should prompt you for input." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05c36b21", + "metadata": { + "dotnet_interactive": { + "language": "value" + }, + "polyglot_notebook": { + "kernelName": "value" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "#!value --from-value @input:wat --name value_from_input" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This should output the thing you typed into the prompt." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1d588f6f", + "metadata": { + "dotnet_interactive": { + "language": "fsharp" + }, + "polyglot_notebook": { + "kernelName": "fsharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "#!share --from value value_from_input\n", + "value_from_input" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now check the variable explorer. Is the value there in the `value` kernel?" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This should prompt you for input. Type something into the input field. Whatever you typed should be displayed in the output." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ae01e6c", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "#!set --name csharpVarFromInput --value @input:\"Please enter a value\"\n", + "csharpVarFromInput" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Run mermaid markdown:\n", + "Execute the markadown in the cell, this should produce a graphic visualization." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4cf36dc0", + "metadata": { + "dotnet_interactive": { + "language": "mermaid" + }, + "polyglot_notebook": { + "kernelName": "mermaid" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "mindmap\n", + " root((.NET Interactive))\n", + " Commands\n", + " SubmitCode\n", + " Cancel\n", + " SendValue\n", + " Events\n", + " CommandSucceeded\n", + " CommandFailed\n", + " ValueProduced\n", + " Kernels\n", + " Kernel\n", + " CompositeKernel\n", + " ProxyKernel\n", + " Directives\n", + " Connect\n", + " SQL\n", + " KQL\n", + " Jupyter" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Check this manual scenario:\n", + "\n", + "1. `Ctrl+Shift+P` => \"Polyglot Notebook: Create new blank notebook\"\n", + "2. Select \"Create as .dib\"\n", + "3. Select \"F#\"\n", + "4. You should have a new empty notebook named something like \"Untitled-1.dib\" with a single cell of type \"F# (Polyglot Notebook)\".\n", + "5. Set the cell contents to `1+1` and execute.\n", + "6. Verify output of `2`.\n", + "7. Save to disk.\n", + "8. Open that file in notepad.\n", + "9. The contents should look like this:\n", + "\n", + " ```\n", + " #!meta\n", + " {\"kernelInfo\":{\"defaultKernelName\":\"fsharp\",\"items\":[{\"name\":\"fsharp\",\"languageName\":\"F#\",\"aliases\":[\"f#\",\"F#\"]},{\"name\":\"vscode\",\"aliases\":[\"frontend\"]}]}}\n", + "\n", + " #!fsharp\n", + "\n", + " 1+1\n", + " ```" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Execute the above scenario again with the following changes:\n", + "\n", + "2. Select \"Create as .ipynb\"\n", + "4. File should be named something like \"Untitled-1.ipynb\".\n", + "\n", + "(new steps)\n", + "\n", + "9. The contents should look like this:\n", + "\n", + "``` json\n", + "{\n", + " \"cells\": [\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 1,\n", + " \"metadata\": {\n", + " \"dotnet_interactive\": {\n", + " \"language\": \"fsharp\"\n", + " },\n", + " \"polyglot_notebook\": {\n", + " \"kernelName\": \"fsharp\"\n", + " }\n", + " },\n", + " \"outputs\": [\n", + " {\n", + " \"data\": {\n", + " \"text/html\": [\n", + " \"
2
\"\n", + " ]\n", + " },\n", + " \"metadata\": {},\n", + " \"output_type\": \"display_data\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"1+1\"\n", + " ]\n", + " }\n", + " ],\n", + " \"metadata\": {\n", + " \"kernelspec\": {\n", + " \"display_name\": \".NET (F#)\",\n", + " \"language\": \"F#\",\n", + " \"name\": \".net-fsharp\"\n", + " },\n", + " \"polyglot_notebook\": {\n", + " \"kernelInfo\": {\n", + " \"defaultKernelName\": \"fsharp\",\n", + " \"items\": [\n", + " {\n", + " \"aliases\": [\n", + " \"f#\",\n", + " \"F#\"\n", + " ],\n", + " \"languageName\": \"F#\",\n", + " \"name\": \"fsharp\"\n", + " },\n", + " {\n", + " \"aliases\": [\n", + " \"frontend\"\n", + " ],\n", + " \"languageName\": null,\n", + " \"name\": \"vscode\"\n", + " }\n", + " ]\n", + " }\n", + " }\n", + " },\n", + " \"nbformat\": 4,\n", + " \"nbformat_minor\": 2\n", + "}\n", + "```" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Open the two previously saved notebooks via the following:\n", + "\n", + "1. `Ctrl+Shift+P` => \"Polyglot Notebook: Open notebook\"\n", + "2. Select the \"Untitled-1.dib\"/\"Untitled-1.ipynb\" from before.\n", + "3. Verify that it contains a single F# cell with the contents \"1+1\" and that it can execute." + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Test adding cell to notebook (VSCode only)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12c0d2c8", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "using Microsoft.DotNet.Interactive;\n", + "using Microsoft.DotNet.Interactive.Commands;\n", + "\n", + "var _ = Kernel.Root.SendAsync(new SendEditableCode(\"fsharp\",\"#!share --from csharp x\\nx\"));" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The code below will add 2 markdown cells" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "830ffe6f", + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "using Microsoft.DotNet.Interactive;\n", + "using Microsoft.DotNet.Interactive.Commands;\n", + "\n", + "for(var i = 0; i<2;i++){\n", + " var _ = await Kernel.Root.SendAsync(new SendEditableCode(\"markdown\",$\"# markdown cell {i}\"));\n", + "} " + ] + } + ], + "metadata": { + "kernelInfo": { + "defaultKernelName": null, + "items": [ + { + "aliases": [ + "c#", + "cs" + ], + "languageName": "C#", + "name": "csharp" + }, + { + "aliases": [ + "f#", + "fs" + ], + "languageName": "F#", + "name": "fsharp" + }, + { + "aliases": [ + "powershell" + ], + "languageName": "PowerShell", + "name": "pwsh" + }, + { + "aliases": [ + "js" + ], + "languageName": "JavaScript", + "name": "javascript" + }, + { + "languageName": "HTML", + "name": "html" + }, + { + "languageName": "SQL", + "name": "sql" + }, + { + "languageName": "KQL", + "name": "kql" + }, + { + "languageName": "Mermaid", + "name": "mermaid" + }, + { + "languageName": "HTTP", + "name": "http" + }, + { + "name": "value" + } + ] + }, + "language_info": { + "name": "csharp" + }, + "polyglot_notebook": { + "kernelInfo": { + "defaultKernelName": "csharp", + "items": [ + { + "aliases": [], + "name": "csharp" + } + ] + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}