Show More
@@ -0,0 +1,37 b'' | |||
|
1 | Enable to add extra attributes to iframe | |
|
2 | ======================================== | |
|
3 | ||
|
4 | You can add any extra attributes to the ``<iframe>`` tag | |
|
5 | since the argument ``extras`` has been added to the ``IFrame`` class. | |
|
6 | for example:: | |
|
7 | ||
|
8 | In [1]: from IPython.display import IFrame | |
|
9 | ||
|
10 | In [2]: print(IFrame(src="src", width=300, height=300, extras=["hello", "world"])._repr_html_()) | |
|
11 | ||
|
12 | <iframe | |
|
13 | width="300" | |
|
14 | height="300" | |
|
15 | src="src" | |
|
16 | frameborder="0" | |
|
17 | allowfullscreen | |
|
18 | hello world | |
|
19 | ></iframe> | |
|
20 | ||
|
21 | Using it, you can autoplay ``YouTubeVideo`` by adding ``'allow="autoplay"'``, | |
|
22 | even in browsers that disable it by default, such as Google Chrome. | |
|
23 | And, you can write it more briefly by using the argument ``allow_autoplay``. | |
|
24 | :: | |
|
25 | ||
|
26 | In [1]: from IPython.display import YouTubeVideo | |
|
27 | ||
|
28 | In [2]: print(YouTubeVideo("video-id", allow_autoplay=True)._repr_html_()) | |
|
29 | ||
|
30 | <iframe | |
|
31 | width="400" | |
|
32 | height="300" | |
|
33 | src="https://www.youtube.com/embed/video-id?autoplay=1" | |
|
34 | frameborder="0" | |
|
35 | allowfullscreen | |
|
36 | allow="autoplay" | |
|
37 | ></iframe> |
@@ -8,7 +8,7 b' from os import walk, sep, fsdecode' | |||
|
8 | 8 | |
|
9 | 9 | from IPython.core.display import DisplayObject, TextDisplayObject |
|
10 | 10 | |
|
11 | from typing import Tuple | |
|
11 | from typing import Tuple, Iterable | |
|
12 | 12 | |
|
13 | 13 | __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument', |
|
14 | 14 | 'FileLink', 'FileLinks', 'Code'] |
@@ -263,13 +263,18 b' class IFrame(object):' | |||
|
263 | 263 | src="{src}{params}" |
|
264 | 264 | frameborder="0" |
|
265 | 265 | allowfullscreen |
|
266 | {extras} | |
|
266 | 267 | ></iframe> |
|
267 | 268 | """ |
|
268 | 269 | |
|
269 | def __init__(self, src, width, height, **kwargs): | |
|
270 | def __init__(self, src, width, height, extras: Iterable[str] = None, **kwargs): | |
|
271 | if extras is None: | |
|
272 | extras = [] | |
|
273 | ||
|
270 | 274 | self.src = src |
|
271 | 275 | self.width = width |
|
272 | 276 | self.height = height |
|
277 | self.extras = extras | |
|
273 | 278 | self.params = kwargs |
|
274 | 279 | |
|
275 | 280 | def _repr_html_(self): |
@@ -279,10 +284,14 b' class IFrame(object):' | |||
|
279 | 284 | params = "?" + urlencode(self.params) |
|
280 | 285 | else: |
|
281 | 286 | params = "" |
|
282 |
return self.iframe.format( |
|
|
283 | width=self.width, | |
|
284 | height=self.height, | |
|
285 | params=params) | |
|
287 | return self.iframe.format( | |
|
288 | src=self.src, | |
|
289 | width=self.width, | |
|
290 | height=self.height, | |
|
291 | params=params, | |
|
292 | extras=" ".join(self.extras), | |
|
293 | ) | |
|
294 | ||
|
286 | 295 | |
|
287 | 296 | class YouTubeVideo(IFrame): |
|
288 | 297 | """Class for embedding a YouTube Video in an IPython session, based on its video id. |
@@ -310,11 +319,14 b' class YouTubeVideo(IFrame):' | |||
|
310 | 319 | will be inserted in the document. |
|
311 | 320 | """ |
|
312 | 321 | |
|
313 | def __init__(self, id, width=400, height=300, **kwargs): | |
|
322 | def __init__(self, id, width=400, height=300, allow_autoplay=False, **kwargs): | |
|
314 | 323 | self.id=id |
|
315 | 324 | src = "https://www.youtube.com/embed/{0}".format(id) |
|
325 | if allow_autoplay: | |
|
326 | extras = list(kwargs.get("extras", [])) + ['allow="autoplay"'] | |
|
327 | kwargs.update(autoplay=1, extras=extras) | |
|
316 | 328 | super(YouTubeVideo, self).__init__(src, width, height, **kwargs) |
|
317 | ||
|
329 | ||
|
318 | 330 | def _repr_jpeg_(self): |
|
319 | 331 | # Deferred import |
|
320 | 332 | from urllib.request import urlopen |
General Comments 0
You need to be logged in to leave comments.
Login now