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