Jupyter lab 或 ipython 中可以方便的书写 python 代码和 markdown 文档。同时,利用魔法命令可以书写 latex 文档、javascript 代码、js 代码、ruby 代码、bash 命令等。另外,魔法命令中还包含一些辅助工具(如 pruntimeitwritefileloadpy)等等。本篇对其进行简单介绍。

魔法命令

魔法命令(注意区别 Python 中的魔法函数)是由 ipython 提供的。查看所有魔法命令,可以在 jupyter lab 中使用如下魔法命令:

1
%lsmagic

所有魔法命令分为两类,一类是面向行的魔法命令,一类是面向单元格的魔法命令。分别对应 line 和 cell:

1
2
3
root:
line:
cell:

行命令是针对单行代码进行使用的,如:

1
2
3
4
%timeit sum((x*2 for x in range(100)))

# 输出结果
6.73 µs ± 70.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

单元格魔法命令是针对整个单元格(除了首行的魔法命令)进行使用的,如:

1
2
3
4
5
6
%%timeit
g = (x*2 for x in range(100))
sum(g)

# 输出结果
6.38 µs ± 32.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

魔法命令的使用

无论是行魔法命令还是单元格魔法命令,都有很多,如何使用它们呢?最好的方法是使用如下方法查看它们的使用说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
%timeit?

# 输出结果
Docstring:
Time execution of a Python statement or expression

Usage, in line mode:
%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
%%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
code
code...

Time execution of a Python statement or expression using the timeit
module. This function can be used both as a line and cell magic:

- In line mode you can time a single-line statement (though multiple
ones can be chained with using semicolons).

- In cell mode, the statement in the first line is used as setup code
(executed but not timed) and the body of the cell is timed. The cell
body has access to any variables created in the setup code.

Options:
-n<N>: execute the given statement <N> times in a loop. If <N> is not
provided, <N> is determined so as to get sufficient accuracy.

-r<R>: number of repeats <R>, each consisting of <N> loops, and take the
best result.
Default: 7

-t: use time.time to measure the time, which is the default on Unix.
This function measures wall time.

-c: use time.clock to measure the time, which is the default on
Windows and measures wall time. On Unix, resource.getrusage is used
instead and returns the CPU user time.

-p<P>: use a precision of <P> digits to display the timing result.
Default: 3

-q: Quiet, do not print result.

-o: return a TimeitResult that can be stored in a variable to inspect
the result in more details.

.. versionchanged:: 7.3
User variables are no longer expanded,
the magic line is always left unmodified.

Examples
--------
::

In [1]: %timeit pass
8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

In [2]: u = None

In [3]: %timeit u is None
29.9 ns ± 0.643 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [4]: %timeit -r 4 u == None

In [5]: import time

In [6]: %timeit -n1 time.sleep(2)


The times reported by %timeit will be slightly higher than those
reported by the timeit.py script when variables are accessed. This is
due to the fact that %timeit executes the statement in the namespace
of the shell, compared with timeit.py, which uses a single setup
statement to import function or create variables. Generally, the bias
does not matter as long as results from timeit.py are not mixed with
those from %timeit.
File: /usr/local/miniconda/lib/python3.9/site-packages/IPython/core/magics/execution.py
1
2
3
4
5
6
7
8
9
10
%%bash?

# 输出结果
Docstring:
%%bash script magic

Run cells with bash in a subprocess.

This is a shortcut for `%%script bash`
File: /usr/local/miniconda/lib/python3.9/site-packages/IPython/core/magics/script.py

参考链接

  1. Jupyter中的魔法命令
  2. Jupyter notebook魔法命令