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