Skip to content

Commit

Permalink
Add support for width and height arguments when displaying Video
Browse files Browse the repository at this point in the history
Closes #11328
  • Loading branch information
finaiized committed Oct 1, 2018
1 parent 94b894d commit 211ca17
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
27 changes: 21 additions & 6 deletions IPython/core/display.py
Expand Up @@ -1256,7 +1256,8 @@ def _find_ext(self, s):

class Video(DisplayObject):

def __init__(self, data=None, url=None, filename=None, embed=False, mimetype=None):
def __init__(self, data=None, url=None, filename=None, embed=False,
mimetype=None, width=None, height=None):
"""Create a video object given raw data or an URL.
When this object is returned by an input cell or passed to the
Expand Down Expand Up @@ -1288,6 +1289,12 @@ def __init__(self, data=None, url=None, filename=None, embed=False, mimetype=Non
mimetype: unicode
Specify the mimetype for embedded videos.
Default will be guessed from file extension, if available.
width : int
Width in pixels to which to constrain the video in HTML.
If not supplied, defaults to the width of the video.
height : int
Height in pixels to which to constrain the video in html.
If not supplied, defaults to the height of the video.
Examples
--------
Expand All @@ -1314,16 +1321,24 @@ def __init__(self, data=None, url=None, filename=None, embed=False, mimetype=Non

self.mimetype = mimetype
self.embed = embed
self.width = width
self.height = height
super(Video, self).__init__(data=data, url=url, filename=filename)

def _repr_html_(self):
width = height = ''
if self.width:
width = ' width="%d"' % self.width
if self.height:
height = ' height="%d"' % self.height

# External URLs and potentially local files are not embedded into the
# notebook output.
if not self.embed:
url = self.url if self.url is not None else self.filename
output = """<video src="{0}" controls>
output = """<video src="{0}" controls {1} {2}>
Your browser does not support the <code>video</code> element.
</video>""".format(url)
</video>""".format(url, width, height)
return output

# Embedded videos are base64-encoded.
Expand All @@ -1342,10 +1357,10 @@ def _repr_html_(self):
else:
b64_video = b2a_base64(video).decode('ascii').rstrip()

output = """<video controls>
<source src="data:{0};base64,{1}" type="{0}">
output = """<video controls {0} {1}>
<source src="data:{2};base64,{3}" type="{2}">
Your browser does not support the video tag.
</video>""".format(mimetype, b64_video)
</video>""".format(width, height, mimetype, b64_video)
return output

def reload(self):
Expand Down
1 change: 1 addition & 0 deletions docs/source/whatsnew/pr/video-width-height.rst
@@ -0,0 +1 @@
``IPython.display.Video`` now supports ``width`` and ``height`` arguments, allowing a custom width and height to be set instead of using the video's width and height

0 comments on commit 211ca17

Please sign in to comment.