Jupyter 小部件widget
小部件是事件性的python对象,在浏览器中具有表示形式,通常作为控件(如滑块,文本框等)使用。小部件可用于构建Jupyter notebook的交互式GUI。使Jupyter notebook变得互动。
关于Jupyter的基础课程在Jupyter里您可以找到。本节的内容您可在右侧实验区的+号下打开一个您专属的jupyter notebook实例进行体验。或者直接打开网址{host0.cell_url_withtoken}
。
安装
# pip
!pip3 install ipywidgets
!jupyter nbextension enable --py widgetsnbextension
让我们看一些小部件。有关完整的详细信息,您可以访问其Github存储库。
相互作用
的interact
功能(ipywidgets.interact
)自动创建探索代码和数据交互的用户界面(UI)的控制。这是开始使用IPython的小部件的最简单方法。
# Start with some imports!
from ipywidgets import interact
import ipywidgets as widgets
1.基本小部件
def f(x):
return x
# Generate a slider
interact(f, x=10,);
# Booleans generate check-boxes
interact(f, x=True);
#字符串生成文本区域
interact(f,x ='Hi there!');
2.高级小部件
这是一些有用的高级小部件的列表。
播放小工具
“播放”小部件可用于通过以一定速度迭代整数序列来执行动画。下面滑块的值链接到播放器。
play = widgets.Play(
# interval=10,
value=50,
min=0,
max=100,
step=1,
description="Press play",
disabled=False
)
slider = widgets.IntSlider()
widgets.jslink((play, 'value'), (slider, 'value'))
widgets.HBox([play, slider])
日期选择器
日期选择器小部件可在Chrome和IE Edge中使用,但当前在Firefox或Safari中不可用,因为它们不支持HTML日期输入字段。
widgets.DatePicker(
description='Pick a Date',
disabled=False
)
颜色选择器
widgets.ColorPicker(
concise=False,
description='Pick a color',
value='blue',
disabled=False
)
Tab标签
tab_contents = ['P0', 'P1', 'P2', 'P3', 'P4']
children = [widgets.Text(description=name) for name in tab_contents]
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
tab.set_title(i, str(i))
tab
5. Qgrid
*使数据框架直观*
Qgrid还是JupyterJupyter notebook小部件,但主要侧重于数据帧。它使用SlickGrid在JupyterJupyter notebook中渲染熊猫DataFrame。这使您可以使用直观的滚动,排序和过滤控件来浏览DataFrame,以及通过双击单元格来编辑DataFrame。在Github上库包含更多细节和例子。
安装
用pip安装:
pip3 install qgrid
jupyter nbextension enable --py --sys-prefix qgrid#仅在尚未启用ipywidgets nbextension但尚未
启用jupyter nbextension 时才需要--py --sys-prefix小部件
6.幻灯片放映
*交流时代码很棒。*
Jupyter notebook是教授和编写可解释代码的有效工具。但是,当我们要展示我们的作品时,我们将显示整个Jupyter notebook(带有所有代码),或者我们将使用PowerPoint。不再。JupyterJupyter notebook可以轻松转换为幻灯片,我们可以轻松地选择从Jupyter notebook中显示或隐藏内容。
有两种方法可以将Jupyter notebook转换为幻灯片:
1. Jupyter Notebook的内置幻灯片选项
打开一个新Jupyter notebook,然后导航到“ 视图”→“单元格工具栏”→“幻灯片放映”。浅灰色的条出现在每个单元格的顶部,您可以自定义幻灯片。
现在转到Jupyter notebook所在的目录并输入以下代码:
jupyter nbconvert *.ipynb --to slides --post serve
# insert your notebook name instead of *.ipynb
幻灯片将显示在端口8000处。此外,.html
目录中还将生成一个文件,您也可以从该目录访问幻灯片。
在主题背景下,这看起来会更加优雅。让我们将主题“ onedork ”应用于Jupyter notebook,然后将其转换为幻灯片。
这些幻灯片有一个缺点,即您可以看到代码但不能对其进行编辑。RISE插件提供了解决方案。
2.使用RISE插件
RISE是Reveal.js(Jupyter / IPython幻灯片扩展)的首字母缩写。它利用了Reveal.js来运行幻灯片。这非常有用,因为它还提供了运行代码而不必退出幻灯片的能力。
安装
使用conda(推荐):
conda install -c damianavila82 rise
使用点子(不建议使用):
pip3 install RISE
然后再执行两个步骤,在适当的位置安装JS和CSS:
jupyter-nbextension install rise --py --sys-prefix
#enable the nbextension:
jupyter-nbextension enable rise --py --sys-prefix
现在让我们使用RISE进行交互式幻灯片演示。我们将重新打开我们之前创建的Jupyter Notebook。现在,我们注意到一个新的扩展名为“ Enter / Exit RISE Slideshow”。
单击它,就可以了。欢迎来到互动幻灯片的世界。
有关更多信息,请参考文档。
6.嵌入URL,PDF和Youtube视频
*在那里显示!*
当您可以使用IPython的显示模块轻松地将URL,pdf和视频嵌入到JupyterJupyter notebook中时,为什么只使用链接。
网址
#Note that http urls will not be displayed. Only https are allowed inside the Iframe
from IPython.display import IFrame
IFrame('https://en.wikipedia.org/wiki/HTTPS', width=800, height=450)
PDF文件
from IPython.display import IFrame
IFrame('https://arxiv.org/pdf/1406.2661.pdf', width=800, height=450)
YouTube视频
from IPython.display import YouTubeVideo
YouTubeVideo('mJeNghZXtMo', width=800, height=300)
原文:https://towardsdatascience.com/bringing-the-best-out-of-jupyter-notebooks-for-data-science-f0871519ca29 作者:Parul Pandey
这些是JupyterJupyter notebook的一些功能,我发现它们很有用,值得分享。其中一些对您来说很明显,而另一些则是新的。因此,继续进行试验。希望他们能够为您节省一些时间,并为您提供更好的UI体验。也可以随时在评论中建议其他有用的功能。
建议使用PC或笔记本电脑,浏览器使用Chrome或FireFox进行浏览,以开启左侧互动实验区来提升学习效率,推荐使用的分辨率为1920x1080或更高。
我们坚信最好的学习是参与其中这一理念,并致力成为中文互联网上体验更好的学练一体的IT技术学习交流平台。

您可加QQ群:575806994,一起学习交流技术,反馈网站使用中遇到问题。
内容、课程、广告等相关合作请扫描右侧二维码添加好友。
狐狸教程 Copyright 2021