deno.com
本页内容

Deno 的 Jupyter Kernel

Deno 内置了一个 Jupyter kernel,允许您编写 JavaScript 和 TypeScript;使用 Web 和 Deno API,并在交互式笔记本中直接导入 npm 包。

deno jupyter 始终以 --allow-all 运行

目前,Jupyter kernel 中执行的所有代码都以 --allow-all 标志运行。这是一个临时的限制,未来将会解决。

快速入门 跳转到标题

运行 deno jupyter --unstable 并按照说明操作。

您可以运行 deno jupyter --unstable --install 以强制安装 kernel。Deno 假设 jupyter 命令在您的 PATH 中可用。

完成安装过程后,Deno kernel 将在 JupyterLab 和经典笔记本的笔记本创建对话框中可用

Jupyter notebook kernel selection

您可以在任何支持 Jupyter 笔记本的编辑器中使用 Deno Jupyter kernel。

VS Code 跳转到标题

  • 安装 VSCode Jupyter 扩展
  • 通过打开命令面板 (Ctrl+Shift+P) 并选择 “创建:新建 Jupyter 笔记本” 来打开或创建一个笔记本文件。这也可以通过手动创建一个扩展名为 “.ipynb” 的文件来完成。
  • 当在新的或现有的笔记本上时,单击创建新的 Jupyter 笔记本,选择 “Jupyter kernels”,然后选择 Deno

Selecting Deno in VS Code

JetBrains IDE 跳转到标题

Jupyter 笔记本开箱即用。

富内容输出 跳转到标题

Deno.jupyter 命名空间提供了辅助函数,用于在您的笔记本中显示富内容 使用 Jupyter 支持的 MIME 类型


提供富内容输出的最简单方法是返回一个具有 [Symbol.for("Jupyter.display")] 方法的对象。

此方法应返回一个字典,将 MIME 类型映射到应显示的值。

{
  [Symbol.for("Jupyter.display")]() {
    return {
      // Plain text content
      "text/plain": "Hello world!",

      // HTML output
      "text/html": "<h1>Hello world!</h1>",
    }
  }
}

返回纯文本和 HTML 输出的对象的示例。

信息

您也可以使用 Deno.jupyter.$display 代替键入 Symbol.for("Jupyter.display")

这是一个常规函数,因此您可以使用任何您想要的库来格式化输出 - 例如,使用 @std/fmt/colors 来提供彩色输出

import * as colors from "jsr:@std/fmt/colors";

{
  [Deno.jupyter.$display]() {
    return {
      "text/plain": colors.green("Hello world"),
    }
  }
}

您还可以使用 Deno.jupyter.display 函数直接显示 MIME 包

await Deno.jupyter.display({
  "text/plain": "Hello, world!",
  "text/html": "<h1>Hello, world!</h1>",
  "text/markdown": "# Hello, world!",
}, { raw: true });

 API example

您的笔记本前端将根据其功能自动选择 “最丰富” 的 MIME 类型来显示。


Deno.jupyter 提供了几个辅助方法,用于富内容输出常见媒体类型。

Deno.jupyter.html 是一个标记模板,它将提供的字符串渲染为笔记本中的 HTML。

Deno.jupyter.html`<h1>Hello, world!</h1>
<h2>From Deno kernel</h2>
<p>Lorem ipsum <i>dolor</i> <b>sit</b> <u>amet</u></p>`;

 API example

Deno.jupyter.md 是一个标记模板,它将提供的字符串渲染为笔记本中的 Markdown 文档。

Deno.jupyter
  .md`# Notebooks in TypeScript via Deno ![Deno logo](https://github.com/denoland.png?size=32)

**Interactive compute with Jupyter _built into Deno_!**`;

 API example

Deno.jupyter.svg 是一个标记模板,它将提供的字符串渲染为笔记本中的 SVG 图形。

Deno.jupyter.svg`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
      <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
    </svg>`;

 API example

Deno.jupyter.image 是一个函数,它将渲染 JPG 或 PNG 图像。您可以传递文件路径,或已读取的字节

Deno.jupyter.image("./cat.jpg");

const data = Deno.readFileSync("./dog.png");
Deno.jupyter.image(data);

prompt 和 confirm API 跳转到标题

您可以使用 promptconfirm Web API 在您的笔记本中等待用户输入。

confirmprompt API 示例

IO pub 通道广播 跳转到标题

Deno.jupyter.broadcast 允许将消息发布到 IO pub 通道,从而在单元格评估时提供实时更新。

考虑这个示例,它在我们开始计算之前打印一条消息,并在计算完成时打印另一条消息

await Deno.jupyter.broadcast("display_data", {
  data: { "text/html": "<b>Processing...</b>" },
  metadata: {},
  transient: { display_id: "progress" },
});

// Pretend we're doing an expensive compute
await new Promise((resolve) => setTimeout(resolve, 1500));

await Deno.jupyter.broadcast("update_display_data", {
  data: { "text/html": "<b>Done</b>" },
  metadata: {},
  transient: { display_id: "progress" },
});

Deno.jupyter.broadcast API 示例

示例 跳转到标题

这是一个使用 @observablehq/plot 生成图表的示例

import { document, penguins } from "jsr:@ry/jupyter-helper";
import * as Plot from "npm:@observablehq/plot";

let p = await penguins();

Plot.plot({
  marks: [
    Plot.dot(p.toRecords(), {
      x: "culmen_depth_mm",
      y: "culmen_length_mm",
      fill: "species",
    }),
  ],
  document,
});

Example plot generated using  library

请参阅 https://github.com/rgbkrk/denotebooks 以获取更多高级示例,利用数据分析和可视化库,如 Polars、Observable 和 d3。

jupyter console 集成 跳转到标题

您还可以在 jupyter console REPL 中使用 Deno Jupyter kernel。为此,您应该使用 jupyter console --kernel deno 启动您的控制台。

Using the Deno kernel in a CLI

您找到您需要的内容了吗?

隐私政策