Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 957
Oriol Nieto Avatar asked Oct 09 '15 23:10

Oriol Nieto


People also ask

What does %% do in Jupyter Notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.

How do you split a cell in Jupyter Notebook?

There are commonly known keyboard shortcuts such as Ctrl + Shift + - to split the cell, Shift + M to merge multiple cells, and Shift + Down / Up to select the cells below or above the selected one.


1 Answers

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

like image 90
Taar Avatar answered Sep 18 '22 19:09

Taar