diff --git a/IPython/lib/display.py b/IPython/lib/display.py index ebb8206..7b94acf 100644 --- a/IPython/lib/display.py +++ b/IPython/lib/display.py @@ -8,7 +8,7 @@ from os import walk, sep, fsdecode from IPython.core.display import DisplayObject, TextDisplayObject -from typing import Tuple +from typing import Tuple, Iterable __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument', 'FileLink', 'FileLinks', 'Code'] @@ -255,13 +255,18 @@ class IFrame(object): src="{src}{params}" frameborder="0" allowfullscreen + {extras} > """ - def __init__(self, src, width, height, **kwargs): + def __init__(self, src, width, height, extras: Iterable[str] = None, **kwargs): + if extras is None: + extras = [] + self.src = src self.width = width self.height = height + self.extras = extras self.params = kwargs def _repr_html_(self): @@ -274,10 +279,14 @@ class IFrame(object): params = "?" + urlencode(self.params) else: params = "" - return self.iframe.format(src=self.src, - width=self.width, - height=self.height, - params=params) + return self.iframe.format( + src=self.src, + width=self.width, + height=self.height, + params=params, + extras=" ".join(self.extras), + ) + class YouTubeVideo(IFrame): """Class for embedding a YouTube Video in an IPython session, based on its video id. @@ -305,11 +314,14 @@ class YouTubeVideo(IFrame): will be inserted in the document. """ - def __init__(self, id, width=400, height=300, **kwargs): + def __init__(self, id, width=400, height=300, allow_autoplay=False, **kwargs): self.id=id src = "https://www.youtube.com/embed/{0}".format(id) + if allow_autoplay: + extras = list(kwargs.get("extras", [])) + ['allow="autoplay"'] + kwargs.update(autoplay=1, extras=extras) super(YouTubeVideo, self).__init__(src, width, height, **kwargs) - + def _repr_jpeg_(self): # Deferred import from urllib.request import urlopen diff --git a/docs/source/whatsnew/pr/enable-to-add-extra-attrs-to-iframe.rst b/docs/source/whatsnew/pr/enable-to-add-extra-attrs-to-iframe.rst new file mode 100644 index 0000000..f4f3ad5 --- /dev/null +++ b/docs/source/whatsnew/pr/enable-to-add-extra-attrs-to-iframe.rst @@ -0,0 +1,37 @@ +Enable to add extra attributes to iframe +======================================== + +You can add any extra attributes to the `` + +Using it, you can autoplay ``YouTubeVideo`` by adding ``'allow="autoplay"'``, +even in browsers that disable it by default, such as Google Chrome. +And, you can write it more briefly by using the argument ``allow_autoplay``. +:: + + In [1]: from IPython.display import YouTubeVideo + + In [2]: print(YouTubeVideo("video-id", allow_autoplay=True)._repr_html_()) + +