[英]How can I layout a list of IPython.display.Audio objects in a Jupyter notebook?
I have a list of audio data that I want to display in a Jupyter notebook using IPython.display.Audio
such that these controls are flowing from left to right next to one another. 我有一个音频数据列表,希望使用
IPython.display.Audio
在Jupyter笔记本中显示,以便这些控件从左到右彼此相邻流动。 I tried using ipywidgets.HBox
but I get the an error since IPython.display.Audio
is not an instance of a Widget
. 我尝试使用
ipywidgets.HBox
但是由于IPython.display.Audio
不是Widget
的实例,所以出现错误。
My list of of audio data looks like the following, where y
is the audio data and sr
is the sampling rate. 我的音频数据列表如下所示,其中
y
是音频数据, sr
是采样率。
data = [(y1, sr1), (y2, sr2), ..., (yN, srN)]
What I tried to do is something like the following. 我试图做的是如下所示。
audio_widgets = [display.Audio(audio[0], sr=audio[1]) for audio in data]
audio_hbox = widgets.HBox(audio_widgets) # TraitError happens here
I get the following error. 我收到以下错误。
TraitError: Element of the 'children' trait of a HBox instance must be a Widget, but a value of <IPython.lib.display.Audio object> <class 'IPython.lib.display.Audio'> was specified.
One way to get the audio displays to show is to simply show them one above the other (which takes up a lot of vertical space when there are a lot of audio data). 一种使音频显示得以显示的方法是简单地将它们一个显示在另一个之上(当有大量音频数据时,它将占用大量垂直空间)。
for audio in data:
audio_widget = display.Audio(audio[0], sr=audio[1])
display(audio_widget)
Any ideas on how I can control the layout display of audio controls? 关于如何控制音频控件的布局显示的任何想法?
You could display the Audio
s into output widgets . 您可以将
Audio
显示到输出小部件中 。 Anything that displays nicely in a notebook will also display nicely in an output widget. 笔记本中显示良好的所有内容也将在输出窗口小部件中良好显示。 You can direct output to the widget using a context manager, see example code below.
您可以使用上下文管理器将输出定向到小部件,请参见下面的示例代码。
from IPython.display import Audio, display
from ipywidgets import widgets
audio_widgets = []
for (audio, sample_rate) in data:
out = widgets.Output()
with out:
display(Audio(data=audio, rate=sample_rate))
audio_widgets.append(out)
widgets.HBox(audio_widgets)
You could also print
or display
something else into the same output widget beside the Audio
- possibly to have a label next to each audio piece. 您还可以在“
Audio
旁边的同一输出小部件中print
或display
其他内容-可能在每个音频片段旁边有一个标签。
This is a very late answer but whatever)). 这是一个很晚的答案,但无论如何)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.