我正在尝试将视频嵌入到jupyter / ipython notebook单元格中 .

如果您希望将视频作为单元格的输出播放,以下说明可以完美地工作(如下图所示):

import io
import base64
from IPython.display import HTML

#video = io.open('/home/ubuntu/Downloads/test.mp4', 'r+b').read()
encoded = base64.b64encode(video)

HTML(data='''<video alt="test" controls>
     <source src="data:video/mp4;base64,{0}" type="video/mp4" />
     </video>'''.format(encoded.decode('ascii')))

enter image description here

但是我正在尝试使用4 matplotlib轴创建一个单元格输出,其中2个是常规绘图,其中2个是嵌入视频 .

使用此命令,我创建一个子图的网格:

fig, ax = plt.subplots(2,2,figsize=(10,10),sharex=False,sharey=False)

然后我可以通过单独索引和绘图来绘制每一个,即:

ax[0][0].plot([x_1, x_2], [y_1, y_2], 'bo-', linewidth=4)

How do I embed the HTML into one of the axis?

我天真地尝试过:

ax[0][1].HTML(data='''<video alt="test" controls>
     <source src="data:video/mp4;base64,{0}" type="video/mp4" />
     </video>'''.format(encoded.decode('ascii')))

但由于轴子图没有HTML属性,显然会出现以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-16-a3bc39226382> in <module>()
      7 
      8 fig, ax = plt.subplots(2,2,figsize=(10,10),sharex=False,sharey=False)
----> 9 ax[0][1].HTML(data='''<video alt="test" controls>
     10                 <source src="data:video/mp4;base64,{0}" type="video/mp4" />
     11              </video>'''.format(encoded.decode('ascii')))

AttributeError: 'AxesSubplot' object has no attribute 'HTML'