From 2d23963c5cb33e2c31cfa071e3b1fa7d7f035179 2013-04-24 04:40:24 From: Min RK Date: 2013-04-24 04:40:24 Subject: [PATCH] Merge pull request #3200 from 3kwa/master add base IFrame class, and YouTubeVideo siblings, VimeoVideo and ScribdDocument --- diff --git a/IPython/lib/display.py b/IPython/lib/display.py index 3066c49..e747b37 100644 --- a/IPython/lib/display.py +++ b/IPython/lib/display.py @@ -6,7 +6,40 @@ from os.path import exists, isfile, splitext, abspath, join, isdir from os import walk, sep -class YouTubeVideo(object): +class IFrame(object): + """ + Generic class to embed an iframe in an IPython notebook + """ + + iframe = """ + + """ + + def __init__(self, src, width, height, **kwargs): + self.src = src + self.width = width + self.height = height + self.params = kwargs + + def _repr_html_(self): + """return the embed iframe""" + if self.params: + from urllib import urlencode + params = "?" + urlencode(self.params) + else: + params = "" + return self.iframe.format(src=self.src, + width=self.width, + height=self.height, + params=params) + +class YouTubeVideo(IFrame): """Class for embedding a YouTube Video in an IPython session, based on its video id. e.g. to embed the video on this page: @@ -17,41 +50,47 @@ class YouTubeVideo(object): vid = YouTubeVideo("foo") display(vid) - + To start from 30 seconds: - + vid = YouTubeVideo("abc", start=30) display(vid) - + To calculate seconds from time as hours, minutes, seconds use: start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds()) - Other parameters can be provided as documented at + Other parameters can be provided as documented at https://developers.google.com/youtube/player_parameters#parameter-subheader """ def __init__(self, id, width=400, height=300, **kwargs): - self.id = id - self.width = width - self.height = height - self.params = kwargs + src = "http://www.youtube.com/embed/{0}".format(id) + super(YouTubeVideo, self).__init__(src, width, height, **kwargs) - def _repr_html_(self): - """return YouTube embed iframe for this video id""" - if self.params: - from urllib import urlencode # Deferred import - params = "?" + urlencode(self.params) - else: - params = "" - return """ - - """ % (self.width, self.height, self.id, params) +class VimeoVideo(IFrame): + """ + Class for embedding a Vimeo video in an IPython session, based on its video id. + """ + + def __init__(self, id, width=400, height=300, **kwargs): + src="http://player.vimeo.com/video/{0}".format(id) + super(VimeoVideo, self).__init__(src, width, height, **kwargs) + +class ScribdDocument(IFrame): + """ + Class for embedding a Scribd document in an IPython session + + Use the start_page params to specify a starting point in the document + Use the view_mode params to specify display type one off scroll | slideshow | book + + e.g to Display Wes' foundational paper about PANDAS in book mode from page 3 + + ScribdDocument(71048089, width=800, height=400, start_page=3, view_mode="book") + """ + + def __init__(self, id, width=400, height=300, **kwargs): + src="http://www.scribd.com/embeds/{0}/content".format(id) + super(ScribdDocument, self).__init__(src, width, height, **kwargs) class FileLink(object): """Class for embedding a local file link in an IPython session, based on path @@ -62,14 +101,14 @@ class FileLink(object): local_file = FileLink("my/data.txt") display(local_file) - + or in the HTML notebook, just:: - + FileLink("my/data.txt") """ - + html_link_str = "%s" - + def __init__(self, path, url_prefix='files/', @@ -85,7 +124,7 @@ class FileLink(object): 'files'] result_html_prefix : str text to append to beginning to link [default: none] - result_html_suffix : str + result_html_suffix : str text to append at the end of link [default: '
'] """ if isdir(path): @@ -96,24 +135,24 @@ class FileLink(object): self.url_prefix = url_prefix self.result_html_prefix = result_html_prefix self.result_html_suffix = result_html_suffix - + def _format_path(self): fp = ''.join([self.url_prefix,self.path]) return ''.join([self.result_html_prefix, self.html_link_str % (fp, self.path), self.result_html_suffix]) - + def _repr_html_(self): """return html link to file """ if not exists(self.path): - return ("Path (%s) doesn't exist. " + return ("Path (%s) doesn't exist. " "It may still be in the process of " "being generated, or you may have the " "incorrect path." % self.path) - + return self._format_path() - + def __repr__(self): """return absolute path to file """ @@ -128,11 +167,11 @@ class FileLinks(FileLink): local_files = FileLinks("my/data") display(local_files) - + or in the HTML notebook, just - + FileLinks("my/data") - + """ def __init__(self, path, @@ -145,34 +184,34 @@ class FileLinks(FileLink): """ included_suffixes : list of filename suffixes to include when formatting output [default: include all files] - - See the FileLink (baseclass of LocalDirectory) docstring for + + See the FileLink (baseclass of LocalDirectory) docstring for information on additional parameters. - - notebook_display_formatter : func used to format links for display + + notebook_display_formatter : func used to format links for display in the notebook. See discussion of formatter function below. - - terminal_display_formatter : func used to format links for display + + terminal_display_formatter : func used to format links for display in the terminal. See discussion of formatter function below. - - + + Passing custom formatter functions ---------------------------------- Formatter functions must be of the form: f(dirname, fnames, included_suffixes) - dirname : the name of a directory (a string), - fnames : a list of the files in that directory - included_suffixes : a list of the file suffixes that should be - included in the output (passing None means + dirname : the name of a directory (a string), + fnames : a list of the files in that directory + included_suffixes : a list of the file suffixes that should be + included in the output (passing None means to include all suffixes in the output in the built-in formatters) - - returns a list of lines that should will be print in the + + returns a list of lines that should will be print in the notebook (if passing notebook_display_formatter) or the terminal - (if passing terminal_display_formatter). This function is iterated - over for each directory in self.path. Default formatters are in + (if passing terminal_display_formatter). This function is iterated + over for each directory in self.path. Default formatters are in place, can be passed here to support alternative formatting. - + """ if isfile(path): raise ValueError,\ @@ -181,33 +220,33 @@ class FileLinks(FileLink): self.included_suffixes = included_suffixes # remove trailing slashs for more consistent output formatting path = path.rstrip('/') - + self.path = path self.url_prefix = url_prefix self.result_html_prefix = result_html_prefix self.result_html_suffix = result_html_suffix - + self.notebook_display_formatter = \ notebook_display_formatter or self._get_notebook_display_formatter() self.terminal_display_formatter = \ terminal_display_formatter or self._get_terminal_display_formatter() - + def _get_display_formatter(self, dirname_output_format, fname_output_format, fp_format, fp_cleaner=None): """ generate built-in formatter function - - this is used to define both the notebook and terminal built-in + + this is used to define both the notebook and terminal built-in formatters as they only differ by some wrapper text for each entry - - dirname_output_format: string to use for formatting directory + + dirname_output_format: string to use for formatting directory names, dirname will be substituted for a single "%s" which must appear in this string fname_output_format: string to use for formatting file names, if a single "%s" appears in the string, fname will be substituted - if two "%s" appear in the string, the path to fname will be + if two "%s" appear in the string, the path to fname will be substituted for the first and fname will be substituted for the second fp_format: string to use for formatting filepaths, must contain @@ -216,7 +255,7 @@ class FileLinks(FileLink): """ def f(dirname, fnames, included_suffixes=None): result = [] - # begin by figuring out which filenames, if any, + # begin by figuring out which filenames, if any, # are going to be displayed display_fnames = [] for fname in fnames: @@ -224,13 +263,13 @@ class FileLinks(FileLink): (included_suffixes == None or splitext(fname)[1] in included_suffixes)): display_fnames.append(fname) - + if len(display_fnames) == 0: # if there are no filenames to display, don't print anything # (not even the directory name) pass else: - # otherwise print the formatted directory name followed by + # otherwise print the formatted directory name followed by # the formatted filenames dirname_output_line = dirname_output_format % dirname result.append(dirname_output_line) @@ -258,7 +297,7 @@ class FileLinks(FileLink): self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix fp_format = self.url_prefix + '%s/%s' if sep == "\\": - # Working on a platform where the path separator is "\", so + # Working on a platform where the path separator is "\", so # must convert these to "/" for generating a URI def fp_cleaner(fp): # Replace all occurences of backslash ("\") with a forward @@ -267,7 +306,7 @@ class FileLinks(FileLink): return fp.replace('\\','/') else: fp_cleaner = None - + return self._get_display_formatter(dirname_output_format, fname_output_format, fp_format, @@ -280,11 +319,11 @@ class FileLinks(FileLink): dirname_output_format = "%s/" fname_output_format = spacer + "%s" fp_format = '%s/%s' - + return self._get_display_formatter(dirname_output_format, fname_output_format, fp_format) - + def _format_path(self): result_lines = [] walked_dir = list(walk(self.path)) @@ -292,7 +331,7 @@ class FileLinks(FileLink): for dirname, subdirs, fnames in walked_dir: result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes) return '\n'.join(result_lines) - + def __repr__(self): """return newline-separated absolute paths """