10.3. JupyterLab 概述#

JupyterLab is the next-generation web-based user interface for Project Jupyter.

JupyterLab enables you to work with documents and activities such as Jupyter notebooks, text editors, and terminals in a flexible, integrated, and extensible manner.

10.3.1. JupyterLab 的功能#

  • Jupyter notebooks (.ipynb files) are fully supported in JupyterLab(核心功能).

  • Use Code consoles.

  • Use Terminals.

  • Multiple views of documents with different editors or viewers(分屏).

  • JupyterLab offers a unified model for viewing and handling data formats.

那为什么要使用 JupyterLab 呢?Personally:

  1. 确实很适合做数据科学。

  2. 别人也在用 Jupyter notebooks(或者类似的 Google colab),比如说某些 GitHub 项目、很多 Coursera 课程的作业、 《Dive into Deep Learning》每个页面都是一个 notebook、去 Kaggle 参加比赛需要自己写 notebook 等。

  3. 用来写 Markdown 文档。

  4. 用来写 Jupyter Book(我们将在第二部分做详细介绍)。

10.3.2. 安装-jupyterlab#

通过 pip 安装:

pip install jupyterlab

通过 conda 安装:

conda install -c conda-forge jupyterlab

启动-jupyterlab

Once installed, launch JupyterLab with:

jupyter lab

JupyterLab will open automatically in your browser(本地运行的 JupyterLab 的 url 一般是 http://localhost:8888/lab):

{image} ../images/lab1.jpg :alt: launch :class: bg-primary mb-1 :width: 700px

You may also access JupyterLab by entering the notebook server’s URL into the browser:

http(s)://<server:port>/<lab-location>/lab

10.3.3. jupyter-界面#

备注

While JupyterLab has many features found in traditional IDEs, it remains focused on interactive, exploratory computing. The JupyterLab interface consists of a main work area containing tabs of documents and activities, a collapsible left sidebar, and a menu bar.

Left Sidebar 从上到下依次是:

  • a file browser,

  • a list of tabs in the main work and of running kernels and terminals,

  • the table of contents,

  • the extension manager.

Menu Bar 和 Main Work Area functioning as you expected.

10.3.4. 使用-jupyter-notebooks#

Jupyter notebooks(.ipynb 文件) are documents that combine live runnable code with narrative text (Markdown), equations (LaTeX), images, interactive visualizations and other rich output.

Create a notebook by clicking the + button in the file browser and then selecting a kernel in the new Launcher tab:

{image} ../images/lab2.jpg :alt: notebook :class: bg-primary mb-1 :width: 700px

Jupyter notebooks 支持三类 cell: Code, Markdown and Raw.

小技巧

  • 可以 Drag and drop cells to rearrange your notebook.

  • 可以先分屏,再 Drag cells between notebooks to quickly copy content.

  • 可以在文件标题处右键选择 New View for Notebook 分屏,Create multiple synchronized views of a single notebook.

  • 输出太长了,可以 Collapse and expand code and output using the View menu or the blue collapser button on left of each cell.

  • 输出太长了,可以 Enable scrolling for long outputs by right-clicking on a cell and selecting “Enable Scrolling for Outputs”.

10.3.5. 一些命令#

# 加 ! 转变为命令行命令
!ls
   1.intro.ipynb    2.notebook.ipynb hello.py
# 运行脚本
%run hello.py
   Hello, Jupyter
# 监测代码运行时间
%timeit [x ** 2 for x in range(1000)]
   255 µs ± 7.35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
# 优雅地使用 matplotlib
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

plt.plot([x ** 2 for x in range(1000)])
plt.show()

# 设置 pandas 的输出
import pandas as pd
pd.set_option('display.max_rows', 1000)  # None 为不限制输出