Show More
@@ -1,145 +1,145 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 join, exists, isfile, splitext |
|
7 | from os.path import join, exists, isfile, splitext | |
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 | def __init__(self, |
|
55 | def __init__(self, | |
56 | path, |
|
56 | path, | |
57 |
|
|
57 | url_prefix='files', | |
58 | _result_html_prefix='', |
|
58 | _result_html_prefix='', | |
59 | _result_html_suffix='<br>'): |
|
59 | _result_html_suffix='<br>'): | |
60 | """ |
|
60 | """ | |
61 | path : path to the file or directory that should be formatted |
|
61 | path : path to the file or directory that should be formatted | |
62 | directory_prefix : prefix to be prepended to all files to form a |
|
62 | directory_prefix : prefix to be prepended to all files to form a | |
63 | working link [default: 'files'] |
|
63 | working link [default: 'files'] | |
64 | result_html_prefix : text to append to beginning to link |
|
64 | result_html_prefix : text to append to beginning to link | |
65 | [default: none] |
|
65 | [default: none] | |
66 | result_html_suffix : text to append at the end of link |
|
66 | result_html_suffix : text to append at the end of link | |
67 | [default: '<br>'] |
|
67 | [default: '<br>'] | |
68 | """ |
|
68 | """ | |
69 | self.path = path |
|
69 | self.path = path | |
70 |
self. |
|
70 | self.url_prefix = url_prefix | |
71 | self._link_str = "<a href='%s' target='_blank'>%s</a>" |
|
71 | self._link_str = "<a href='%s' target='_blank'>%s</a>" | |
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. |
|
76 | fp = join(self.url_prefix,self.path) | |
77 | return ''.join([self._result_html_prefix, |
|
77 | return ''.join([self._result_html_prefix, | |
78 | self._link_str % (fp, self.path), |
|
78 | self._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 link to local file |
|
82 | """return link to local 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 | # Create an alias for formatting a single directory name as a link. |
|
92 | # Create an alias for formatting a single directory name as a link. | |
93 | # Right now this is the same as a formatting for a single file, but |
|
93 | # Right now this is the same as a formatting for a single file, but | |
94 | # we'll encorage users to reference these with a different class in |
|
94 | # we'll encorage users to reference these with a different class in | |
95 | # case we want to change this in the future. |
|
95 | # case we want to change this in the future. | |
96 | DirectoryLink = FileLink |
|
96 | DirectoryLink = FileLink | |
97 |
|
97 | |||
98 | class FileLinks(FileLink): |
|
98 | class FileLinks(FileLink): | |
99 | """Class for embedding local file links in an IPython session, based on path |
|
99 | """Class for embedding local file links in an IPython session, based on path | |
100 |
|
100 | |||
101 | e.g. to embed links to files that were generated in the IPython notebook under my/data |
|
101 | e.g. to embed links to files that were generated in the IPython notebook under my/data | |
102 |
|
102 | |||
103 | you would do: |
|
103 | you would do: | |
104 |
|
104 | |||
105 | local_files = FileLinks("my/data") |
|
105 | local_files = FileLinks("my/data") | |
106 | display(local_files) |
|
106 | display(local_files) | |
107 |
|
107 | |||
108 | or in the HTML notebook, just |
|
108 | or in the HTML notebook, just | |
109 |
|
109 | |||
110 | FileLinks("my/data") |
|
110 | FileLinks("my/data") | |
111 |
|
111 | |||
112 | """ |
|
112 | """ | |
113 | def __init__(self, |
|
113 | def __init__(self, | |
114 | path, |
|
114 | path, | |
115 |
|
|
115 | url_prefix='files', | |
116 | _included_suffixes=None, |
|
116 | _included_suffixes=None, | |
117 | _result_html_prefix='', |
|
117 | _result_html_prefix='', | |
118 | _result_html_suffix='<br>'): |
|
118 | _result_html_suffix='<br>'): | |
119 | """ |
|
119 | """ | |
120 | included_suffixes : list of filename suffixes to include when |
|
120 | included_suffixes : list of filename suffixes to include when | |
121 | formatting output [default: include all files] |
|
121 | formatting output [default: include all files] | |
122 |
|
122 | |||
123 | See the FileLink (baseclass of LocalDirectory) docstring for |
|
123 | See the FileLink (baseclass of LocalDirectory) docstring for | |
124 | information on additional parameters. |
|
124 | information on additional parameters. | |
125 | """ |
|
125 | """ | |
126 | self._included_suffixes = _included_suffixes |
|
126 | self._included_suffixes = _included_suffixes | |
127 | FileLink.__init__(self, |
|
127 | FileLink.__init__(self, | |
128 | path, |
|
128 | path, | |
129 |
|
|
129 | url_prefix, | |
130 | _result_html_prefix, |
|
130 | _result_html_prefix, | |
131 | _result_html_suffix) |
|
131 | _result_html_suffix) | |
132 |
|
132 | |||
133 | def _format_path(self): |
|
133 | def _format_path(self): | |
134 | result_entries = [] |
|
134 | result_entries = [] | |
135 | for root, dirs, files in walk(self.path): |
|
135 | for root, dirs, files in walk(self.path): | |
136 | for fn in files: |
|
136 | for fn in files: | |
137 |
fp = join(self. |
|
137 | fp = join(self.url_prefix,root,fn) | |
138 | # if all files are being included, or fp has a suffix |
|
138 | # if all files are being included, or fp has a suffix | |
139 | # that is in included_suffix, create a link to fp |
|
139 | # that is in included_suffix, create a link to fp | |
140 | if self._included_suffixes == None or \ |
|
140 | if self._included_suffixes == None or \ | |
141 | splitext(fn)[1] in self._included_suffixes: |
|
141 | splitext(fn)[1] in self._included_suffixes: | |
142 | result_entries.append(''.join([self._result_html_prefix, |
|
142 | result_entries.append(''.join([self._result_html_prefix, | |
143 | self._link_str % (fp,fn), |
|
143 | self._link_str % (fp,fn), | |
144 | self._result_html_suffix])) |
|
144 | self._result_html_suffix])) | |
145 | return '\n'.join(result_entries) |
|
145 | return '\n'.join(result_entries) |
General Comments 0
You need to be logged in to leave comments.
Login now