使用 Jupyter notebooks#

Note

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:

notebook

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”.

一些命令#

# 加 ! 转变为命令行命令
!ls
1.overview.ipynb 2.notebook.ipynb 3.markdown.ipynb hello.py
# 运行脚本
%run hello.py
Hello, Jupyter
# 监测代码运行时间
%timeit [x ** 2 for x in range(1000)] 
262 µs ± 4.88 µ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()
../_images/2.notebook_6_0.svg
# 设置 pandas 的输出
import pandas as pd
pd.set_option('display.max_rows', 1000)  # None 为不限制输出