display.py
145 lines
| 4.8 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r4408 | """Various display related classes. | ||
Greg Caporaso
|
r8272 | Authors : MinRK, gregcaporaso | ||
Brian E. Granger
|
r4408 | """ | ||
Greg Caporaso
|
r8272 | from os import walk | ||
from os.path import join, exists, isfile, splitext | ||||
Brian E. Granger
|
r4408 | class YouTubeVideo(object): | ||
"""Class for embedding a YouTube Video in an IPython session, based on its video id. | ||||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | e.g. to embed the video on this page: | ||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | http://www.youtube.com/watch?v=foo | ||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | you would do: | ||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | vid = YouTubeVideo("foo") | ||
display(vid) | ||||
""" | ||||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | def __init__(self, id, width=400, height=300): | ||
self.id = id | ||||
self.width = width | ||||
self.height = height | ||||
Bernardo B. Marques
|
r4872 | |||
Brian E. Granger
|
r4408 | def _repr_html_(self): | ||
"""return YouTube embed iframe for this video id""" | ||||
return """ | ||||
Bernardo B. Marques
|
r4872 | <iframe | ||
width="%i" | ||||
height="%i" | ||||
src="http://www.youtube.com/embed/%s" | ||||
frameborder="0" | ||||
Brian E. Granger
|
r4408 | allowfullscreen | ||
></iframe> | ||||
"""%(self.width, self.height, self.id) | ||||
Greg Caporaso
|
r8365 | class FileLink(object): | ||
Greg Caporaso
|
r8272 | """Class for embedding a local file link in an IPython session, based on path | ||
e.g. to embed a link that was generated in the IPython notebook as my/data.txt | ||||
you would do: | ||||
Greg Caporaso
|
r8365 | local_file = FileLink("my/data.txt") | ||
Greg Caporaso
|
r8272 | display(local_file) | ||
Greg Caporaso
|
r8276 | |||
or in the HTML notebook, just | ||||
Greg Caporaso
|
r8365 | FileLink("my/data.txt") | ||
Greg Caporaso
|
r8272 | """ | ||
def __init__(self, | ||||
path, | ||||
_directory_prefix='files', | ||||
_result_html_prefix='', | ||||
_result_html_suffix='<br>'): | ||||
""" | ||||
path : path to the file or directory that should be formatted | ||||
directory_prefix : prefix to be prepended to all files to form a | ||||
working link [default: 'files'] | ||||
result_html_prefix : text to append to beginning to link | ||||
[default: none] | ||||
result_html_suffix : text to append at the end of link | ||||
[default: '<br>'] | ||||
""" | ||||
self.path = path | ||||
self._directory_prefix = _directory_prefix | ||||
self._link_str = "<a href='%s' target='_blank'>%s</a>" | ||||
self._result_html_prefix = _result_html_prefix | ||||
self._result_html_suffix = _result_html_suffix | ||||
def _format_path(self): | ||||
Greg Caporaso
|
r8275 | fp = join(self._directory_prefix,self.path) | ||
Greg Caporaso
|
r8272 | return ''.join([self._result_html_prefix, | ||
Greg Caporaso
|
r8275 | self._link_str % (fp, self.path), | ||
Greg Caporaso
|
r8272 | self._result_html_suffix]) | ||
def _repr_html_(self): | ||||
"""return link to local file | ||||
""" | ||||
if not exists(self.path): | ||||
return ("Path (<tt>%s</tt>) doesn't exist. " | ||||
"It may still be in the process of " | ||||
"being generated, or you may have the " | ||||
"incorrect path." % self.path) | ||||
return self._format_path() | ||||
# Create an alias for formatting a single directory name as a link. | ||||
# Right now this is the same as a formatting for a single file, but | ||||
# we'll encorage users to reference these with a different class in | ||||
# case we want to change this in the future. | ||||
Greg Caporaso
|
r8365 | DirectoryLink = FileLink | ||
Greg Caporaso
|
r8272 | |||
Greg Caporaso
|
r8365 | class FileLinks(FileLink): | ||
Greg Caporaso
|
r8272 | """Class for embedding local file links in an IPython session, based on path | ||
e.g. to embed links to files that were generated in the IPython notebook under my/data | ||||
you would do: | ||||
Greg Caporaso
|
r8365 | local_files = FileLinks("my/data") | ||
Greg Caporaso
|
r8272 | display(local_files) | ||
Greg Caporaso
|
r8276 | |||
or in the HTML notebook, just | ||||
Greg Caporaso
|
r8365 | FileLinks("my/data") | ||
Greg Caporaso
|
r8276 | |||
Greg Caporaso
|
r8272 | """ | ||
def __init__(self, | ||||
path, | ||||
_directory_prefix='files', | ||||
_included_suffixes=None, | ||||
_result_html_prefix='', | ||||
_result_html_suffix='<br>'): | ||||
""" | ||||
included_suffixes : list of filename suffixes to include when | ||||
formatting output [default: include all files] | ||||
Greg Caporaso
|
r8365 | See the FileLink (baseclass of LocalDirectory) docstring for | ||
Greg Caporaso
|
r8272 | information on additional parameters. | ||
""" | ||||
self._included_suffixes = _included_suffixes | ||||
Greg Caporaso
|
r8365 | FileLink.__init__(self, | ||
Greg Caporaso
|
r8272 | path, | ||
_directory_prefix, | ||||
_result_html_prefix, | ||||
_result_html_suffix) | ||||
def _format_path(self): | ||||
result_entries = [] | ||||
for root, dirs, files in walk(self.path): | ||||
for fn in files: | ||||
fp = join(self._directory_prefix,root,fn) | ||||
# if all files are being included, or fp has a suffix | ||||
# that is in included_suffix, create a link to fp | ||||
if self._included_suffixes == None or \ | ||||
splitext(fn)[1] in self._included_suffixes: | ||||
result_entries.append(''.join([self._result_html_prefix, | ||||
self._link_str % (fp,fn), | ||||
self._result_html_suffix])) | ||||
return '\n'.join(result_entries) | ||||