display.py
51 lines
| 1.4 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r4408 | """Various display related classes. | ||
Danny Staple
|
r8665 | Authors : MinRK, dannystaple | ||
Brian E. Granger
|
r4408 | """ | ||
Danny Staple
|
r8645 | import urllib | ||
Brian E. Granger
|
r4408 | |||
class YouTubeVideo(object): | ||||
"""Class for embedding a YouTube Video in an IPython session, based on its video id. | ||||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | e.g. to embed the video on this page: | ||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | http://www.youtube.com/watch?v=foo | ||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | you would do: | ||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | vid = YouTubeVideo("foo") | ||
display(vid) | ||||
Danny Staple
|
r8639 | |||
Danny Staple
|
r8645 | To start from 30 seconds: | ||
Danny Staple
|
r8639 | |||
Danny Staple
|
r8645 | vid = YouTubeVideo("abc", start=30) | ||
Danny Staple
|
r8639 | display(vid) | ||
Danny Staple
|
r8645 | |||
Danny Staple
|
r8666 | To calculate seconds from time as hours, minutes, seconds use: | ||
start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds()) | ||||
Danny Staple
|
r8651 | |||
Danny Staple
|
r8645 | Other parameters can be provided as documented at | ||
https://developers.google.com/youtube/player_parameters#parameter-subheader | ||||
Brian E. Granger
|
r4408 | """ | ||
Bernardo B. Marques
|
r4872 | |||
Danny Staple
|
r8645 | def __init__(self, id, width=400, height=300, **kwargs): | ||
Brian E. Granger
|
r4408 | self.id = id | ||
self.width = width | ||||
self.height = height | ||||
Danny Staple
|
r8651 | self.params = kwargs | ||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | def _repr_html_(self): | ||
"""return YouTube embed iframe for this video id""" | ||||
Danny Staple
|
r8645 | if self.params: | ||
params = "?" + urllib.urlencode(self.params) | ||||
else: | ||||
params = "" | ||||
Brian E. Granger
|
r4408 | return """ | ||
Bernardo B. Marques
|
r4872 | <iframe | ||
width="%i" | ||||
height="%i" | ||||
Danny Staple
|
r8645 | src="http://www.youtube.com/embed/%s%s" | ||
Bernardo B. Marques
|
r4872 | frameborder="0" | ||
Brian E. Granger
|
r4408 | allowfullscreen | ||
></iframe> | ||||
Danny Staple
|
r8680 | """ % (self.width, self.height, self.id, params) | ||