##// END OF EJS Templates
Merge pull request #3200 from 3kwa/master...
Min RK -
r10272:2d23963c merge
parent child Browse files
Show More
@@ -6,7 +6,40 b' from os.path import exists, isfile, splitext, abspath, join, isdir'
6 from os import walk, sep
6 from os import walk, sep
7
7
8
8
9 class YouTubeVideo(object):
9 class IFrame(object):
10 """
11 Generic class to embed an iframe in an IPython notebook
12 """
13
14 iframe = """
15 <iframe
16 width="{width}"
17 height={height}"
18 src="{src}{params}"
19 frameborder="0"
20 allowfullscreen
21 ></iframe>
22 """
23
24 def __init__(self, src, width, height, **kwargs):
25 self.src = src
26 self.width = width
27 self.height = height
28 self.params = kwargs
29
30 def _repr_html_(self):
31 """return the embed iframe"""
32 if self.params:
33 from urllib import urlencode
34 params = "?" + urlencode(self.params)
35 else:
36 params = ""
37 return self.iframe.format(src=self.src,
38 width=self.width,
39 height=self.height,
40 params=params)
41
42 class YouTubeVideo(IFrame):
10 """Class for embedding a YouTube Video in an IPython session, based on its video id.
43 """Class for embedding a YouTube Video in an IPython session, based on its video id.
11
44
12 e.g. to embed the video on this page:
45 e.g. to embed the video on this page:
@@ -17,41 +50,47 b' class YouTubeVideo(object):'
17
50
18 vid = YouTubeVideo("foo")
51 vid = YouTubeVideo("foo")
19 display(vid)
52 display(vid)
20
53
21 To start from 30 seconds:
54 To start from 30 seconds:
22
55
23 vid = YouTubeVideo("abc", start=30)
56 vid = YouTubeVideo("abc", start=30)
24 display(vid)
57 display(vid)
25
58
26 To calculate seconds from time as hours, minutes, seconds use:
59 To calculate seconds from time as hours, minutes, seconds use:
27 start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds())
60 start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds())
28
61
29 Other parameters can be provided as documented at
62 Other parameters can be provided as documented at
30 https://developers.google.com/youtube/player_parameters#parameter-subheader
63 https://developers.google.com/youtube/player_parameters#parameter-subheader
31 """
64 """
32
65
33 def __init__(self, id, width=400, height=300, **kwargs):
66 def __init__(self, id, width=400, height=300, **kwargs):
34 self.id = id
67 src = "http://www.youtube.com/embed/{0}".format(id)
35 self.width = width
68 super(YouTubeVideo, self).__init__(src, width, height, **kwargs)
36 self.height = height
37 self.params = kwargs
38
69
39 def _repr_html_(self):
70 class VimeoVideo(IFrame):
40 """return YouTube embed iframe for this video id"""
71 """
41 if self.params:
72 Class for embedding a Vimeo video in an IPython session, based on its video id.
42 from urllib import urlencode # Deferred import
73 """
43 params = "?" + urlencode(self.params)
74
44 else:
75 def __init__(self, id, width=400, height=300, **kwargs):
45 params = ""
76 src="http://player.vimeo.com/video/{0}".format(id)
46 return """
77 super(VimeoVideo, self).__init__(src, width, height, **kwargs)
47 <iframe
78
48 width="%i"
79 class ScribdDocument(IFrame):
49 height="%i"
80 """
50 src="http://www.youtube.com/embed/%s%s"
81 Class for embedding a Scribd document in an IPython session
51 frameborder="0"
82
52 allowfullscreen
83 Use the start_page params to specify a starting point in the document
53 ></iframe>
84 Use the view_mode params to specify display type one off scroll | slideshow | book
54 """ % (self.width, self.height, self.id, params)
85
86 e.g to Display Wes' foundational paper about PANDAS in book mode from page 3
87
88 ScribdDocument(71048089, width=800, height=400, start_page=3, view_mode="book")
89 """
90
91 def __init__(self, id, width=400, height=300, **kwargs):
92 src="http://www.scribd.com/embeds/{0}/content".format(id)
93 super(ScribdDocument, self).__init__(src, width, height, **kwargs)
55
94
56 class FileLink(object):
95 class FileLink(object):
57 """Class for embedding a local file link in an IPython session, based on path
96 """Class for embedding a local file link in an IPython session, based on path
@@ -62,14 +101,14 b' class FileLink(object):'
62
101
63 local_file = FileLink("my/data.txt")
102 local_file = FileLink("my/data.txt")
64 display(local_file)
103 display(local_file)
65
104
66 or in the HTML notebook, just::
105 or in the HTML notebook, just::
67
106
68 FileLink("my/data.txt")
107 FileLink("my/data.txt")
69 """
108 """
70
109
71 html_link_str = "<a href='%s' target='_blank'>%s</a>"
110 html_link_str = "<a href='%s' target='_blank'>%s</a>"
72
111
73 def __init__(self,
112 def __init__(self,
74 path,
113 path,
75 url_prefix='files/',
114 url_prefix='files/',
@@ -85,7 +124,7 b' class FileLink(object):'
85 'files']
124 'files']
86 result_html_prefix : str
125 result_html_prefix : str
87 text to append to beginning to link [default: none]
126 text to append to beginning to link [default: none]
88 result_html_suffix : str
127 result_html_suffix : str
89 text to append at the end of link [default: '<br>']
128 text to append at the end of link [default: '<br>']
90 """
129 """
91 if isdir(path):
130 if isdir(path):
@@ -96,24 +135,24 b' class FileLink(object):'
96 self.url_prefix = url_prefix
135 self.url_prefix = url_prefix
97 self.result_html_prefix = result_html_prefix
136 self.result_html_prefix = result_html_prefix
98 self.result_html_suffix = result_html_suffix
137 self.result_html_suffix = result_html_suffix
99
138
100 def _format_path(self):
139 def _format_path(self):
101 fp = ''.join([self.url_prefix,self.path])
140 fp = ''.join([self.url_prefix,self.path])
102 return ''.join([self.result_html_prefix,
141 return ''.join([self.result_html_prefix,
103 self.html_link_str % (fp, self.path),
142 self.html_link_str % (fp, self.path),
104 self.result_html_suffix])
143 self.result_html_suffix])
105
144
106 def _repr_html_(self):
145 def _repr_html_(self):
107 """return html link to file
146 """return html link to file
108 """
147 """
109 if not exists(self.path):
148 if not exists(self.path):
110 return ("Path (<tt>%s</tt>) doesn't exist. "
149 return ("Path (<tt>%s</tt>) doesn't exist. "
111 "It may still be in the process of "
150 "It may still be in the process of "
112 "being generated, or you may have the "
151 "being generated, or you may have the "
113 "incorrect path." % self.path)
152 "incorrect path." % self.path)
114
153
115 return self._format_path()
154 return self._format_path()
116
155
117 def __repr__(self):
156 def __repr__(self):
118 """return absolute path to file
157 """return absolute path to file
119 """
158 """
@@ -128,11 +167,11 b' class FileLinks(FileLink):'
128
167
129 local_files = FileLinks("my/data")
168 local_files = FileLinks("my/data")
130 display(local_files)
169 display(local_files)
131
170
132 or in the HTML notebook, just
171 or in the HTML notebook, just
133
172
134 FileLinks("my/data")
173 FileLinks("my/data")
135
174
136 """
175 """
137 def __init__(self,
176 def __init__(self,
138 path,
177 path,
@@ -145,34 +184,34 b' class FileLinks(FileLink):'
145 """
184 """
146 included_suffixes : list of filename suffixes to include when
185 included_suffixes : list of filename suffixes to include when
147 formatting output [default: include all files]
186 formatting output [default: include all files]
148
187
149 See the FileLink (baseclass of LocalDirectory) docstring for
188 See the FileLink (baseclass of LocalDirectory) docstring for
150 information on additional parameters.
189 information on additional parameters.
151
190
152 notebook_display_formatter : func used to format links for display
191 notebook_display_formatter : func used to format links for display
153 in the notebook. See discussion of formatter function below.
192 in the notebook. See discussion of formatter function below.
154
193
155 terminal_display_formatter : func used to format links for display
194 terminal_display_formatter : func used to format links for display
156 in the terminal. See discussion of formatter function below.
195 in the terminal. See discussion of formatter function below.
157
196
158
197
159 Passing custom formatter functions
198 Passing custom formatter functions
160 ----------------------------------
199 ----------------------------------
161 Formatter functions must be of the form:
200 Formatter functions must be of the form:
162 f(dirname, fnames, included_suffixes)
201 f(dirname, fnames, included_suffixes)
163 dirname : the name of a directory (a string),
202 dirname : the name of a directory (a string),
164 fnames : a list of the files in that directory
203 fnames : a list of the files in that directory
165 included_suffixes : a list of the file suffixes that should be
204 included_suffixes : a list of the file suffixes that should be
166 included in the output (passing None means
205 included in the output (passing None means
167 to include all suffixes in the output in
206 to include all suffixes in the output in
168 the built-in formatters)
207 the built-in formatters)
169
208
170 returns a list of lines that should will be print in the
209 returns a list of lines that should will be print in the
171 notebook (if passing notebook_display_formatter) or the terminal
210 notebook (if passing notebook_display_formatter) or the terminal
172 (if passing terminal_display_formatter). This function is iterated
211 (if passing terminal_display_formatter). This function is iterated
173 over for each directory in self.path. Default formatters are in
212 over for each directory in self.path. Default formatters are in
174 place, can be passed here to support alternative formatting.
213 place, can be passed here to support alternative formatting.
175
214
176 """
215 """
177 if isfile(path):
216 if isfile(path):
178 raise ValueError,\
217 raise ValueError,\
@@ -181,33 +220,33 b' class FileLinks(FileLink):'
181 self.included_suffixes = included_suffixes
220 self.included_suffixes = included_suffixes
182 # remove trailing slashs for more consistent output formatting
221 # remove trailing slashs for more consistent output formatting
183 path = path.rstrip('/')
222 path = path.rstrip('/')
184
223
185 self.path = path
224 self.path = path
186 self.url_prefix = url_prefix
225 self.url_prefix = url_prefix
187 self.result_html_prefix = result_html_prefix
226 self.result_html_prefix = result_html_prefix
188 self.result_html_suffix = result_html_suffix
227 self.result_html_suffix = result_html_suffix
189
228
190 self.notebook_display_formatter = \
229 self.notebook_display_formatter = \
191 notebook_display_formatter or self._get_notebook_display_formatter()
230 notebook_display_formatter or self._get_notebook_display_formatter()
192 self.terminal_display_formatter = \
231 self.terminal_display_formatter = \
193 terminal_display_formatter or self._get_terminal_display_formatter()
232 terminal_display_formatter or self._get_terminal_display_formatter()
194
233
195 def _get_display_formatter(self,
234 def _get_display_formatter(self,
196 dirname_output_format,
235 dirname_output_format,
197 fname_output_format,
236 fname_output_format,
198 fp_format,
237 fp_format,
199 fp_cleaner=None):
238 fp_cleaner=None):
200 """ generate built-in formatter function
239 """ generate built-in formatter function
201
240
202 this is used to define both the notebook and terminal built-in
241 this is used to define both the notebook and terminal built-in
203 formatters as they only differ by some wrapper text for each entry
242 formatters as they only differ by some wrapper text for each entry
204
243
205 dirname_output_format: string to use for formatting directory
244 dirname_output_format: string to use for formatting directory
206 names, dirname will be substituted for a single "%s" which
245 names, dirname will be substituted for a single "%s" which
207 must appear in this string
246 must appear in this string
208 fname_output_format: string to use for formatting file names,
247 fname_output_format: string to use for formatting file names,
209 if a single "%s" appears in the string, fname will be substituted
248 if a single "%s" appears in the string, fname will be substituted
210 if two "%s" appear in the string, the path to fname will be
249 if two "%s" appear in the string, the path to fname will be
211 substituted for the first and fname will be substituted for the
250 substituted for the first and fname will be substituted for the
212 second
251 second
213 fp_format: string to use for formatting filepaths, must contain
252 fp_format: string to use for formatting filepaths, must contain
@@ -216,7 +255,7 b' class FileLinks(FileLink):'
216 """
255 """
217 def f(dirname, fnames, included_suffixes=None):
256 def f(dirname, fnames, included_suffixes=None):
218 result = []
257 result = []
219 # begin by figuring out which filenames, if any,
258 # begin by figuring out which filenames, if any,
220 # are going to be displayed
259 # are going to be displayed
221 display_fnames = []
260 display_fnames = []
222 for fname in fnames:
261 for fname in fnames:
@@ -224,13 +263,13 b' class FileLinks(FileLink):'
224 (included_suffixes == None or
263 (included_suffixes == None or
225 splitext(fname)[1] in included_suffixes)):
264 splitext(fname)[1] in included_suffixes)):
226 display_fnames.append(fname)
265 display_fnames.append(fname)
227
266
228 if len(display_fnames) == 0:
267 if len(display_fnames) == 0:
229 # if there are no filenames to display, don't print anything
268 # if there are no filenames to display, don't print anything
230 # (not even the directory name)
269 # (not even the directory name)
231 pass
270 pass
232 else:
271 else:
233 # otherwise print the formatted directory name followed by
272 # otherwise print the formatted directory name followed by
234 # the formatted filenames
273 # the formatted filenames
235 dirname_output_line = dirname_output_format % dirname
274 dirname_output_line = dirname_output_format % dirname
236 result.append(dirname_output_line)
275 result.append(dirname_output_line)
@@ -258,7 +297,7 b' class FileLinks(FileLink):'
258 self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix
297 self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix
259 fp_format = self.url_prefix + '%s/%s'
298 fp_format = self.url_prefix + '%s/%s'
260 if sep == "\\":
299 if sep == "\\":
261 # Working on a platform where the path separator is "\", so
300 # Working on a platform where the path separator is "\", so
262 # must convert these to "/" for generating a URI
301 # must convert these to "/" for generating a URI
263 def fp_cleaner(fp):
302 def fp_cleaner(fp):
264 # Replace all occurences of backslash ("\") with a forward
303 # Replace all occurences of backslash ("\") with a forward
@@ -267,7 +306,7 b' class FileLinks(FileLink):'
267 return fp.replace('\\','/')
306 return fp.replace('\\','/')
268 else:
307 else:
269 fp_cleaner = None
308 fp_cleaner = None
270
309
271 return self._get_display_formatter(dirname_output_format,
310 return self._get_display_formatter(dirname_output_format,
272 fname_output_format,
311 fname_output_format,
273 fp_format,
312 fp_format,
@@ -280,11 +319,11 b' class FileLinks(FileLink):'
280 dirname_output_format = "%s/"
319 dirname_output_format = "%s/"
281 fname_output_format = spacer + "%s"
320 fname_output_format = spacer + "%s"
282 fp_format = '%s/%s'
321 fp_format = '%s/%s'
283
322
284 return self._get_display_formatter(dirname_output_format,
323 return self._get_display_formatter(dirname_output_format,
285 fname_output_format,
324 fname_output_format,
286 fp_format)
325 fp_format)
287
326
288 def _format_path(self):
327 def _format_path(self):
289 result_lines = []
328 result_lines = []
290 walked_dir = list(walk(self.path))
329 walked_dir = list(walk(self.path))
@@ -292,7 +331,7 b' class FileLinks(FileLink):'
292 for dirname, subdirs, fnames in walked_dir:
331 for dirname, subdirs, fnames in walked_dir:
293 result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes)
332 result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes)
294 return '\n'.join(result_lines)
333 return '\n'.join(result_lines)
295
334
296 def __repr__(self):
335 def __repr__(self):
297 """return newline-separated absolute paths
336 """return newline-separated absolute paths
298 """
337 """
General Comments 0
You need to be logged in to leave comments. Login now