More than one Audio object in a Jupyter (IPython) Notebook cell
Asked Answered
S

1

32

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.

Sims answered 9/10, 2015 at 23:2 Comment(4)
You can display multiple "display" objects in the display area of a single cell by using the IPython.display.display function repeatedly. See this for an example.Diverticulitis
Taar, unfortunately this doesn't work when using Audio objects (or at least I couldn't figure it out). Thanks, though.Sims
I did this: 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")) and it seems to be working (on Windows 7 with python 3.4 + IPython 3.2...)Diverticulitis
Awesome, that was exactly it! Would you care to post this as an answer? Thanks.Sims
D
51

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

Diverticulitis answered 13/10, 2015 at 18:8 Comment(1)
Really educational answer. I wish there were more like this.Vulgar

© 2022 - 2024 — McMap. All rights reserved.