Show More
@@ -1,220 +1,215 b'' | |||||
1 | """Various display related classes. |
|
1 | """Various display related classes. | |
2 |
|
2 | |||
3 | Authors : MinRK, gregcaporaso |
|
3 | Authors : MinRK, gregcaporaso | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | from os.path import exists, isfile, splitext, abspath, join, isdir, walk |
|
6 | from os.path import exists, isfile, splitext, abspath, join, isdir, walk | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | class YouTubeVideo(object): |
|
9 | class YouTubeVideo(object): | |
10 | """Class for embedding a YouTube Video in an IPython session, based on its video id. |
|
10 | """Class for embedding a YouTube Video in an IPython session, based on its video id. | |
11 |
|
11 | |||
12 | e.g. to embed the video on this page: |
|
12 | e.g. to embed the video on this page: | |
13 |
|
13 | |||
14 | http://www.youtube.com/watch?v=foo |
|
14 | http://www.youtube.com/watch?v=foo | |
15 |
|
15 | |||
16 | you would do: |
|
16 | you would do: | |
17 |
|
17 | |||
18 | vid = YouTubeVideo("foo") |
|
18 | vid = YouTubeVideo("foo") | |
19 | display(vid) |
|
19 | display(vid) | |
20 | """ |
|
20 | """ | |
21 |
|
21 | |||
22 | def __init__(self, id, width=400, height=300): |
|
22 | def __init__(self, id, width=400, height=300): | |
23 | self.id = id |
|
23 | self.id = id | |
24 | self.width = width |
|
24 | self.width = width | |
25 | self.height = height |
|
25 | self.height = height | |
26 |
|
26 | |||
27 | def _repr_html_(self): |
|
27 | def _repr_html_(self): | |
28 | """return YouTube embed iframe for this video id""" |
|
28 | """return YouTube embed iframe for this video id""" | |
29 | return """ |
|
29 | return """ | |
30 | <iframe |
|
30 | <iframe | |
31 | width="%i" |
|
31 | width="%i" | |
32 | height="%i" |
|
32 | height="%i" | |
33 | src="http://www.youtube.com/embed/%s" |
|
33 | src="http://www.youtube.com/embed/%s" | |
34 | frameborder="0" |
|
34 | frameborder="0" | |
35 | allowfullscreen |
|
35 | allowfullscreen | |
36 | ></iframe> |
|
36 | ></iframe> | |
37 | """%(self.width, self.height, self.id) |
|
37 | """%(self.width, self.height, self.id) | |
38 |
|
38 | |||
39 | class FileLink(object): |
|
39 | class FileLink(object): | |
40 | """Class for embedding a local file link in an IPython session, based on path |
|
40 | """Class for embedding a local file link in an IPython session, based on path | |
41 |
|
41 | |||
42 | e.g. to embed a link that was generated in the IPython notebook as my/data.txt |
|
42 | e.g. to embed a link that was generated in the IPython notebook as my/data.txt | |
43 |
|
43 | |||
44 | you would do: |
|
44 | you would do: | |
45 |
|
45 | |||
46 | local_file = FileLink("my/data.txt") |
|
46 | local_file = FileLink("my/data.txt") | |
47 | display(local_file) |
|
47 | display(local_file) | |
48 |
|
48 | |||
49 | or in the HTML notebook, just |
|
49 | or in the HTML notebook, just | |
50 |
|
50 | |||
51 | FileLink("my/data.txt") |
|
51 | FileLink("my/data.txt") | |
52 | """ |
|
52 | """ | |
53 |
|
53 | |||
54 | html_link_str = "<a href='%s' target='_blank'>%s</a>" |
|
54 | html_link_str = "<a href='%s' target='_blank'>%s</a>" | |
55 |
|
55 | |||
56 | def __init__(self, |
|
56 | def __init__(self, | |
57 | path, |
|
57 | path, | |
58 | url_prefix='files/', |
|
58 | url_prefix='files/', | |
59 | result_html_prefix='', |
|
59 | result_html_prefix='', | |
60 | result_html_suffix='<br>'): |
|
60 | result_html_suffix='<br>'): | |
61 | """ |
|
61 | """ | |
62 | path : path to the file or directory that should be formatted |
|
62 | path : path to the file or directory that should be formatted | |
63 | directory_prefix : prefix to be prepended to all files to form a |
|
63 | directory_prefix : prefix to be prepended to all files to form a | |
64 | working link [default: 'files'] |
|
64 | working link [default: 'files'] | |
65 | result_html_prefix : text to append to beginning to link |
|
65 | result_html_prefix : text to append to beginning to link | |
66 | [default: none] |
|
66 | [default: none] | |
67 | result_html_suffix : text to append at the end of link |
|
67 | result_html_suffix : text to append at the end of link | |
68 | [default: '<br>'] |
|
68 | [default: '<br>'] | |
69 | """ |
|
69 | """ | |
70 | self.path = path |
|
70 | self.path = path | |
71 | self.url_prefix = url_prefix |
|
71 | self.url_prefix = url_prefix | |
72 | self.result_html_prefix = result_html_prefix |
|
72 | self.result_html_prefix = result_html_prefix | |
73 | self.result_html_suffix = result_html_suffix |
|
73 | self.result_html_suffix = result_html_suffix | |
74 |
|
74 | |||
75 | def _format_path(self): |
|
75 | def _format_path(self): | |
76 | fp = ''.join([self.url_prefix,self.path]) |
|
76 | fp = ''.join([self.url_prefix,self.path]) | |
77 | return ''.join([self.result_html_prefix, |
|
77 | return ''.join([self.result_html_prefix, | |
78 | self.html_link_str % (fp, self.path), |
|
78 | self.html_link_str % (fp, self.path), | |
79 | self.result_html_suffix]) |
|
79 | self.result_html_suffix]) | |
80 |
|
80 | |||
81 | def _repr_html_(self): |
|
81 | def _repr_html_(self): | |
82 | """return html link to file |
|
82 | """return html link to file | |
83 | """ |
|
83 | """ | |
84 | if not exists(self.path): |
|
84 | if not exists(self.path): | |
85 | return ("Path (<tt>%s</tt>) doesn't exist. " |
|
85 | return ("Path (<tt>%s</tt>) doesn't exist. " | |
86 | "It may still be in the process of " |
|
86 | "It may still be in the process of " | |
87 | "being generated, or you may have the " |
|
87 | "being generated, or you may have the " | |
88 | "incorrect path." % self.path) |
|
88 | "incorrect path." % self.path) | |
89 |
|
89 | |||
90 | return self._format_path() |
|
90 | return self._format_path() | |
91 |
|
91 | |||
92 | def __repr__(self): |
|
92 | def __repr__(self): | |
93 | """return absolute path to file |
|
93 | """return absolute path to file | |
94 | """ |
|
94 | """ | |
95 | return abspath(self.path) |
|
95 | return abspath(self.path) | |
96 |
|
96 | |||
97 | # Create an alias for formatting a single directory name as a link. |
|
97 | # Create an alias for formatting a single directory name as a link. | |
98 | # Right now this is the same as a formatting for a single file, but |
|
98 | # Right now this is the same as a formatting for a single file, but | |
99 | # we'll encourage users to reference these with a different class in |
|
99 | # we'll encourage users to reference these with a different class in | |
100 | # case we want to change this in the future. |
|
100 | # case we want to change this in the future. | |
101 | DirectoryLink = FileLink |
|
101 | DirectoryLink = FileLink | |
102 |
|
102 | |||
103 | class FileLinks(FileLink): |
|
103 | class FileLinks(FileLink): | |
104 | """Class for embedding local file links in an IPython session, based on path |
|
104 | """Class for embedding local file links in an IPython session, based on path | |
105 |
|
105 | |||
106 | e.g. to embed links to files that were generated in the IPython notebook under my/data |
|
106 | e.g. to embed links to files that were generated in the IPython notebook under my/data | |
107 |
|
107 | |||
108 | you would do: |
|
108 | you would do: | |
109 |
|
109 | |||
110 | local_files = FileLinks("my/data") |
|
110 | local_files = FileLinks("my/data") | |
111 | display(local_files) |
|
111 | display(local_files) | |
112 |
|
112 | |||
113 | or in the HTML notebook, just |
|
113 | or in the HTML notebook, just | |
114 |
|
114 | |||
115 | FileLinks("my/data") |
|
115 | FileLinks("my/data") | |
116 |
|
116 | |||
117 | """ |
|
117 | """ | |
118 | def __init__(self, |
|
118 | def __init__(self, | |
119 | path, |
|
119 | path, | |
120 | url_prefix='files/', |
|
120 | url_prefix='files/', | |
121 | included_suffixes=None, |
|
121 | included_suffixes=None, | |
122 | result_html_prefix='', |
|
122 | result_html_prefix='', | |
123 | result_html_suffix='<br>', |
|
123 | result_html_suffix='<br>', | |
124 | notebook_display_formatter=None, |
|
124 | notebook_display_formatter=None, | |
125 | terminal_display_formatter=None): |
|
125 | terminal_display_formatter=None): | |
126 | """ |
|
126 | """ | |
127 | included_suffixes : list of filename suffixes to include when |
|
127 | included_suffixes : list of filename suffixes to include when | |
128 | formatting output [default: include all files] |
|
128 | formatting output [default: include all files] | |
129 |
|
129 | |||
130 | See the FileLink (baseclass of LocalDirectory) docstring for |
|
130 | See the FileLink (baseclass of LocalDirectory) docstring for | |
131 | information on additional parameters. |
|
131 | information on additional parameters. | |
132 | """ |
|
132 | """ | |
133 | self.included_suffixes = included_suffixes |
|
133 | self.included_suffixes = included_suffixes | |
134 |
|
134 | |||
135 | self.notebook_display_formatter = \ |
|
|||
136 | self._get_notebook_display_formatter(url_prefix, |
|
|||
137 | result_html_prefix, |
|
|||
138 | result_html_suffix, |
|
|||
139 | included_suffixes) |
|
|||
140 | self.terminal_display_formatter = \ |
|
|||
141 | self._get_terminal_display_formatter(url_prefix, |
|
|||
142 | result_html_prefix, |
|
|||
143 | result_html_suffix, |
|
|||
144 | included_suffixes) |
|
|||
145 | FileLink.__init__(self, |
|
135 | FileLink.__init__(self, | |
146 | path, |
|
136 | path, | |
147 | url_prefix, |
|
137 | url_prefix, | |
148 | result_html_prefix, |
|
138 | result_html_prefix, | |
149 | result_html_suffix) |
|
139 | result_html_suffix) | |
150 |
|
140 | |||
151 |
|
|
141 | self.notebook_display_formatter = \ | |
152 | url_prefix, |
|
142 | self._get_notebook_display_formatter() | |
153 | result_html_prefix, |
|
143 | self.terminal_display_formatter = \ | |
154 | result_html_suffix, |
|
144 | self._get_terminal_display_formatter() | |
155 | included_suffixes): |
|
145 | ||
156 | """ """ |
|
146 | def _get_display_formatter(self, | |
|
147 | dirname_output_format, | |||
|
148 | fname_output_format, | |||
|
149 | fp_format): | |||
|
150 | ||||
|
151 | included_suffixes = self.included_suffixes | |||
|
152 | ||||
157 | def f(output_lines, dirname, fnames): |
|
153 | def f(output_lines, dirname, fnames): | |
158 | """ """ |
|
154 | """ """ | |
159 | # begin by figuring out which filenames, if any, |
|
155 | # begin by figuring out which filenames, if any, | |
160 | # are going to be displayed |
|
156 | # are going to be displayed | |
161 | display_fnames = [] |
|
157 | display_fnames = [] | |
162 | for fname in fnames: |
|
158 | for fname in fnames: | |
163 | if (isfile(join(dirname,fname)) and |
|
159 | if (isfile(join(dirname,fname)) and | |
164 | (included_suffixes == None or |
|
160 | (included_suffixes == None or | |
165 | splitext(fname)[1] in included_suffixes)): |
|
161 | splitext(fname)[1] in included_suffixes)): | |
166 | display_fnames.append(fname) |
|
162 | display_fnames.append(fname) | |
167 |
|
|
163 | ||
168 | if len(display_fnames) == 0: |
|
164 | if len(display_fnames) == 0: | |
169 | pass |
|
165 | pass | |
170 | else: |
|
166 | else: | |
171 | output_lines.append(''.join([result_html_prefix, |
|
167 | dirname_output_line = dirname_output_format % dirname | |
172 | dirname, |
|
168 | output_lines.append(dirname_output_line) | |
173 | result_html_suffix])) |
|
|||
174 | for fname in display_fnames: |
|
169 | for fname in display_fnames: | |
175 |
fp = |
|
170 | fp = fp_format % (dirname,fname) | |
176 | output_lines.append(''.join([self.result_html_prefix, |
|
171 | try: | |
177 | ' ', |
|
172 | # output can include both a filepath and a filename... | |
178 | self.html_link_str % (fp,fname), |
|
173 | fname_output_line = fname_output_format % (fp, fname) | |
179 | self.result_html_suffix])) |
|
174 | except TypeError: | |
|
175 | # ... or just a single filepath | |||
|
176 | fname_output_line = fname_output_format % fname | |||
|
177 | output_lines.append(fname_output_line) | |||
180 | return |
|
178 | return | |
181 | return f |
|
179 | return f | |
182 |
|
180 | |||
183 |
def _get_ |
|
181 | def _get_notebook_display_formatter(self, | |
184 |
|
|
182 | spacer=" "): | |
185 | result_html_prefix, |
|
|||
186 | result_html_suffix, |
|
|||
187 | included_suffixes): |
|
|||
188 | """ """ |
|
183 | """ """ | |
189 | def f(output_lines, dirname, fnames): |
|
184 | dirname_output_format = \ | |
|
185 | self.result_html_prefix + "%s" + self.result_html_suffix | |||
|
186 | fname_output_format = \ | |||
|
187 | self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix | |||
|
188 | fp_format = self.url_prefix + '%s/%s' | |||
|
189 | ||||
|
190 | return self._get_display_formatter(dirname_output_format, | |||
|
191 | fname_output_format, | |||
|
192 | fp_format) | |||
|
193 | ||||
|
194 | def _get_terminal_display_formatter(self, | |||
|
195 | spacer=" "): | |||
190 |
|
|
196 | """ """ | |
191 | # begin by figuring out which filenames, if any, |
|
197 | dirname_output_format = "%s" | |
192 | # are going to be displayed |
|
198 | fname_output_format = spacer + "%s" | |
193 | display_fnames = [] |
|
199 | fp_format = '%s/%s' | |
194 | for fname in fnames: |
|
|||
195 | if (isfile(join(dirname,fname)) and |
|
|||
196 | (included_suffixes == None or |
|
|||
197 | splitext(fname)[1] in included_suffixes)): |
|
|||
198 | display_fnames.append(fname) |
|
|||
199 |
|
200 | |||
200 | if len(display_fnames) == 0: |
|
201 | return self._get_display_formatter(dirname_output_format, | |
201 | pass |
|
202 | fname_output_format, | |
202 | else: |
|
203 | fp_format) | |
203 | output_lines.append(dirname) |
|
|||
204 | for fname in display_fnames: |
|
|||
205 | fp = abspath(join(dirname,fname)) |
|
|||
206 | output_lines.append(' %s' % fp) |
|
|||
207 | return |
|
|||
208 | return f |
|
|||
209 |
|
204 | |||
210 | def _format_path(self): |
|
205 | def _format_path(self): | |
211 | result_lines = [] |
|
206 | result_lines = [] | |
212 | walk(self.path, self.notebook_display_formatter, result_lines) |
|
207 | walk(self.path, self.notebook_display_formatter, result_lines) | |
213 | return '\n'.join(result_lines) |
|
208 | return '\n'.join(result_lines) | |
214 |
|
209 | |||
215 | def __repr__(self): |
|
210 | def __repr__(self): | |
216 | """return newline-separated absolute paths |
|
211 | """return newline-separated absolute paths | |
217 | """ |
|
212 | """ | |
218 | result_lines = [] |
|
213 | result_lines = [] | |
219 | walk(self.path, self.terminal_display_formatter, result_lines) |
|
214 | walk(self.path, self.terminal_display_formatter, result_lines) | |
220 | return '\n'.join(result_lines) No newline at end of file |
|
215 | return '\n'.join(result_lines) |
General Comments 0
You need to be logged in to leave comments.
Login now