From 9fd6be2ffd940b78ce356d8d68df07f496bf5239 2018-10-03 15:48:34 From: Matthias Bussonnier Date: 2018-10-03 15:48:34 Subject: [PATCH] Merge pull request #11353 from finaiized/master Add support for width and height arguments when displaying Video --- diff --git a/IPython/core/display.py b/IPython/core/display.py index 1f2a19c..7128d0f 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -1267,7 +1267,8 @@ class Image(DisplayObject): 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 @@ -1299,6 +1300,12 @@ class Video(DisplayObject): 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 -------- @@ -1325,16 +1332,24 @@ class Video(DisplayObject): 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 = """""".format(url, width, height) return output # Embedded videos are base64-encoded. @@ -1353,10 +1368,10 @@ class Video(DisplayObject): else: b64_video = b2a_base64(video).decode('ascii').rstrip() - output = """""".format(width, height, mimetype, b64_video) return output def reload(self): diff --git a/docs/source/whatsnew/pr/video-width-height.rst b/docs/source/whatsnew/pr/video-width-height.rst new file mode 100644 index 0000000..84757f1 --- /dev/null +++ b/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 \ No newline at end of file