Show More
@@ -0,0 +1,4 b'' | |||||
|
1 | Video HTML attributes | |||
|
2 | ===================== | |||
|
3 | ||||
|
4 | Add an option to `IPython.display.Video` to change the attributes of the HTML display of the video. |
@@ -1329,7 +1329,7 b' class Image(DisplayObject):' | |||||
1329 | class Video(DisplayObject): |
|
1329 | class Video(DisplayObject): | |
1330 |
|
1330 | |||
1331 | def __init__(self, data=None, url=None, filename=None, embed=False, |
|
1331 | def __init__(self, data=None, url=None, filename=None, embed=False, | |
1332 | mimetype=None, width=None, height=None): |
|
1332 | mimetype=None, width=None, height=None, html_attributes="controls"): | |
1333 | """Create a video object given raw data or an URL. |
|
1333 | """Create a video object given raw data or an URL. | |
1334 |
|
1334 | |||
1335 | When this object is returned by an input cell or passed to the |
|
1335 | When this object is returned by an input cell or passed to the | |
@@ -1367,14 +1367,22 b' class Video(DisplayObject):' | |||||
1367 | height : int |
|
1367 | height : int | |
1368 | Height in pixels to which to constrain the video in html. |
|
1368 | Height in pixels to which to constrain the video in html. | |
1369 | If not supplied, defaults to the height of the video. |
|
1369 | If not supplied, defaults to the height of the video. | |
|
1370 | html_attributes : str | |||
|
1371 | Attributes for the HTML `<video>` block. | |||
|
1372 | Default: `"controls"` to get video controls. | |||
|
1373 | Other examples: `"controls muted"` for muted video with controls, | |||
|
1374 | `"loop autoplay"` for looping autoplaying video without controls. | |||
1370 |
|
1375 | |||
1371 | Examples |
|
1376 | Examples | |
1372 | -------- |
|
1377 | -------- | |
1373 |
|
1378 | |||
1374 | Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4') |
|
1379 | :: | |
1375 | Video('path/to/video.mp4') |
|
1380 | ||
1376 | Video('path/to/video.mp4', embed=True) |
|
1381 | Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4') | |
1377 | Video(b'raw-videodata', embed=True) |
|
1382 | Video('path/to/video.mp4') | |
|
1383 | Video('path/to/video.mp4', embed=True) | |||
|
1384 | Video('path/to/video.mp4', embed=True, html_attributes="controls muted autoplay") | |||
|
1385 | Video(b'raw-videodata', embed=True) | |||
1378 | """ |
|
1386 | """ | |
1379 | if isinstance(data, (Path, PurePath)): |
|
1387 | if isinstance(data, (Path, PurePath)): | |
1380 | data = str(data) |
|
1388 | data = str(data) | |
@@ -1398,6 +1406,7 b' class Video(DisplayObject):' | |||||
1398 | self.embed = embed |
|
1406 | self.embed = embed | |
1399 | self.width = width |
|
1407 | self.width = width | |
1400 | self.height = height |
|
1408 | self.height = height | |
|
1409 | self.html_attributes = html_attributes | |||
1401 | super(Video, self).__init__(data=data, url=url, filename=filename) |
|
1410 | super(Video, self).__init__(data=data, url=url, filename=filename) | |
1402 |
|
1411 | |||
1403 | def _repr_html_(self): |
|
1412 | def _repr_html_(self): | |
@@ -1411,9 +1420,9 b' class Video(DisplayObject):' | |||||
1411 | # notebook output. |
|
1420 | # notebook output. | |
1412 | if not self.embed: |
|
1421 | if not self.embed: | |
1413 | url = self.url if self.url is not None else self.filename |
|
1422 | url = self.url if self.url is not None else self.filename | |
1414 |
output = """<video src="{0}" |
|
1423 | output = """<video src="{0}" {1} {2} {3}> | |
1415 | Your browser does not support the <code>video</code> element. |
|
1424 | Your browser does not support the <code>video</code> element. | |
1416 | </video>""".format(url, width, height) |
|
1425 | </video>""".format(url, self.html_attributes, width, height) | |
1417 | return output |
|
1426 | return output | |
1418 |
|
1427 | |||
1419 | # Embedded videos are base64-encoded. |
|
1428 | # Embedded videos are base64-encoded. | |
@@ -1432,10 +1441,10 b' class Video(DisplayObject):' | |||||
1432 | else: |
|
1441 | else: | |
1433 | b64_video = b2a_base64(video).decode('ascii').rstrip() |
|
1442 | b64_video = b2a_base64(video).decode('ascii').rstrip() | |
1434 |
|
1443 | |||
1435 |
output = """<video |
|
1444 | output = """<video {0} {1} {2}> | |
1436 |
<source src="data:{ |
|
1445 | <source src="data:{3};base64,{4}" type="{3}"> | |
1437 | Your browser does not support the video tag. |
|
1446 | Your browser does not support the video tag. | |
1438 | </video>""".format(width, height, mimetype, b64_video) |
|
1447 | </video>""".format(self.html_attributes, width, height, mimetype, b64_video) | |
1439 | return output |
|
1448 | return output | |
1440 |
|
1449 | |||
1441 | def reload(self): |
|
1450 | def reload(self): |
General Comments 0
You need to be logged in to leave comments.
Login now