欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何优化Jupyter Notebook

程序员文章站 2022-04-21 11:57:38
...

介绍 (Introduction)

Jupyter Notebook is nowadays probably the most used environment for solving Machine Learning/Data Science tasks in Python.

如今,Jupyter Notebook可能是解决Python中的机器学习/数据科学任务的最常用的环境。

Jupyter Notebook is a client-server application used for running notebook documents in the browser. Notebook documents are documents able to contain both code and rich text elements such as paragraphs, equations, and so on.

Jupyter Notebook是一个客户端服务器应用程序,用于在浏览器中运行笔记本文档。 笔记本文档是能够同时包含代码和富文本元素(如段落,方程式等)的文档。

In this article, I will walk you through some simple tricks on how to improve your experience with Jupyter Notebook. We will start from useful shortcuts and we will end up adding themes, automatically generated table of contents, and more.

在本文中,我将引导您完成一些简单的技巧,以提高Jupyter Notebook的使用体验。 我们将从有用的快捷方式开始,最后添加主题,自动生成的目录等。

捷径 (Shortcuts)

Shortcuts can be really useful to speed up writing your code. I will now walk you through some of the shortcuts I found most useful to use in Jupyter.

快捷键对于加速编写代码非常有用。 现在,我将向您介绍一些我发现在Jupyter中最有用的快捷方式。

There are two possible way to interact with Jupyter Notebook: Command Mode and Edit Mode. Some shortcuts work only on one mode or another while others are common to both modes.

与Jupyter Notebook交互的方法有两种:命令模式和编辑模式。 有些快捷方式仅在一种模式或另一种模式下有效,而另一些快捷方式在两种模式下均通用。

Some shortcuts which are common in both modes are:

在这两种模式下通用的一些快捷方式是:

  • Ctrl + Enter: to run all the selected cells

    Ctrl + Enter :运行所有选定的单元格

  • Shift + Enter: run the current cell and move the next one

    Shift + Enter :运行当前单元格并移动下一个单元格

  • Ctrl + s: save notebook

    Ctrl + s :保存笔记本

In order to enter Jupyter command mode, we need to press Esc and then any of the following commands:

为了进入Jupyter命令模式,我们需要按Esc键,然后按以下任意命令:

  • H: show all the shortcuts available in Jupyter Notebook

    H :显示Jupyter Notebook中所有可用的快捷方式

  • Shift + Up/Down Arrow: to select multiple notebook cells at the same time (pressing enter after selecting multiple cells will make all of them run!)

    Shift +向上/向下箭头 :同时选择多个笔记本单元(选择多个单元后按Enter将使它们全部运行!)

  • A: insert a new cell above

    A :在上方插入一个新单元格

  • B: insert a new cell below

    B :在下面插入一个新单元格

  • X: cut the selected cells

    X :剪切选定的单元格

  • Z: undo the deletion of a cell

    Z :撤消删除单元格

  • Y: change the type of cell to Code

    Y :将单元格的类型更改为Code

  • M: change the type of cell to Markdown

    M :将单元格类型更改为Markdown

  • Space: scroll notebook down

    空间 :向下滚动笔记本

  • Shift + Space: scroll notebook up

    Shift +空格键 :向上滚动笔记本

In order to enter Jupyter edit mode instead, we need to press Enter and successively any of the following commands:

