Jupyter(IPython)笔记本单元中的多个音频对象 [英] More than one Audio object in a Jupyter (IPython) Notebook cell

查看:101
本文介绍了Jupyter(IPython)笔记本单元中的多个音频对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个Jupyter Notebook单元中嵌入多个IPython.display.Audio对象,但是由于某种原因,只有最后一个显示.

下面是一个简单的示例:

import IPython
IPython.display.Audio(url="https://ccrma.stanford.edu/~urinieto/drop/090412-Incendios.mp3")
IPython.display.Audio(url="https://ccrma.stanford.edu/~urinieto/drop/130224-Undertow.mp3")

这仅显示一个(第二个)音频对象.理想情况下,我想将其放置在for循环中,并在单个单元格中显示多个音频对象.

有什么想法吗?

注意:我正在Python 2.7.10上运行带有IPython 4.0.0的Jupyter 4.0.6.

解决方案

命令创建 对象(在这种情况下,是子类Audio的对象).类DisplayObject).

然后,您可以对与类DisplayObject关联的对象(以及与类Audio关联的特定内容)进行基本操作.这些操作之一是通过使用IPython.display.display函数显示它.

您的特定目标将通过以下代码实现:

import IPython
IPython.display.display(IPython.display.Audio(url="https://ccrma.stanford.edu/~urinieto/drop/090412-Incendios.mp3"))
IPython.display.display(IPython.display.Audio(url="https://ccrma.stanford.edu/~urinieto/drop/130224-Undertow.mp3"))

使用相同的机制来显示DisplayObject对象的其他类型(子类):HTMLMarkdownMathSVGJavascriptVideoImage等.有关详细信息,请参见.

当您初次尝试这样做时,三件事确实令人困惑(起初我也很困惑):

  • 命令IPython.display.Audio的名称,这似乎意味着将显示某些内容.并非如此;

  • 所有这些多媒体对象统称为显示"对象,而其中一些从未真正显示",而是仅嵌入DOM树中(例如,Javascript对象); p>

  • 如果您创建了这样的对象并且不使用IPython.display.display,那么它是标准IPython交互机制会自动显示的事实,如果它是单元格中最后创建的内容;这是造成混乱的主要原因,因为它使人们认为您不需要使用任何特定的功能来显示显示对象".

I am trying to embed more than one IPython.display.Audio object in a single Jupyter Notebook cell, but for some reason only the last one gets displayed.

Here a simple example:

import IPython
IPython.display.Audio(url="https://ccrma.stanford.edu/~urinieto/drop/090412-Incendios.mp3")
IPython.display.Audio(url="https://ccrma.stanford.edu/~urinieto/drop/130224-Undertow.mp3")

This only displays one (the second one) audio object. Ideally I would like to place this in a for loop and display multiple audio objects in a single cell.

Any ideas?

Note: I am running Jupyter 4.0.6, with IPython 4.0.0, on Python 2.7.10.

解决方案

The IPython.display.Audio(...) command only creates a "display" object (in that particular case, an object of the subclass Audio of the class DisplayObject).

Afterwards, you may do basic actions with such an object, tied to the class DisplayObject (and specific stuff tied to the class Audio). One of those actions consists of displaying it, by using the IPython.display.display function.

Your particular goal will thus be achieved by the following code:

import IPython
IPython.display.display(IPython.display.Audio(url="https://ccrma.stanford.edu/~urinieto/drop/090412-Incendios.mp3"))
IPython.display.display(IPython.display.Audio(url="https://ccrma.stanford.edu/~urinieto/drop/130224-Undertow.mp3"))

The same mechanism is used to display other types (subclasses) of DisplayObject objects: HTML, Markdown, Math, SVG, Javascript, Video, Image, etc. See this for details.

Three things are really confusing when you try to do this for the first time (I was also confused at first):

  • the name of the command IPython.display.Audio, which seems to imply that something will be displayed; that isn't the case;

  • the fact that all those multimedia objects are collectively called "display" objects, while some of them are never really "displayed", just embedded in the DOM tree (e.g., a Javascript object);

  • the fact that if you create such an object and don't use IPython.display.display on it, it will be automatically displayed by the standard IPython interactive mechanism if it's the last thing created in the cell; that's the major source of confusion because it lets people think that you don't need to use any particular function to display a "display object".

这篇关于Jupyter(IPython)笔记本单元中的多个音频对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