Skip to content

Instantly share code, notes, and snippets.

@jtpio
Last active January 22, 2024 16:52
Show Gist options
  • Save jtpio/dc3ca4ff627e54f44ffbbdc491b3fd3e to your computer and use it in GitHub Desktop.
Save jtpio/dc3ca4ff627e54f44ffbbdc491b3fd3e to your computer and use it in GitHub Desktop.
Deno Jupyter kernel
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "a65dcd20-ff27-4b17-99bd-9c3a6cb6fea6",
"metadata": {},
"source": [
"# Deno 🦕\n",
"\n",
"* Next-generation JavaScript runtime\n",
"* Secure by default\n",
"* Native support for TypeScript and JSX\n",
"* Testing, linting, formatting, and more out of the box\n",
"* High performance async I/O with Rust and Tokio\n",
"* Backwards compatible with Node.js and npm\n",
"\n",
"## Notebook friendliness\n",
"\n",
"* ESM imports very friendlty\n",
"* Top level async await\n",
"\n",
"\n",
"![](https://examples.deno.land/logo.png?__frsh_c=7d22d8d9deb32596c0a3a3241c977bad4977ad0f)\n",
"\n",
"## Deno by Example\n",
"\n",
"\n",
"Deno programs can either be written in JavaScript or TypeScript, or a mixture of both. All code in these examples is written in TypeScript, but all the examples also work in JavaScript."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bc40f7f6-3a79-4e4b-912c-44f748725cef",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to Deno!\n"
]
}
],
"source": [
"console.log(\"Welcome to Deno!\");\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b284b60f-f238-4210-86f8-969a4a978c8f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[48;2;21;128;61m\u001b[38;2;255;255;255m Hello Deno \u001b[0m\n"
]
}
],
"source": [
"console.log(\"%c Hello Deno \", \"background-color: #15803d; color: #fff\");\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7de95151-b7e1-412c-82ac-eeee20382c85",
"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]);\n"
]
},
{
"cell_type": "markdown",
"id": "7056de78-4c0a-4892-9a06-d05d4f3a19a9",
"metadata": {},
"source": [
"## Imports\n",
"\n",
"You can import relative files like this:\n",
"\n",
"```typescript\n",
"import { sayHello } from \"./util.ts\";\n",
"sayHello(\"World\");\n",
"import * as util from \"./util.ts\";\n",
"util.sayHello(\"World\");\n",
"```\n",
"\n",
"Imports don't have to be relative, they can also reference absolute file or https URLs.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "96ac4b42-12fb-410e-9119-10582046ee35",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.201.0\n"
]
}
],
"source": [
"import { VERSION } from \"https://deno.land/std/version.ts\";\n",
"console.log(VERSION);\n"
]
},
{
"cell_type": "markdown",
"id": "5397e8b0-8ae5-4989-aee0-c3ff3ed51024",
"metadata": {},
"source": [
"## Use Node.js built-in modules\n",
"\n",
"Deno supports most built-in Node.js modules natively - you can include them in your code using \"node:\" specifiers in your imports.\n"
]
},
{
"cell_type": "markdown",
"id": "4cefbf7a-b8de-454a-902c-eedac7310caa",
"metadata": {},
"source": [
"Import the `os` module from core Node to get operating system info.\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "17bdc71f-d597-4ac6-b2fa-a7687e91c406",
"metadata": {},
"outputs": [],
"source": [
"import os from \"node:os\";\n"
]
},
{
"cell_type": "markdown",
"id": "ce34fe0c-02c9-4bf2-ba49-90ffa3e24f8f",
"metadata": {},
"source": [
"Use the module as you would in Node.js\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "485e44c9-b0d7-4130-bd79-3867ad12c74f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current architecture is: arm64\n",
"Home directory is: /Users/kai\n"
]
}
],
"source": [
"console.log(\"Current architecture is:\", os.arch());\n",
"console.log(\"Home directory is:\", os.homedir());\n"
]
},
{
"cell_type": "markdown",
"id": "76200581-9ef8-4ba4-b8c9-eb9f88399f98",
"metadata": {},
"source": [
"## Import modules from npm\n",
"\n",
"Use JavaScript modules from npm in your Deno programs with the \"npm:\" specifier in your imports.\n"
]
},
{
"cell_type": "markdown",
"id": "2892b068-44af-4a66-8b66-e16a5d61f04e",
"metadata": {},
"source": [
"Import the `moment` module from npm using an npm: prefix, and appending a version number. Dependencies from npm can be configured in an import map as well.\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "642b6290-7abd-4517-9f7b-3e65c8761a9b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[32m\"2.29.4\"\u001b[39m"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import moment from \"npm:moment@2.29.4\";\n",
"\n",
"moment.version;\n"
]
},
{
"cell_type": "markdown",
"id": "7c6926e3-a1c3-4bb5-aa1f-9d682f73d9f2",
"metadata": {},
"source": [
"Get the current Date and Time from `moment`\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "217df67f-ba25-4324-9920-9b3308cf2b38",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current Date and Time: Wed Dec 13 2023 09:02:37 GMT-0800\n"
]
}
],
"source": [
"const now = moment();\n",
"console.log(\"Current Date and Time:\", now.toString());\n"
]
},
{
"cell_type": "markdown",
"id": "6ae56701-42d6-4fc5-9232-ea05d230e68e",
"metadata": {},
"source": [
"Add and subtract time\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "83726faf-9524-418b-af4b-b6e930202d56",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7 days from now: Wed Dec 20 2023 09:02:37 GMT-0800\n",
"7 days ago: Wed Dec 06 2023 09:02:37 GMT-0800\n"
]
}
],
"source": [
"const future = moment().add(7, \"days\");\n",
"console.log(\"7 days from now:\", future.toString());\n",
"\n",
"const past = moment().subtract(7, \"days\");\n",
"console.log(\"7 days ago:\", past.toString());\n"
]
},
{
"cell_type": "markdown",
"id": "b311587f-602b-4ff1-8714-8ac054f066ac",
"metadata": {},
"source": [
"## Built-in TypeScript support\n",
"\n",
"Deno natively understands TypeScript code with no compiler to configure. Start writing code in .ts files, and the runtime will work with them just fine.\n"
]
},
{
"cell_type": "markdown",
"id": "70e2ef45-502d-427d-afc0-dfaab0664304",
"metadata": {},
"source": [
"Define an interface in TypeScript\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b6e825f5-2c06-45c1-96a6-8ad7447939df",
"metadata": {},
"outputs": [],
"source": [
"interface Person {\n",
" name: string;\n",
" age: number;\n",
"}\n"
]
},
{
"cell_type": "markdown",
"id": "9a46b58e-3761-4cdd-af6e-a4d400708037",
"metadata": {},
"source": [
"Provide a typed input to a function\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "6a17306d-0d76-4008-8e19-fe2e14aadde3",
"metadata": {},
"outputs": [],
"source": [
"function greet(person: Person) {\n",
" return \"Hello, \" + person.name + \"!\";\n",
"}\n"
]
},
{
"cell_type": "markdown",
"id": "f5b2dd1a-d5c2-40d2-aabf-234d121ddf3e",
"metadata": {},
"source": [
"Everything works with zero config!\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "6746660f-1dae-4b34-9527-b75a4400bc20",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, Alice!\n"
]
}
],
"source": [
"console.log(greet({ name: \"Alice\", age: 36 }));\n"
]
},
{
"cell_type": "markdown",
"id": "cbab8c15",
"metadata": {},
"source": [
"## Manipulating & Parsing URLs\n",
"\n",
"`URL` is the web standard interface to parse and manipulate URLs.\n"
]
},
{
"cell_type": "markdown",
"id": "26030a3d",
"metadata": {},
"source": [
"We can create a new object in a variety of ways In the most simple case we can simply just write the whole url\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "bb4b14b3",
"metadata": {},
"outputs": [],
"source": [
"let url = new URL(\"https://deno.land/manual/introduction\");\n"
]
},
{
"cell_type": "markdown",
"id": "99dac17e",
"metadata": {},
"source": [
"Alternatively we are able to pass a (relative) url which will be automatically resolved to an absolute url\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "65cc81ac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"URL {\n",
" href: \u001b[32m\"https://deno.land/manual/introduction\"\u001b[39m,\n",
" origin: \u001b[32m\"https://deno.land\"\u001b[39m,\n",
" protocol: \u001b[32m\"https:\"\u001b[39m,\n",
" username: \u001b[32m\"\"\u001b[39m,\n",
" password: \u001b[32m\"\"\u001b[39m,\n",
" host: \u001b[32m\"deno.land\"\u001b[39m,\n",
" hostname: \u001b[32m\"deno.land\"\u001b[39m,\n",
" port: \u001b[32m\"\"\u001b[39m,\n",
" pathname: \u001b[32m\"/manual/introduction\"\u001b[39m,\n",
" hash: \u001b[32m\"\"\u001b[39m,\n",
" search: \u001b[32m\"\"\u001b[39m\n",
"}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"url = new URL(\"/manual/introduction\", \"https://deno.land\");\n"
]
}
],
"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"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

Deno Jupyter kernel demo

Try it on Binder!

Binder

name: jupyter-deno
channels:
- conda-forge
dependencies:
- deno >=1.39.4,<2
- notebook >=7,<8
deno jupyter --unstable --install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment