More than one Audio object in a Jupyter (IPython)

2019-06-16 17:31发布

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.

1条回答
Lonely孤独者°
2楼-- · 2019-06-16 18:16

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".

查看更多
登录 后发表回答