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