转载自:https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

Jupyter notebook, formerly known as the IPython notebook, is a flexible tool that helps you create readable analyses, as you can keep code, images, comments, formulae and plots together.

Jupyter Notebook, 集代码、图像、注释、公式、图表为一体的神器。

Jupyter名字的起源 :the name Jupyter is an indirect acronyum of the three core languages it was designed for: JUlia, PYThon, and R and is inspired by the planet Jupiter.

1. Keyboard Shortcuts  快捷键

怎么查看说明,打开一个notebook,Help > Keyboard Shortcuts

有命令模式的快捷键:

也有编辑模式的快捷键:

几个常用的:

1. Esc + F  :Find and replace on your code but not the outputs.

查找并替换代码(而不是输出)

2. 对于cell的一些操作

Esc + O  折叠cell输出;

Shift + J or Shift + Down:向下选中多个cell,反之亦然

选中多个cell有什么用呢?删除修改复制等不用说,可以合并呀,哈哈

Shift + M: 合并多个cell

2. Pretty Display of Variables   美化输出

主要把数据以更好的形式输出,方便观察,先来个普通的:

虽然能看,但是……也还行吧,能更好就尽量更好,那么

是不是觉得很赞,但是每次还要import interactiveShell,有点麻烦哈

没关系,在~/.ipython/profile_default/ipython_config.py  里添加:

c = get_config()

# Run all nodes interactively
c.InteractiveShell.ast_node_interactivity = "all"

我测试的时候好像有点问题,第一句话其实是不用的,但是第二句话删掉后貌似还可以,有点奇怪。

3. Easy links to documentation    查看文档的一些便捷方式

1. Help菜单

2. ?操作符,?后面跟着想查询的对象就可以啦,其实就是调用了docstring。

4. Plotting in notebooks 绘图

一般都是用matplotlib绘图,就一条命令:

%matplotlib inline

另外,据说%matplotlib notebook 可已创建可交互图,但是非常慢,可是我都没成功。作为替代,bokeh包可以尝试下。土豪也可以用plot.ly,能做出更漂亮的图表,但是要付费。mpld3 也是一个不错的替代。

5. Jupyter Magic Commands   神奇的命令符 %

说实话我真不知道这个用处到底多大,用%lsmagic 查看  

有缘人请查看文档:http://ipython.readthedocs.io/en/stable/interactive/magics.html

后面介绍几个所谓的%绝技

6. Jupyter Magic - %env: Set Environment Variables   定义环境变量

定义环境变量,而且不用重新启动notebook,一些库,比如theano,会用到环境变量

可以用%env  查看环境变量

也可以这样设置:%env OMP_NUM_THREADS=4

话说这个还是挺给力的。

7. Jupyter Magic - %run: Execute python code   直接运行文件

命令:%run

用途:可以直接运行.py 文件,或者 notebook的.ipynb文件

没什么说的,提高了集成性,有用

8. Jupyter Magic - %load: Insert the code from an external script

将python文件插入到cell中,%load E:/test.py

但是要注意,test.py中的内容会把cell里面的东西覆盖掉

所以,这个类似于一个快捷的复制粘贴吧,当然,也可以load一个URL,哈哈

9. Jupyter Magic - %store: Pass variables between notebooks. 在notebook之间共享变量

如题

两个命令: %store 变量

%store -r 变量

 10. Jupyter Magic - %who: List all variables of global scope.  列出所有的全局变量

如题,也可以根据类型筛选,如:%who str

 11. Jupyter Magic - Timing   计时

这个功能很有用,尤其是分析一段代码的性能时,有两种方式:

1. %%time  记录一个cell中代码的运行时间  

2. %%timeit  调用python的timeit模块,运行10万次,给出最好的三次运行时间的分析结果。

12. Jupyter Magic - %%writefile and %pycat: Export the contents of a cell/Show the contents of an external script

代码的保存和查看

notebook一般是用来试验,一段代码写好,测试没问题了,不用复制粘贴出去,使用命令:%%writefile filename.py 保存。

反过来,有之前的%load, %run 命令,把已经有的代码载入,或者运行,但是有些代码只想看看,就可以用命令:%pycat filename.py 查看。

13. Jupyter Magic - %prun: Show how much time your program spent in each function.  分析一条命令所涉及的函数,及总的运行时间

命令: %prun fun()

14. Jupyter Magic - Debugging with %pdb  调试

命令: %pdb

然而,这个pdb的调试我不会用……

