Last active
September 21, 2017 09:37
-
-
Save dmmfll/55823165c104717ca49863fc526d1354 to your computer and use it in GitHub Desktop.
function for embedding a YouTube video into a Jupyter notebook. Be sure to use the url in the html from the embed share tab in YouTube.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Embed a YouTube video via its embed url into a notebook.""" | |
from functools import partial | |
from IPython.display import display, IFrame | |
width, height = (560, 315, ) | |
def _iframe_attrs(embed_url): | |
"""Get IFrame args.""" | |
return ( | |
('src', 'width', 'height'), | |
(embed_url, width, height, ), | |
) | |
def _get_args(embed_url): | |
"""Get args for type to create a class.""" | |
iframe = dict(zip(*_iframe_attrs(embed_url))) | |
attrs = { | |
'display': partial(display, IFrame(**iframe)), | |
} | |
return ('YouTubeVideo', (object, ), attrs, ) | |
def youtube_video(embed_url): | |
"""Embed YouTube video into a notebook. | |
Place this module into the same directory as the notebook. | |
>>> from embed import youtube_video | |
>>> youtube_video(url).display() | |
""" | |
YouTubeVideo = type(*_get_args(embed_url)) # make a class | |
return YouTubeVideo() # return an object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment