转载自: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. crontab Job权限重要

    在撰写JOb时,需要将SH目录及文件设为777,方可正常执行~

  2. 点餐系统Sprint1总结

    经过第一个阶段的冲刺,大家基本上都熟悉自己要做什么,并且把界面做了出来.通过这个阶段的冲刺,大家都学到了很多东西,团队更加默契,遇到不懂的都会提出来一起解决. 团队贡献分 148彭宏亮 20 150张 ...

  3. 16-阿里-intership

  4. TensorFlow支持windows了

    (留坑)找个时间测试一下. 终于来了,TensorFlow 新增官方 Windows 支持

  5. Openstack+Kubernetes+Docker微服务实践之路--RPC

    重点来了,本文全面阐述一下我们的RPC是怎么实现并如何使用的,跟Kubernetes和Openstack怎么结合.  在选型一文中说到我们选定的RPC框架是Apache Thrift,它的用法是在Ma ...

  6. 用CSS3和Canvas来画网格

    我们经常使用一些excel表格来处理数据,在html中,我们可以用table来制成表格.今天来看一下一些不同的方法. 方法一:使用CSS3的background的linear-gradient属性 l ...

  7. qt开源社区学习

    http://bbs.qter.org/forum.php?mod=forumdisplay&fid=52

  8. adb shell使用

    adb shell可以用来操纵数据库 1.在cmd界面 输入adb shell 进入adb shell 2.使用cd 切换到工作目录:使用ls查看文件 3.使用sqlite3+数据库名字 打开某个数据 ...

  9. java 重载规则

    首先看Java重载的规则: 1.必须具有不同的参数列表: 2.可以有不同的返回类型,只要参数列表不同就可以: 3.可以有不同的访问修饰符: 4.可以抛出不同的异常: 5.方法能够在一个类中或者在一个子 ...

  10. Security » Authorization » 通过映射限制身份

    Limiting identity by scheme¶ 通过映射限制身份(这部分有好几个概念还不清楚,翻译的有问题) 36 of 39 people found this helpful In so ...