Show More
@@ -1,151 +1,165 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 import walk |
|
6 | from os import walk | |
7 | from os.path import exists, isfile, splitext, abspath |
|
7 | from os.path import exists, isfile, splitext, abspath, join | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | class YouTubeVideo(object): |
|
10 | class YouTubeVideo(object): | |
11 | """Class for embedding a YouTube Video in an IPython session, based on its video id. |
|
11 | """Class for embedding a YouTube Video in an IPython session, based on its video id. | |
12 |
|
12 | |||
13 | e.g. to embed the video on this page: |
|
13 | e.g. to embed the video on this page: | |
14 |
|
14 | |||
15 | http://www.youtube.com/watch?v=foo |
|
15 | http://www.youtube.com/watch?v=foo | |
16 |
|
16 | |||
17 | you would do: |
|
17 | you would do: | |
18 |
|
18 | |||
19 | vid = YouTubeVideo("foo") |
|
19 | vid = YouTubeVideo("foo") | |
20 | display(vid) |
|
20 | display(vid) | |
21 | """ |
|
21 | """ | |
22 |
|
22 | |||
23 | def __init__(self, id, width=400, height=300): |
|
23 | def __init__(self, id, width=400, height=300): | |
24 | self.id = id |
|
24 | self.id = id | |
25 | self.width = width |
|
25 | self.width = width | |
26 | self.height = height |
|
26 | self.height = height | |
27 |
|
27 | |||
28 | def _repr_html_(self): |
|
28 | def _repr_html_(self): | |
29 | """return YouTube embed iframe for this video id""" |
|
29 | """return YouTube embed iframe for this video id""" | |
30 | return """ |
|
30 | return """ | |
31 | <iframe |
|
31 | <iframe | |
32 | width="%i" |
|
32 | width="%i" | |
33 | height="%i" |
|
33 | height="%i" | |
34 | src="http://www.youtube.com/embed/%s" |
|
34 | src="http://www.youtube.com/embed/%s" | |
35 | frameborder="0" |
|
35 | frameborder="0" | |
36 | allowfullscreen |
|
36 | allowfullscreen | |
37 | ></iframe> |
|
37 | ></iframe> | |
38 | """%(self.width, self.height, self.id) |
|
38 | """%(self.width, self.height, self.id) | |
39 |
|
39 | |||
40 | class FileLink(object): |
|
40 | class FileLink(object): | |
41 | """Class for embedding a local file link in an IPython session, based on path |
|
41 | """Class for embedding a local file link in an IPython session, based on path | |
42 |
|
42 | |||
43 | e.g. to embed a link that was generated in the IPython notebook as my/data.txt |
|
43 | e.g. to embed a link that was generated in the IPython notebook as my/data.txt | |
44 |
|
44 | |||
45 | you would do: |
|
45 | you would do: | |
46 |
|
46 | |||
47 | local_file = FileLink("my/data.txt") |
|
47 | local_file = FileLink("my/data.txt") | |
48 | display(local_file) |
|
48 | display(local_file) | |
49 |
|
49 | |||
50 | or in the HTML notebook, just |
|
50 | or in the HTML notebook, just | |
51 |
|
51 | |||
52 | FileLink("my/data.txt") |
|
52 | FileLink("my/data.txt") | |
53 | """ |
|
53 | """ | |
54 |
|
54 | |||
55 | html_link_str = "<a href='%s' target='_blank'>%s</a>" |
|
55 | html_link_str = "<a href='%s' target='_blank'>%s</a>" | |
56 |
|
56 | |||
57 | def __init__(self, |
|
57 | def __init__(self, | |
58 | path, |
|
58 | path, | |
59 | url_prefix='files', |
|
59 | url_prefix='files', | |
60 | result_html_prefix='', |
|
60 | result_html_prefix='', | |
61 | result_html_suffix='<br>'): |
|
61 | result_html_suffix='<br>'): | |
62 | """ |
|
62 | """ | |
63 | path : path to the file or directory that should be formatted |
|
63 | path : path to the file or directory that should be formatted | |
64 | directory_prefix : prefix to be prepended to all files to form a |
|
64 | directory_prefix : prefix to be prepended to all files to form a | |
65 | working link [default: 'files'] |
|
65 | working link [default: 'files'] | |
66 | result_html_prefix : text to append to beginning to link |
|
66 | result_html_prefix : text to append to beginning to link | |
67 | [default: none] |
|
67 | [default: none] | |
68 | result_html_suffix : text to append at the end of link |
|
68 | result_html_suffix : text to append at the end of link | |
69 | [default: '<br>'] |
|
69 | [default: '<br>'] | |
70 | """ |
|
70 | """ | |
71 | self.path = path |
|
71 | self.path = path | |
72 | self.url_prefix = url_prefix |
|
72 | self.url_prefix = url_prefix | |
73 | self.result_html_prefix = result_html_prefix |
|
73 | self.result_html_prefix = result_html_prefix | |
74 | self.result_html_suffix = result_html_suffix |
|
74 | self.result_html_suffix = result_html_suffix | |
75 |
|
75 | |||
76 | def _format_path(self): |
|
76 | def _format_path(self): | |
77 | fp = '/'.join([self.url_prefix,self.path]) |
|
77 | fp = '/'.join([self.url_prefix,self.path]) | |
78 | return ''.join([self.result_html_prefix, |
|
78 | return ''.join([self.result_html_prefix, | |
79 | self.html_link_str % (fp, self.path), |
|
79 | self.html_link_str % (fp, self.path), | |
80 | self.result_html_suffix]) |
|
80 | self.result_html_suffix]) | |
81 |
|
81 | |||
82 | def _repr_html_(self): |
|
82 | def _repr_html_(self): | |
83 | """return html link to file |
|
83 | """return html link to file | |
84 | """ |
|
84 | """ | |
85 | if not exists(self.path): |
|
85 | if not exists(self.path): | |
86 | return ("Path (<tt>%s</tt>) doesn't exist. " |
|
86 | return ("Path (<tt>%s</tt>) doesn't exist. " | |
87 | "It may still be in the process of " |
|
87 | "It may still be in the process of " | |
88 | "being generated, or you may have the " |
|
88 | "being generated, or you may have the " | |
89 | "incorrect path." % self.path) |
|
89 | "incorrect path." % self.path) | |
90 |
|
90 | |||
91 | return self._format_path() |
|
91 | return self._format_path() | |
92 |
|
92 | |||
93 | def __repr__(self): |
|
93 | def __repr__(self): | |
94 | """return path to file |
|
94 | """return absolute path to file | |
95 | """ |
|
95 | """ | |
96 | return abspath(self.path) |
|
96 | return abspath(self.path) | |
97 |
|
97 | |||
98 | # Create an alias for formatting a single directory name as a link. |
|
98 | # Create an alias for formatting a single directory name as a link. | |
99 | # Right now this is the same as a formatting for a single file, but |
|
99 | # Right now this is the same as a formatting for a single file, but | |
100 | # we'll encourage users to reference these with a different class in |
|
100 | # we'll encourage users to reference these with a different class in | |
101 | # case we want to change this in the future. |
|
101 | # case we want to change this in the future. | |
102 | DirectoryLink = FileLink |
|
102 | DirectoryLink = FileLink | |
103 |
|
103 | |||
104 | class FileLinks(FileLink): |
|
104 | class FileLinks(FileLink): | |
105 | """Class for embedding local file links in an IPython session, based on path |
|
105 | """Class for embedding local file links in an IPython session, based on path | |
106 |
|
106 | |||
107 | e.g. to embed links to files that were generated in the IPython notebook under my/data |
|
107 | e.g. to embed links to files that were generated in the IPython notebook under my/data | |
108 |
|
108 | |||
109 | you would do: |
|
109 | you would do: | |
110 |
|
110 | |||
111 | local_files = FileLinks("my/data") |
|
111 | local_files = FileLinks("my/data") | |
112 | display(local_files) |
|
112 | display(local_files) | |
113 |
|
113 | |||
114 | or in the HTML notebook, just |
|
114 | or in the HTML notebook, just | |
115 |
|
115 | |||
116 | FileLinks("my/data") |
|
116 | FileLinks("my/data") | |
117 |
|
117 | |||
118 | """ |
|
118 | """ | |
119 | def __init__(self, |
|
119 | def __init__(self, | |
120 | path, |
|
120 | path, | |
121 | url_prefix='files', |
|
121 | url_prefix='files', | |
122 | included_suffixes=None, |
|
122 | included_suffixes=None, | |
123 | result_html_prefix='', |
|
123 | result_html_prefix='', | |
124 | result_html_suffix='<br>'): |
|
124 | result_html_suffix='<br>'): | |
125 | """ |
|
125 | """ | |
126 | included_suffixes : list of filename suffixes to include when |
|
126 | included_suffixes : list of filename suffixes to include when | |
127 | formatting output [default: include all files] |
|
127 | formatting output [default: include all files] | |
128 |
|
128 | |||
129 | See the FileLink (baseclass of LocalDirectory) docstring for |
|
129 | See the FileLink (baseclass of LocalDirectory) docstring for | |
130 | information on additional parameters. |
|
130 | information on additional parameters. | |
131 | """ |
|
131 | """ | |
132 | self.included_suffixes = included_suffixes |
|
132 | self.included_suffixes = included_suffixes | |
133 | FileLink.__init__(self, |
|
133 | FileLink.__init__(self, | |
134 | path, |
|
134 | path, | |
135 | url_prefix, |
|
135 | url_prefix, | |
136 | result_html_prefix, |
|
136 | result_html_prefix, | |
137 | result_html_suffix) |
|
137 | result_html_suffix) | |
138 |
|
138 | |||
139 | def _format_path(self): |
|
139 | def _format_path(self): | |
140 | result_entries = [] |
|
140 | result_entries = [] | |
141 | for root, dirs, files in walk(self.path): |
|
141 | for root, dirs, files in walk(self.path): | |
142 | for fn in files: |
|
142 | for fn in files: | |
143 | fp = '/'.join([self.url_prefix,root,fn]) |
|
143 | fp = '/'.join([self.url_prefix,root,fn]) | |
144 | # if all files are being included, or fp has a suffix |
|
144 | # if all files are being included, or fp has a suffix | |
145 | # that is in included_suffix, create a link to fp |
|
145 | # that is in included_suffix, create a link to fp | |
146 | if self.included_suffixes == None or \ |
|
146 | if self.included_suffixes == None or \ | |
147 | splitext(fn)[1] in self.included_suffixes: |
|
147 | splitext(fn)[1] in self.included_suffixes: | |
148 | result_entries.append(''.join([self.result_html_prefix, |
|
148 | result_entries.append(''.join([self.result_html_prefix, | |
149 | self.html_link_str % (fp,fn), |
|
149 | self.html_link_str % (fp,fn), | |
150 | self.result_html_suffix])) |
|
150 | self.result_html_suffix])) | |
151 | return '\n'.join(result_entries) |
|
151 | return '\n'.join(result_entries) | |
|
152 | ||||
|
153 | def __repr__(self): | |||
|
154 | """return newline-separated absolute paths | |||
|
155 | """ | |||
|
156 | result_entries = [] | |||
|
157 | for root, dirs, files in walk(self.path): | |||
|
158 | for fn in files: | |||
|
159 | fp = abspath(join(root,fn)) | |||
|
160 | # if all files are being included, or fp has a suffix | |||
|
161 | # that is in included_suffix, create a link to fp | |||
|
162 | if self.included_suffixes == None or \ | |||
|
163 | splitext(fn)[1] in self.included_suffixes: | |||
|
164 | result_entries.append(fp) | |||
|
165 | return '\n'.join(result_entries) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now