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