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 | from IPython.core.display import DisplayObject, TextDisplayObject |
|
9 | from IPython.core.display import DisplayObject, TextDisplayObject | |
10 |
|
10 | |||
11 | from typing import Tuple |
|
11 | from typing import Tuple, Iterable | |
12 |
|
12 | |||
13 | __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument', |
|
13 | __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument', | |
14 | 'FileLink', 'FileLinks', 'Code'] |
|
14 | 'FileLink', 'FileLinks', 'Code'] | |
@@ -255,13 +255,18 b' class IFrame(object):' | |||||
255 | src="{src}{params}" |
|
255 | src="{src}{params}" | |
256 | frameborder="0" |
|
256 | frameborder="0" | |
257 | allowfullscreen |
|
257 | allowfullscreen | |
|
258 | {extras} | |||
258 | ></iframe> |
|
259 | ></iframe> | |
259 | """ |
|
260 | """ | |
260 |
|
261 | |||
261 | def __init__(self, src, width, height, **kwargs): |
|
262 | def __init__(self, src, width, height, extras: Iterable[str] = None, **kwargs): | |
|
263 | if extras is None: | |||
|
264 | extras = [] | |||
|
265 | ||||
262 | self.src = src |
|
266 | self.src = src | |
263 | self.width = width |
|
267 | self.width = width | |
264 | self.height = height |
|
268 | self.height = height | |
|
269 | self.extras = extras | |||
265 | self.params = kwargs |
|
270 | self.params = kwargs | |
266 |
|
271 | |||
267 | def _repr_html_(self): |
|
272 | def _repr_html_(self): | |
@@ -274,10 +279,14 b' class IFrame(object):' | |||||
274 | params = "?" + urlencode(self.params) |
|
279 | params = "?" + urlencode(self.params) | |
275 | else: |
|
280 | else: | |
276 | params = "" |
|
281 | params = "" | |
277 |
return self.iframe.format( |
|
282 | return self.iframe.format( | |
278 | width=self.width, |
|
283 | src=self.src, | |
279 | height=self.height, |
|
284 | width=self.width, | |
280 | params=params) |
|
285 | height=self.height, | |
|
286 | params=params, | |||
|
287 | extras=" ".join(self.extras), | |||
|
288 | ) | |||
|
289 | ||||
281 |
|
290 | |||
282 | class YouTubeVideo(IFrame): |
|
291 | class YouTubeVideo(IFrame): | |
283 | """Class for embedding a YouTube Video in an IPython session, based on its video id. |
|
292 | """Class for embedding a YouTube Video in an IPython session, based on its video id. | |
@@ -305,11 +314,14 b' class YouTubeVideo(IFrame):' | |||||
305 | will be inserted in the document. |
|
314 | will be inserted in the document. | |
306 | """ |
|
315 | """ | |
307 |
|
316 | |||
308 | def __init__(self, id, width=400, height=300, **kwargs): |
|
317 | def __init__(self, id, width=400, height=300, allow_autoplay=False, **kwargs): | |
309 | self.id=id |
|
318 | self.id=id | |
310 | src = "https://www.youtube.com/embed/{0}".format(id) |
|
319 | src = "https://www.youtube.com/embed/{0}".format(id) | |
|
320 | if allow_autoplay: | |||
|
321 | extras = list(kwargs.get("extras", [])) + ['allow="autoplay"'] | |||
|
322 | kwargs.update(autoplay=1, extras=extras) | |||
311 | super(YouTubeVideo, self).__init__(src, width, height, **kwargs) |
|
323 | super(YouTubeVideo, self).__init__(src, width, height, **kwargs) | |
312 |
|
324 | |||
313 | def _repr_jpeg_(self): |
|
325 | def _repr_jpeg_(self): | |
314 | # Deferred import |
|
326 | # Deferred import | |
315 | from urllib.request import urlopen |
|
327 | from urllib.request import urlopen |
General Comments 0
You need to be logged in to leave comments.
Login now