##// END OF EJS Templates
Working versions of formatters for files and directories that are generated within an IPython notebook session. This is useful for integrating tools like QIIME with the IPython notebook as it allows users to generate QIIME's HTML visualizations and open them in new pages in their the web browser.
Greg Caporaso -
Show More
@@ -1,8 +1,13 b''
1 """Various display related classes.
1 """Various display related classes.
2
2
3 Authors : MinRK
3 Authors : MinRK, gregcaporaso
4 """
4 """
5
5
6 from IPython.display import display, HTML
7 from os import walk
8 from os.path import join, exists, isfile, splitext
9
10
6 class YouTubeVideo(object):
11 class YouTubeVideo(object):
7 """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.
8
13
@@ -33,3 +38,100 b' class YouTubeVideo(object):'
33 ></iframe>
38 ></iframe>
34 """%(self.width, self.height, self.id)
39 """%(self.width, self.height, self.id)
35
40
41 class LocalFile(object):
42 """Class for embedding a local file link in an IPython session, based on path
43
44 e.g. to embed a link that was generated in the IPython notebook as my/data.txt
45
46 you would do:
47
48 local_file = LocalFile("my/data.txt")
49 display(local_file)
50 """
51
52 def __init__(self,
53 path,
54 _directory_prefix='files',
55 _result_html_prefix='',
56 _result_html_suffix='<br>'):
57 """
58 path : path to the file or directory that should be formatted
59 directory_prefix : prefix to be prepended to all files to form a
60 working link [default: 'files']
61 result_html_prefix : text to append to beginning to link
62 [default: none]
63 result_html_suffix : text to append at the end of link
64 [default: '<br>']
65 """
66 self.path = path
67 self._directory_prefix = _directory_prefix
68 self._link_str = "<a href='%s' target='_blank'>%s</a>"
69 self._result_html_prefix = _result_html_prefix
70 self._result_html_suffix = _result_html_suffix
71
72 def _format_path(self):
73 link = join(self._directory_prefix,self.path)
74 return ''.join([self._result_html_prefix,
75 link,
76 self._result_html_suffix])
77
78 def _repr_html_(self):
79 """return link to local file
80 """
81 if not exists(self.path):
82 return ("Path (<tt>%s</tt>) doesn't exist. "
83 "It may still be in the process of "
84 "being generated, or you may have the "
85 "incorrect path." % self.path)
86
87 return self._format_path()
88
89 # 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
91 # we'll encorage users to reference these with a different class in
92 # case we want to change this in the future.
93 LocalDirectory = LocalFile
94
95 class LocalFiles(LocalFile):
96 """Class for embedding local file links in an IPython session, based on path
97
98 e.g. to embed links to files that were generated in the IPython notebook under my/data
99
100 you would do:
101
102 local_files = LocalFiles("my/data")
103 display(local_files)
104 """
105 def __init__(self,
106 path,
107 _directory_prefix='files',
108 _included_suffixes=None,
109 _result_html_prefix='',
110 _result_html_suffix='<br>'):
111 """
112 included_suffixes : list of filename suffixes to include when
113 formatting output [default: include all files]
114
115 See the LocalFile (baseclass of LocalDirectory) docstring for
116 information on additional parameters.
117 """
118 self._included_suffixes = _included_suffixes
119 LocalFile.__init__(self,
120 path,
121 _directory_prefix,
122 _result_html_prefix,
123 _result_html_suffix)
124
125 def _format_path(self):
126 result_entries = []
127 for root, dirs, files in walk(self.path):
128 for fn in files:
129 fp = join(self._directory_prefix,root,fn)
130 # if all files are being included, or fp has a suffix
131 # that is in included_suffix, create a link to fp
132 if self._included_suffixes == None or \
133 splitext(fn)[1] in self._included_suffixes:
134 result_entries.append(''.join([self._result_html_prefix,
135 self._link_str % (fp,fn),
136 self._result_html_suffix]))
137 return '\n'.join(result_entries)
General Comments 0
You need to be logged in to leave comments. Login now