简体   繁体   English

Jupyter(IPython)Notebook单元中的多个Audio对象

[英]More than one Audio object in a Jupyter (IPython) Notebook cell

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. 我试图在一个Jupyter Notebook单元格中嵌入多个IPython.display.Audio对象,但由于某种原因,只显示最后一个。

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. 理想情况下,我想将其置于for循环中,并在单个单元格中显示多个音频对象。

Any ideas? 有任何想法吗?

Note: I am running Jupyter 4.0.6, with IPython 4.0.0, on Python 2.7.10. 注意:我在Python 2.7.10上使用IPython 4.0.0运行Jupyter 4.0.6。

The IPython.display.Audio(...) command only creates a "display" object (in that particular case, an object of the subclass Audio of the class DisplayObject ). IPython.display.Audio(...)命令仅创建 “显示”对象(在该特定情况下,是DisplayObject类的子类Audio的对象)。

Afterwards, you may do basic actions with such an object, tied to the class DisplayObject (and specific stuff tied to the class Audio ). 之后,您可以使用这样的对象执行基本操作,绑定到DisplayObject类(以及绑定到类Audio特定内容)。 One of those actions consists of displaying it, by using the IPython.display.display function. 其中一个操作包括使用IPython.display.display函数显示它。

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. 相同的机制被用于显示其他类型的(子类) DisplayObject对象: HTMLMarkdownMathSVGJavascriptVideoImage等。参见的详细信息。

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; 命令IPython.display.Audio的名称,似乎意味着会显示某些内容; 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 (eg, a Javascript object); 事实上,所有这些多媒体对象统称为“显示”对象,而其中一些永远不会真正“显示”,只是嵌入在DOM树中(例如, Javascript对象);

  • 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 ; 事实上,如果你创建这样一个对象并且不使用IPython.display.display ,它将自动由标准IPython交互机制显示, 如果它是在单元格中创建的最后一件事 ; 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". 这是混淆的主要原因,因为它让人们认为你不需要使用任何特定的功能来显示“显示对象”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM