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

夙愿已清 提交于 2019-12-12 10:35:12

问题


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:


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



来源:https://stackoverflow.com/questions/33048353/more-than-one-audio-object-in-a-jupyter-ipython-notebook-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!