文档:https://docs.python.org/3.5/library/pdb.html#debugger-commands  

友情提示,调试模式下,

15. Suppress the output of a final function.    简化输出  

就是一个分号;,作用就是不显示变量,这里变量是重点,先看官方的例子,是一个很好的使用场景

然后继续深入研究下

一目了然吧,就是不显示变量,一个分号;搞定。

16. Executing Shell Commands  运行Shell命令

一个叹号!加上命令,就可以把notebook变成cmd黑框框,也是很方便啦

试了下,不能用powershell,算是一个小辅助吧。

17. Using LaTeX for forumlas  使用LaTex编写公式

看上去很方便,然而我没用过,有缘人自己挖掘吧

18. Run code from a different kernel in a notebook 在notebook里运行其他语言

在一个notebook里运行多种语言,这个也是很给力了,至少python2和python3切换是很方便了,其他的还有bash, HTML, ruby, perl。只需要加两个百分号,比如:%%python2。

19. Install other kernels for Jupyter 为jupyter notebook添加其他语言的kernel

好啦,用%%可以运行其他语言,但是也要先安装啊,两种安装方法

1. easy模式,前提是安装了Anaconda

cmd窗口运行conda install -c r r-essentials 即可安装

2. 手动安装

还是以R为例,先要自己安装R

然后,在R的console中运行:

