1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 18:19:08 -05:00
denoland-deno/cli/tests/testdata/jupyter/integration_test.ipynb
Trevor Manz 48bb3b2b0f
feat(unstable): Await return from Jupyter.display (#20807)
Allows `Jupyter.display` to return a promise.

Example:

```javascript
class WikiPage {
    constructor(public name) {}
    async [Symbol.for("Jupyter.display")]() {
        let response = await fetch("https://en.wikipedia.org/wiki/" + this.name);
        return { "text/html": await response.text() }
    }
}

new WikiPage("Deno_(software)")
```
2023-10-06 21:26:11 +00:00

899 lines
19 KiB
Text

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "182aef1d",
"metadata": {},
"source": [
"# Integration Tests for Deno Jupyter\n",
"This notebook contains a number of tests to ensure that Jupyter is working as expected. You should be able to select \"Kernel -> Restart Kernel and Run All\" in Jupyter's notebook UI to run the tests."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "d7705d88",
"metadata": {},
"source": [
"## Passing Tests"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "669f972e",
"metadata": {
"heading_collapsed": true
},
"source": [
"### Simple Tests"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e7e8a512",
"metadata": {
"hidden": true
},
"source": [
"#### This test should print \"hi\".\n",
"If this doesn't work, everything else will probably fail :)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a5d38758",
"metadata": {
"hidden": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hi\n"
]
}
],
"source": [
"console.log(\"hi\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "bc5ce8e3",
"metadata": {
"hidden": true
},
"source": [
"#### Top-level await"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f7fa885a",
"metadata": {
"hidden": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is \u001b[33m42\u001b[39m\n"
]
}
],
"source": [
"let x = await Promise.resolve(42);\n",
"console.log(\"x is\", x);"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c21455ae",
"metadata": {
"hidden": true
},
"source": [
"#### TypeScript transpiling\n",
"Credit to [typescriptlang.org](https://www.typescriptlang.org/docs/handbook/interfaces.html) for this code"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "08a17340",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"{ color: \u001b[32m\"red\"\u001b[39m, area: \u001b[33m10000\u001b[39m }"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interface SquareConfig {\n",
" color?: string;\n",
" width?: number;\n",
"}\n",
" \n",
"function createSquare(config: SquareConfig): { color: string; area: number } {\n",
" return {\n",
" color: config.color || \"red\",\n",
" area: config.width ? config.width * config.width : 20,\n",
" };\n",
"}\n",
" \n",
"createSquare({ colour: \"red\", width: 100 });"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "eaa0ebc0",
"metadata": {
"heading_collapsed": true
},
"source": [
"### Return Values"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "52876276",
"metadata": {
"hidden": true
},
"source": [
"#### undefined should not return a value"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "bbf2c09b",
"metadata": {
"hidden": true
},
"outputs": [],
"source": [
"undefined"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e175c803",
"metadata": {
"hidden": true
},
"source": [
"#### null should return \"null\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d9801d80",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[1mnull\u001b[22m"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"null"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "a2a716dc",
"metadata": {
"hidden": true
},
"source": [
"#### boolean should return the boolean"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "cfaac330",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[33mtrue\u001b[39m"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"true"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "8d9f1aba",
"metadata": {
"hidden": true
},
"source": [
"#### number should return the number"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "ec3be2da",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[33m42\u001b[39m"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"42"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "60965915",
"metadata": {
"hidden": true
},
"source": [
"#### string should return the string"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "997cf2d7",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[32m\"this is a test of the emergency broadcast system\"\u001b[39m"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"this is a test of the emergency broadcast system\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "fe38dc27",
"metadata": {
"hidden": true
},
"source": [
"#### bigint should return the bigint in literal format"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "44b63807",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[33m31337n\u001b[39m"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"31337n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "843ccb6c",
"metadata": {
"hidden": true
},
"source": [
"#### symbol should return a string describing the symbol"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "e10c0d31",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[32mSymbol(foo)\u001b[39m"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Symbol(\"foo\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "171b817f",
"metadata": {
"hidden": true
},
"source": [
"#### object should describe the object inspection"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "81c99233",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"{ foo: \u001b[32m\"bar\"\u001b[39m }"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{foo: \"bar\"}"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "6caeb583",
"metadata": {
"hidden": true
},
"source": [
"#### resolve returned promise"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "43c1581b",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"Promise { \u001b[32m\"it worked!\"\u001b[39m }"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Promise.resolve(\"it worked!\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "9a34b725",
"metadata": {
"hidden": true
},
"outputs": [
{
"data": {
"text/plain": [
"Promise {\n",
" \u001b[36m<rejected>\u001b[39m Error: it failed!\n",
" at <anonymous>:2:16\n",
"}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Promise.reject(new Error(\"it failed!\"));"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "b5c7b819",
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "Error",
"evalue": "this is a test",
"output_type": "error",
"traceback": [
"Stack trace:",
"Error: this is a test",
" at foo (<anonymous>:3:9)",
" at <anonymous>:4:3"
]
}
],
"source": [
"(function foo() {\n",
" throw new Error(\"this is a test\")\n",
"})()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "14844fc9-536e-4121-a9bd-fc2d3f7b6395",
"metadata": {},
"outputs": [
{
"ename": "Unknown error",
"evalue": "a party",
"output_type": "error",
"traceback": [
"Stack trace:",
"\"a party\"",
" at <unknown>"
]
}
],
"source": [
"throw \"a party\""
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "72d01fdd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Promise {\n",
" \u001b[36m<rejected>\u001b[39m NotFound: No such file or directory (os error 2): readfile ''\n",
" at async Object.readFile (ext:deno_fs/30_fs.js:716:18) {\n",
" name: \u001b[32m\"NotFound\"\u001b[39m,\n",
" code: \u001b[32m\"ENOENT\"\u001b[39m\n",
" }\n",
"}"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Deno.readFile(1)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "28cf59d0-6908-4edc-bb10-c325beeee362",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello from Deno!\n"
]
}
],
"source": [
"console.log(\"Hello from Deno!\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "8d5485c3-0da3-43fe-8ef5-a61e672f5e81",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[48;2;21;128;61m\u001b[37m Hello Deno \u001b[0m\n"
]
}
],
"source": [
"console.log(\"%c Hello Deno \", \"background-color: #15803d; color: white;\");"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "1401d9d5-6994-4c7b-b55a-db3c16a1e2dc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[32m\"Cool 🫡\"\u001b[39m"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Cool 🫡\""
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "7afdaa0a-a2a0-4f52-8c7d-b6c5f237aa0d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"┌───────┬────────┐\n",
"│ (idx) │ Values │\n",
"├───────┼────────┤\n",
"│ 0 │ 1 │\n",
"│ 1 │ 2 │\n",
"│ 2 │ 3 │\n",
"└───────┴────────┘\n"
]
}
],
"source": [
"console.table([1, 2, 3])"
]
},
{
"cell_type": "markdown",
"id": "9f38f1eb",
"metadata": {},
"source": [
"## Unit Tests With `Deno.test()`"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b33808fd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"passing test ... \u001b[0m\u001b[32mok\u001b[0m \u001b[0m\u001b[38;5;245m(1ms)\u001b[0m\n",
"passing test with steps ...\n",
" step 1 ... \u001b[0m\u001b[32mok\u001b[0m \u001b[0m\u001b[38;5;245m(0ms)\u001b[0m\n",
" step 2 ... \u001b[0m\u001b[32mok\u001b[0m \u001b[0m\u001b[38;5;245m(0ms)\u001b[0m\n",
"passing test with steps ... \u001b[0m\u001b[32mok\u001b[0m \u001b[0m\u001b[38;5;245m(1ms)\u001b[0m\n",
"failing test ... \u001b[0m\u001b[31mFAILED\u001b[0m \u001b[0m\u001b[38;5;245m(1ms)\u001b[0m\n",
"\n",
"\u001b[0m\u001b[1m\u001b[37m\u001b[41m ERRORS \u001b[0m\n",
"\n",
"failing test \u001b[0m\u001b[38;5;245m=> <anonymous>:7:6\u001b[0m\n",
"\u001b[0m\u001b[1m\u001b[31merror\u001b[0m: Error: some message\n",
" at \u001b[0m\u001b[36m<anonymous>\u001b[0m:\u001b[0m\u001b[33m8\u001b[0m:\u001b[0m\u001b[33m9\u001b[0m\n",
"\n",
"\u001b[0m\u001b[1m\u001b[37m\u001b[41m FAILURES \u001b[0m\n",
"\n",
"failing test \u001b[0m\u001b[38;5;245m=> <anonymous>:7:6\u001b[0m\n",
"\n",
"\u001b[0m\u001b[31mFAILED\u001b[0m | 2 passed (2 steps) | 1 failed \u001b[0m\u001b[38;5;245m(0ms)\u001b[0m\n"
]
}
],
"source": [
"Deno.test(\"passing test\", () => {});\n",
"\n",
"Deno.test(\"passing test with steps\", async (t) => {\n",
" await t.step(\"step 1\", () => {});\n",
" await t.step(\"step 2\", () => {});\n",
"});\n",
"\n",
"Deno.test(\"failing test\", () => {\n",
" throw new Error(\"some message\");\n",
"});\n"
]
},
{
"cell_type": "markdown",
"id": "8822eed9-a801-4c1b-81c0-00e4ff180f40",
"metadata": {},
"source": [
"## Broadcasting Display Updates"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "8e93df23-06eb-414b-98d4-51fbebb53d1f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<b>Complete ✅</b>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"await Deno.jupyter.broadcast(\"display_data\", {\n",
" data: { \"text/html\": \"<b>Processing.</b>\" },\n",
" metadata: {},\n",
" transient: { display_id: \"progress\" }\n",
"});\n",
"\n",
"await new Promise((resolve) => setTimeout(resolve, 500));\n",
"\n",
"await Deno.jupyter.broadcast(\"update_display_data\", {\n",
" data: { \"text/html\": \"<b>Processing..</b>\" },\n",
" metadata: {},\n",
" transient: { display_id: \"progress\" }\n",
"});\n",
"\n",
"await new Promise((resolve) => setTimeout(resolve, 500));\n",
"\n",
"await Deno.jupyter.broadcast(\"update_display_data\", {\n",
" data: { \"text/html\": \"<b>Processing...</b>\" },\n",
" metadata: {},\n",
" transient: { display_id: \"progress\" }\n",
"});\n",
"\n",
"await new Promise((resolve) => setTimeout(resolve, 500));\n",
"\n",
"await Deno.jupyter.broadcast(\"update_display_data\", {\n",
" data: { \"text/html\": \"<b>Complete ✅</b>\" },\n",
" metadata: {},\n",
" transient: { display_id: \"progress\" }\n",
"});"
]
},
{
"cell_type": "markdown",
"id": "c0eaa476-b9c8-4d00-8a97-24e6b8e861fd",
"metadata": {},
"source": [
"## Comms"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "6e9b530f-554d-4ef7-a5d6-69432283fd40",
"metadata": {},
"outputs": [],
"source": [
"// Smoke test: Send example Jupyter Widgets messages with \"extra\" context.\n",
"// No return because we don't have a front-end widget to get the message from.\n",
"await Deno.jupyter.broadcast(\n",
" \"comm_open\",\n",
" {\n",
" \"comm_id\": \"foo\",\n",
" \"target_name\": \"jupyter.widget\",\n",
" \"data\": {\n",
" \"state\": {},\n",
" },\n",
" },\n",
" {\n",
" \"metadata\": { \"version\": \"2.1.0\" },\n",
" },\n",
");\n",
"\n",
"await Deno.jupyter.broadcast(\n",
" \"comm_msg\",\n",
" {\n",
" \"comm_id\": \"foo\",\n",
" \"data\": {\n",
" \"method\": \"update\",\n",
" \"state\": { \"answer\": null },\n",
" \"buffer_paths\": [[\"answer\"]]\n",
" },\n",
" },\n",
" {\n",
" \"buffers\": [new Uint8Array([42])],\n",
" },\n",
");"
]
},
{
"cell_type": "markdown",
"id": "05d9729c-c2e7-4a4c-8d6a-59782ed9ff11",
"metadata": {},
"source": [
"## Rich Classes"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "f678313e-06c6-4fb8-a4ef-54a417129a82",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div style=\"width: 32px; height: 32px; background-color: #239814\" />"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class SuperColor {\n",
" constructor() {\n",
" this.color = \"#239814\"\n",
" }\n",
" hex() {\n",
" return this.color\n",
" }\n",
" \n",
" [Symbol.for(\"Jupyter.display\")]() {\n",
" return {\n",
" \"text/html\": `<div style=\"width: 32px; height: 32px; background-color: ${this.hex()}\" />`\n",
" }\n",
" }\n",
"}\n",
"\n",
"let sc = new SuperColor()\n",
"sc"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "c1296291-a3e8-457b-8329-6cc58a1e528a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div style=\"width: 32px; height: 32px; background-color: #239814\" />"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class SuperColorAsync {\n",
" constructor() {\n",
" this.color = \"#239814\"\n",
" }\n",
" hex() {\n",
" return this.color\n",
" }\n",
" \n",
" async [Symbol.for(\"Jupyter.display\")]() {\n",
" return {\n",
" \"text/html\": `<div style=\"width: 32px; height: 32px; background-color: ${this.hex()}\" />`\n",
" }\n",
" }\n",
"}\n",
"\n",
"let sc = new SuperColorAsync()\n",
"sc"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Deno",
"language": "typescript",
"name": "deno"
},
"language_info": {
"file_extension": ".ts",
"mimetype": "text/x.typescript",
"name": "typescript",
"nb_converter": "script",
"pygments_lexer": "typescript",
"version": "5.2.2"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 5
}