Jupyter 简明教程

Jupyter - Converting Notebooks

Jupyter 笔记本文件具有 .ipynb 扩展名。笔记本 app 会在网络浏览器中呈现笔记本。它可以通过使用文件菜单中下载作为选项导出到各种文件格式。Jupyter 还有 nbconvert 选项形式的命令行界面。默认情况下,nbconvert 将笔记本导出为 HTML 格式。您可以使用以下命令用于此目的 −

Jupyter notebook files have .ipynb extension. Notebook is rendered in web browser by the notebook app. It can be exported to various file formats by using download as an option in the file menu. Jupyter also has a command line interface in the form of nbconvert option. By default, nbconvert exports the notebook to HTML format. You can use the following command for tis purpose −

jupyter nbconvert mynotebook.ipynb

这会将 mynotebook.ipynb 转换为 mynotebook.html 。其他导出格式通过 --to 子句指定。

This will convert mynotebook.ipynb to the mynotebook.html. Other export format is specified with --to clause.

请注意,其他选项包括 ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides']

Note that other options include ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides']

HTML 包含 'basic' 和 'full' 模板。您可以按如下所示在命令行中指定 −

HTML includes 'basic' and 'full' templates. You can specify that in the command line as shown below −

jupyter nbconvert --to html --template basic mynotebook.ipynb

LaTex 是一种特别用于科学排版的文档编制格式。Jupyter 包含 'base', 'article' 和 'report' 模板。

LaTex is a document preparation format used specially in scientific typesetting. Jupyter includes 'base', 'article' and 'report' templates.

jupyter nbconvert --to latex –template report mynotebook.ipynb

要通过 latex 生成 PDF,请使用以下命令 −

To generate PDF via latex, use the following command −

jupyter nbconvert mynotebook.ipynb --to pdf

笔记本可以导出到 HTML 幻灯片放映。转换在后台使用 Reveal.js。要由 HTTP 服务器处理幻灯片,请在命令行中添加 --postserve。要生成不需要 Internet 连接的幻灯片,只需将 Reveal.js 库放在 your_talk.slides.html 所在的同一目录中。

Notebook can be exported to HTML slideshow. The conversion uses Reveal.js in the background. To serve the slides by an HTTP server, add --postserve on the command-line. To make slides that does not require an internet connection, just place the Reveal.js library in the same directory where your_talk.slides.html is located.

jupyter nbconvert myslides.ipynb --to slides --post serve

markdown 选项将笔记本转换为简单的 markdown 输出。Markdown 单元格不受影响,而代码单元格缩进了 4 个空格。

The markdown option converts notebook to simple markdown output. Markdown cells are unaffected, and code cells indented 4 spaces.

--to markdown

您可以使用 rst 选项将笔记本转换为 Basic reStructuredText 输出。它可以用作将笔记本嵌入到 Sphinx 文档中的起点。

You can use rst option to convert notebook to Basic reStructuredText output. It is useful as a starting point for embedding notebooks in Sphinx docs.

--to rst

这是从笔记本中获取 Python(或其他语言,取决于内核)脚本的最简单方法。

This is the simplest way to get a Python (or other language, depending on the kernel) script out of a notebook.

--to script