为了进入Jupyter编辑模式,我们需要依次按Enter和以下任何命令:

  • Tab: code competition suggestions

    标签 :代码竞争建议

  • Ctrl + ]: indent code

    Ctrl +] :缩进代码

  • Ctrl + [: dedent code

    Ctrl + [ :目标代码

  • Ctrl + z: undo

    Ctrl + z :撤消

  • Ctrl + y: redo

    Ctrl + y :重做

  • Ctrl + a: select all

    Ctrl + a :全选

  • Ctrl + Home: move cursor to cell start

    Ctrl + Home :将光标移到单元格开始

  • Ctrl + End: move cursor to the end of the cell

    Ctrl + End :将光标移到单元格的末尾

  • Ctrl + Left: move cursor one word left

    Ctrl +左 :将光标向左移动一个单词

  • Ctrl + Right: move cursor one word right

    Ctrl +右键 :将光标向右移动一个字

Shell命令和软件包安装 (Shell commands and Packages installation)

Not many users are aware of this, but it is possible to run shell commands in a Jupyter notebook cell by adding an exclamation mark at the beginning of the cell. For example, running a cell with !ls will return all the items in the current working directory. Running a cell with !pwd will instead print out the current directory file-path.

没有多少用户意识到这一点,但是可以通过在单元格的开头添加感叹号的方式在Jupyter笔记本单元格中运行Shell命令。 例如,使用!ls运行单元将返回当前工作目录中的所有项目。 使用!pwd运行单元将打印出当前目录的文件路径。

This same trick can also be applied to install Python packages in Jupyter notebook.

同样的技巧也可以应用于在Jupyter笔记本中安装Python软件包。

!pip install numpy

Jupyter主题 (Jupyter Themes)

If you are interested in changing how your Jupyter notebook looks, it is possible to install a package with a collection of different themes. The default Jupyter theme looks like the one in Figure 1. In Figure 2 you will see how we will be able to personalise its aspect.

如果您有兴趣更改Jupyter笔记本的外观,则可以安装具有不同主题集合的软件包。 默认的Jupyter主题如图1所示。在图2中,您将看到我们如何个性化其外观。

如何优化Jupyter Notebook
Figure 1: Default Jupyter Notebook Theme
图1:默认的Jupyter Notebook主题

We can install our package directly in the notebook using the trick I showed you in the previous section:

我们可以使用上一节中向您展示的技巧将软件包直接安装在笔记本中:

!pip install jupyterthemes

We can the run the following command to list the names of all the available themes:

我们可以运行以下命令来列出所有可用主题的名称:

!jt -l

# Cell output:
# Available Themes: 
#   chesterish
#   grade3
#   gruvboxd
#   gruvboxl
#   monokai
#   oceans16
#   onedork
#   solarizedd
#   solarizedl

Finally, we can choose a theme using the following command (in this example I decided to use the solarized1 theme):

最后,我们可以使用以下命令选择一个主题(在本示例中,我决定使用solarized1主题):

!jt -t solarizedl

Once we've run this command and refreshed the page, our notebook should look like the one in Figure 2.

运行此命令并刷新页面后,我们的笔记本应类似于图2中的笔记本。

如何优化Jupyter Notebook
Figure 2: Solarized1 notebook Theme
图2:Solarized1笔记本主题

In case you wish anytime to come back to the original Jupyter notebook theme, you can just run the following command and refresh your page.

如果您希望随时回到原始的Jupyter笔记本主题,则可以运行以下命令并刷新页面。

!jt -r

Jupyter笔记本扩展 (Jupyter Notebook Extensions)

Notebook extensions can be used to enhance the user experience and offer a wide variety of personalization techniques.

笔记本扩展可以用于增强用户体验并提供多种个性化技术。

In this example, I will be using the nbextensions library in order to install all the necessary widgets (this time I suggest you to first install the packages through terminal and then open the Jupyter notebook). This library makes use of different Javascript models in order to enrich the notebook frontend.

在此示例中,我将使用nbextensions库来安装所有必需的小部件(这一次,我建议您首先通过终端安装软件包,然后打开Jupyter笔记本)。 该库利用不同的Javascript模型来丰富笔记本前端。

! pip install jupyter_contrib_nbextensions
! jupyter contrib nbextension install --system

Once nbextensions is installed you will notice that there is an extra tab on your Jupyter notebook homepage (Figure 3).

安装nbextensions后 ,您会注意到Jupyter笔记本电脑主页上还有一个附加选项卡(图3)。

如何优化Jupyter Notebook
Figure 3: Adding nbextensions to Jupyter notebook
图3:向Jupyter Notebook添加nbextensions

By clicking on the Nbextensions tab, we will be provided with a list of available widgets. In my case, I decided to enable the ones shown in Figure 4.

通过单击Nbextensions选项卡,将为我们提供可用小部件的列表。 就我而言,我决定启用图4所示的选项。

如何优化Jupyter Notebook
nbextensions widgets optionsnbextensions小部件选项

Some of my favourite extensions are:

我最喜欢的一些扩展是:

  1. Table of Contents

    目录

Auto-generate a table of contents from markdown headings (Figure 5).

从markdown标题自动生成目录(图5)。

如何优化Jupyter Notebook
Figure 5: Table of Contents
图5:目录

2. Snippets

2. 片段

Sample codes to load common libraries and create sample plots which you can use as a starting point for your data analysis (Figure 6).

用于加载公共库并创建示例图的示例代码,您可以将它们用作数据分析的起点(图6)。

如何优化Jupyter Notebook
Figure 6: Snippets
图6:片段

3. Hinterland

3. 腹地

Code autocompletion for Jupyter Notebooks (Figure 7).

Jupyter Notebook的代码自动补全(图7)。

如何优化Jupyter Notebook
Figure 7: Code autocompletion
图7:代码自动补全

The nbextensions library provides many other extensions apart for these three, so I encourage you to experiment and test any other which can be of interest for you!

nbextensions库为这三个库分别提供了许多其他扩展,因此,我建议您尝试一下其他可能感兴趣的扩展!

降价选项 (Markdown Options)

By default, the last output in a Jupyter Notebook cell is the only one that gets printed. If instead we want to automatically print all the commands without having to use print(), we can add the following lines of code at the beginning of the notebook.

默认情况下,Jupyter Notebook单元中的最后一个输出是唯一打印的输出。 相反,如果我们想自动打印所有命令而不必使用print() ,则可以在笔记本的开头添加以下代码行。

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

Additionally, it is possible to write LaTex in a Markdown cell by enclosing the text between dollar signs ($).

此外,可以通过将文本括在美元符号($)之间来在Markdown单元中编写LaTex。

笔记本幻灯片 (Notebook Slides)

It is possible to create a slideshow presentation of a Jupyter Notebook by going to View -> Cell Toolbar -> Slideshow and then selecting the slides configuration for each cell in the notebook.

通过转到查看->单元格工具栏->幻灯片 ,然后为笔记本中每个单元格选择幻灯片配置,可以创建Jupyter笔记本的幻灯片演示文稿。

Finally, by going to the terminal and typing the following commands the slideshow will be created.

最后,通过转到终端并键入以下命令,将创建幻灯片。

pip install jupyter_contrib_nbextensions

# and successively:

jupyter nbconvert my_notebook_name.ipynb --to slides --post serve
如何优化Jupyter Notebook

魔法 (Magic)

Magics are commands which can be used to perform specific commands. Some examples are: inline plotting, printing the execution time of a cell, printing the memory consumption of running a cell, and so on.

魔术是可用于执行特定命令的命令。 一些示例是:内联绘图,打印单元格的执行时间,打印运行单元格的内存消耗等。

Magic commands which start with just one % apply their functionality just for one single line of a cell (where the command is placed). Magic commands which instead start with two %% are applied to the whole cell.

仅以开头的魔术命令仅将其功能应用于单元格的一行(放置命令的位置)。 反而以2 %%开头的魔术命令将应用于整个单元格。

It is possible to print out all the available magic commands by using the following command:

使用以下命令可以打印出所有可用的魔术命令:

%lsmagic

联络资料 (Contact info)

If you want to keep updated with my latest articles and projects follow me and subscribe to my mailing list. These are some of my contact details:

如果您想随时了解我的最新文章和项目,请关注我并订阅我的邮件列表 。 这些是我的一些联系方式:

翻译自: https://www.freecodecamp.org/news/optimize-your-jupyter-notebook/