install.packages(c('repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'devtools'))
devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec()  # to register the kernel in the current R installation

所以还是安装Anacoda吧……

20. Running R and Python in the same notebook. 在同一个Notebook中运行python 和R

之前提到,用%%可以在Notebook中运行不同的语言

针对Python和R,貌似有更好的解决方案,即安装rpy2,当前这之前还是要装好R的

!pip install rpy2

这样,据说可以在两种语言之间共享变量,看上去很厉害的样子,但是我还没试过

看图,貌似R绘制的图更加漂亮呢……

21. Writing functions in other languages  使用其他语言编写函数

用于numpy性能不够时,需要自己编写性能更好,更快的函数,那么也就不能指望python了,需要搬出祖宗级的cython或者fortran了,编写完直接用python调用。

当然也是要先安装相关的东西

!pip install cython fortran-magic 

然后就可以开干了

cython

fortran

扩展阅读:

using fortran from python

http://arogozhnikov.github.io/2015/11/29/using-fortran-from-python.html

python代码加速的一些东西

http://arogozhnikov.github.io/2015/09/08/SpeedBenchmarks.html

22. Multicursor support  编辑多行

按住Alt,选择多行,每行就都会有光标,同时编辑

23. Jupyter-contrib extensions 一个小宝库

比如 jupyter spell-checker ,code-formatter

可以用命令安装:

!pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master
!pip install jupyter_nbextensions_configurator
!jupyter contrib nbextension install --user
!jupyter nbextensions_configurator enable --user

也可以在Notebook中安装

24. Create a presentation from a Jupyter notebook.  notebook 秒变PPT

安装RISE

conda install -c damianavila82 rise

或者  pip install RISE

然后启动这个东西:

jupyter-nbextension install rise --py --sys-prefix
jupyter-nbextension enable rise --py --sys-prefix

怎么用呢,以后再说。

25. The Jupyter output system

Notebook是使i用HTML展示的,cell的输出也是HTML,这样基于浏览器的话,那就可以输出视频,音频,图片了

当然首先还是需要Notebook支持的:

from IPython.display import display, Image

还有一个悲惨的播放视频失败案例:

融会贯通一下,也可以用bash命令获得文件列表

26. ‘Big data’ analysis  大数据分析

python大数据处理的一些工具

ipyparallel (formerly ipython cluster)

pyspark

spark-sql magic %%sql

27. Sharing notebooks  共享notebook

最好的方式使用.ipynb,对于没有使用Jupyter的人来说,也有一些方案:

1. 导出HTML文件,File > Download as > HTML

2. 导出PDF文件,File > Download as > PDF,参考Making publication ready Python notebooks.

3. 将Notebook发布在gists 或者 github。

4. 建立自己的体系:jupyterhub,可以用来讲课或者多人协作。

5. nbviewer,将ipynb文件存到网盘,这个网站可以输入URL,呈现notebook。http://nbviewer.jupyter.org/

6. 建立blog:Create a blog using Pelican from your Jupyter notebooks.

大功告成!

未经允许,请不要转载!

未经允许,请不要转载!

未经允许,请不要转载!

Jupyter Notebook 27绝技——27 Jupyter Notebook tips, tricks and shortcuts的更多相关文章

  1. 45 Useful JavaScript Tips, Tricks and Best Practices(有用的JavaScript技巧,技巧和最佳实践)

    As you know, JavaScript is the number one programming language in the world, the language of the web ...

  2. 转:45 Useful JavaScript Tips, Tricks and Best Practices

    原文来自于:http://flippinawesome.org/2013/12/23/45-useful-javascript-tips-tricks-and-best-practices/ 1 – ...

  3. 45 Useful JavaScript Tips, Tricks and Best Practices

    <45 Useful JavaScript Tips, Tricks and Best Practices> http://flippinawesome.org/2013/12/23/45 ...

  4. 【翻译】C# Tips & Tricks: Weak References - When and How to Use Them

    原文:C# Tips & Tricks: Weak References - When and How to Use Them Sometimes you have an object whi ...

  5. Must Know Tips/Tricks in Deep Neural Networks

    Must Know Tips/Tricks in Deep Neural Networks (by Xiu-Shen Wei)   Deep Neural Networks, especially C ...

  6. Productivity tips, tricks and hacks for academics (2015 edition)

    Productivity tips, tricks and hacks for academics (2015 edition) Contents Jump to: My philosophy: Op ...

  7. tips for using shortcuts

    tips for using shortcuts for mac: command+ctrl+F:full screen(当前应用全屏之后有一个好处 就是 使用 4 tap 的手势 可以在全屏的界面之 ...

  8. Jupyter notebook工具栏隐藏和jupyter notebook主题更改

    有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python  https://www.cnblogs.com/xxtalhr/p/10747200.html 一.问题 Jupyt ...

  9. 运行jupyter notebook 出错 Error executing Jupyter command 'notebook'

    实际上是安装jupyter时候有错误, 仔细看日志发现需要缺少 Microsoft Visual C++ Compiler for Python 2.7 下载安装后,重新安装jupyter即可 htt ...

随机推荐

  1. 读《编写可维护的JavaScript》第九、十章总结

    第九章 将配置数据从代码中分离出来 9.2 抽离配置数据 这章比较好理解,也非常常见,作者给的俩个例子就能说明一切: // 将配置数据藏在代码中 function validate(value) { ...

  2. 新一代Ajax API --fetch

    之前 师傅跟我提过 一个新的Ajax API  fetch 今天看到一篇关于fetch的文章,受益匪浅. XMLHttpRequest并不是专为Ajax而设计的,虽然各种框架对XHR的封装已经足够好用 ...

  3. an interview question(1)

    声明:本文为博主原创文章,未经博主允许不得转载. 以下是英文翻译: warnning: Copyright!you can't reprint this blog when you not get b ...

  4. Nginx

    Nginx的常识 1.nginx三大功能:(百万级别繁忙服务器占有率) 1)web服务器 轻量级,处理的并发量更大. 2)反向代理服务器 通过将用户端的请求,透明的转送给应用服务器.这样所 有的客户端 ...

  5. 金融IC卡 ARQC和ARPC计算方法和实例(转)

    假设发卡行的IC卡应用密钥如下:   PBOC_IMK_AC               F0C34A8124CEE0A91A0B034AA97D6EAC PBOC_IMK_ENC           ...

  6. python操作csv和excel文件

    1.操作csv文件 1).读取文件 import csv f=open("test.csv",'r') t_text=csv.reader(f) for t,i in t_text ...

  7. iOS开发 iOS9横屏后状态栏隐藏处理

    - (BOOL)prefersStatusBarHidden { return NO; }

  8. linux建立一个快捷方式,连接到另一个目录

    sudo ln -s 源目录 目标快捷方式比如你要在/etc下面建立一个叫LXBC553的快捷方式,指向/home/LXBC,那就是sudo ln -s /home/LXBC /etc/LXBC553

  9. 真核转录组(denovo/resequencing)及案例分析

    参考: 转录组文章的常规套路 文章解读:<Science>小麦转录组研究文章 转录组数据饱和度评估方法 Paper这个东西是多么的诱人,可以毕业,可以评职称,可以拿绩效. 现在的文章都是有 ...

  10. [分享] IT天空的二十二条军规

    Una 发表于 2014-9-19 20:25:06 https://www.itsk.com/thread-335975-1-1.html IT天空的二十二条军规 第一条.你不是什么都会,也不是什么 ...