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