你真的会用Jupyter吗?这里有7个进阶功能助你效率翻倍

原作 Parul Pandey 
郭一璞 编译 
量子位 出品 | 公众号 QbitAI

作为一个超好用的交互式笔记本,Jupyter总是深受开发者的喜爱。

不过,除了基础的写文档之外,其实Jupyter还有N多功能,简直是一个集视频、图片、PPT、多种交互于一身的万花筒。

如果不会用,你可能错过了Jupyter 99%的功能。

Medium上走向数据科学(Towards Data Science)专栏的作者Parul Pandey就总结了七大Jupyter的进阶用法,量子位编译如下~

执行shell命令

Shell是一种与计算机进行文本交互的方式。

一般来讲,当你正在使用Python编译器,需要用到命令行工具的时候,要在shell和IDLE之间进行切换。

但是,如果你用的是Jupyter,就完全不用这么麻烦了,你可以直接在命令之前放一个“!”,就能执行shell命令,完全不用切换来切换去,就能在IPython里执行任何命令行。

1In [1]: !ls
2example.jpeg list tmp
3In [2]: !pwd
4/home/Parul/Desktop/Hello World Folder 
5In [3]: !echo "Hello World"
6Hello World
7
1In [4]: files= !ls
2In [5]: print(files)
3[ example.jpeg ,  list ,  tmp ]
4In [6]: directory = !pwd
5In [7]: print(directory)
6[ /Users/Parul/Desktop/Hello World Folder ]
7In [8]: type(directory)
8IPython.utils.text.SList

给Jupyter换主题

使用Jupyter主题,不仅能让你的Jupyter界面更好看、更舒服,还能让屏幕上的代码看起来更显眼。

比如Chesterish主题:

solarizedl主题:

来看一下具体的操作方法:

第一步,安装:

1pip install jupyterthemes
1jt -l
1# selecting a particular theme
2jt -t <name of the theme>
3# reverting to original Theme
4jt -r

chesterish
grade3
gruvboxd
gruvboxl monokai
oceans16
onedork
solarizedd
solarizedl

把名字填进去就好啦。

不过,每次换主题的时候都要重新加载Jupyter,才能看到主题变化。

另外我们上面讲的在命令之前加“!”也一样可用。

笔记本扩展

笔记本扩展(nbextensions)是一种JavaScript模块,可以加载到笔记本前端页面上,可以大大提升用户体验。

比如下面这些扩展工具,简直能让效率提升10000倍。

Hinterland

Hinterland功能可以让你每敲完一个键,就出现下拉菜单,可以直接选中你需要的词汇。

Snippets

Snippets在工具栏里加了一个下拉菜单,可以非常方便的直接插入代码段,完全不用手动敲。

拆分单元格

拆分笔记本中的单元格,改成相邻的模式,看起来就像分了两栏。

目录

这个功能可以自动找到所有的标题,生成目录。

并且这个目录还是移动的呦,你可以放在侧边栏,也可以拖动到任何你喜欢的地方悬浮起来。

折叠一个标题下的全部内容

如果你的代码太长,觉得滚动过去太麻烦,可以直接折叠掉。

Autopep8

一键美化代码,强迫症的福音。

安装方法

最后看一下怎么装,需要用到conda:

1conda install -c conda-forge jupyter_nbextensions_configurator
1pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install
2#incase you get permission errors on MacOS,
3pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install --user

然后把Jupyter打开,你就可以看到NBextensions这个选项卡了。

找不到的话就去菜单的Edit里面找。

搞定。

Jupyter小工具

还有一些Jupyter小工具,比如滑块、文本框之类的部分,可以做一些方便的交互。

滑块

1def f(x):
2    return x
3# Generate a slider 
4interact(f, x=10,);

1# Booleans generate check-boxes
2interact(f, x=True);

1# Strings generate text areas
2interact(f, x= Hi there! );

 1play = widgets.Play(
 2    # interval=10,
 3    value=50,
 4    min=0,
 5    max=100,
 6    step=1,
 7    description="Press play",
 8    disabled=False
 9)
10slider = widgets.IntSlider()
11widgets.jslink((play,  value ), (slider,  value ))
12widgets.HBox([play, slider])

1widgets.DatePicker(
2    description= Pick a Date ,
3    disabled=False
4)

调色盘

1widgets.ColorPicker(
2    concise=False,
3    description= Pick a color ,
4    value= blue ,
5    disabled=False
6)

1tab_contents = [ P0 ,  P1 ,  P2 ,  P3 ,  P4 ]
2children = [widgets.Text(description=name) for name in tab_contents]
3tab = widgets.Tab()
4tab.children = children
5for i in range(len(children)):
6    tab.set_title(i, str(i))
7tab

安装方法

1# pip
2pip install ipywidgets
3jupyter nbextension enable --py widgetsnbextension
4# Conda
5conda install -c conda-forge ipywidgets
6#Installing ipywidgets with conda automatically enables the extension
1# Start with some imports!
2from ipywidgets import interact
3import ipywidgets as widgets

Qgrid也是一个Jupyter的小部件,不过它主要用于数据帧,装上之后,就可以像操作Excel里的筛选功能一样,方便的处理数据。

安装方法

用pip安装:

1pip install qgrid
2jupyter nbextension enable --py --sys-prefix qgrid
3# only required if you have not enabled the ipywidgets nbextension yet
4jupyter nbextension enable --py --sys-prefix widgetsnbextension
1# only required if you have not added conda-forge to your channels yet
2conda config --add channels conda-forge
3conda install qgrid

人在江湖飘,难免遇到要做PPT的时候。

但是,如果你的内容都已经放在Jupyter里了,再重新导入PPT,太麻烦了,我们自动转换吧。

一种方法是用Jupyter内置的PPT选项,依次点击菜单栏里的View → Cell Toolbar → Slideshow,之后每个单元格上面都会有一个灰色的横条,选Sub-Slide

然后输入以下代码:

1jupyter nbconvert *.ipynb --to slides --post serve
2# insert your notebook name instead of *.ipynb

你也可以提前换好主题,比如onedork主题,然后再转换成PPT的的时候就是同一个主题风格的PPT了。

 

不过,用PyTorch默认方法生成的PPT代码不能编辑,这个时候就要用到RISE插件了。

RISE用到reveal.js来运行PPT,可以不退出PPT,直接运行代码。

先来安装RISE,推荐用conda:

1conda install -c damianavila82 rise
1pip install RISE
1jupyter-nbextension install rise --py --sys-prefix
2#enable the nbextension:
3jupyter-nbextension enable rise --py --sys-prefix

戳一下,一个可以边演示边改的PPT就好了。

嵌入链接和pdf

扔链接再跳转实在是太麻烦了,不如直接把素材放进Jupyter里面。

嵌入链接

1#Note that http urls will not be displayed. Only https are allowed inside the Iframe
2from IPython.display import IFrame
3IFrame( https://en.wikipedia.org/wiki/HTTPS , width=800, height=450)

嵌入pdf

1from IPython.display import IFrame
2IFrame( https://arxiv.org/pdf/1406.2661.pdf , width=800, height=450)

 

传送门

原文:Bringing the best out of Jupyter Notebooks for Data Science
Parul Pandey, Towards Data Science
https://towardsdatascience.com/bringing-the-best-out-of-jupyter-notebooks-for-data-science-f0871519ca29

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值