Jupyter Notebook Tutorial
文章目录
title: Jupyter notebook 教程(高级) date: 2020-03-05 11:22:17
总结在 Jupyter notebook 使用中涉及到的比较有意思的特性(advanced tutorial,不涉及安装软件)。
- Shell Commands
|
|
Note that the shell in which ! commands are executed is discarded after execution completes, so commands like cd will have no effect. However, IPython magics offer a solution. ch 是不起作用的,因为是没有状态保留~
- Basic Magics
There are two categories of magic: line magics and cell magics.
使用以下命令显示所有的 magics
|
|
- Displaying Matplotlib Plots
|
|
Providing the inline argument instructs IPython to show Matplotlib plot images inline, within your cell outputs, enabling you to include charts inside your notebooks. Be sure to include this magic before you import Matplotlib, as it may not work if you do not; many import it at the start of their notebook, in the first code cell. 如果你想要画图(plot),那么一定要使用这个 magic 去输出图像
- 统计时间
|
|
- 对 latex 的支持
|
|
- Extensions (扩展包是非常有用的)
|
|
This will install the jupyter_contrib_nbextensions package in Python, install it in Jupyter, and then enable the spell check and code folding extensions.
具体的扩展包,是可以到网上找的。
- Enhancing Charts with Seaborn
主要的功能:实现更加好看。seaborn 实现效果更加好看
|
|
|
|
|
|
- Executing External Code
能够导入外部的项目或者包是非常重要的。
Jupyter also lets you %load
and %run
external scripts to support better organised, larger-scale projects and reusability.
We can load this simply by writing a one-line code cell, like so:
|
|
load 相当于 python 文件中的import, 导入文件中的函数,但并没有执行。
The %run
magic is similar, except it will execute the code and display any output, including Matplotlib plots. You can even execute entire notebooks this way, but remember that not all code truly belongs in a notebook. Let’s check out an example of this magic; consider a file containing the following short script.
run 命令是真正的运行。然后会将结果显示到 jupyter 的输出中(包括图像)。并且在jupyter 中是可以传参的,将一些参数可以加入到其中。类似命令行。
If you wish to pass arguments to a script, simply list them explicitly after the filename %run my_file.py 0 “Hello, World!” or using variables %run $filename {arg0} {arg1}. Additionally, use the -p option to run the code through the Python profiler.
- nbconvert
nbconvert: Convert Notebooks to other formats
|
|
- Running Jupyter Notebooks in Docker Containers
可以在docker 中使用 jupyter notebook
- jupyter notebook 的撤销功能
方法一 先按esc进入命令模式,即左侧线为蓝色(为绿色时是编辑模式),按z键即可恢复
方法二 如果是运行过的代码 直接运行
history 1 方法三 功能栏 edit -> undo delete cell
文章作者 jijeng
上次更新 2020-05-28