##// END OF EJS Templates
fixed bug in test and in support for passing alternative formatters
fixed bug in test and in support for passing alternative formatters

File last commit:

r8626:0a8c28c2
r8626:0a8c28c2
Show More
display.py
215 lines | 7.3 KiB | text/x-python | PythonLexer
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 """Various display related classes.
Greg Caporaso
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.
r8272 Authors : MinRK, gregcaporaso
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 """
Greg Caporaso
improved formatting for FileLinks
r8619 from os.path import exists, isfile, splitext, abspath, join, isdir, walk
Greg Caporaso
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.
r8272
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 class YouTubeVideo(object):
"""Class for embedding a YouTube Video in an IPython session, based on its video id.
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 e.g. to embed the video on this page:
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 http://www.youtube.com/watch?v=foo
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 you would do:
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 vid = YouTubeVideo("foo")
display(vid)
"""
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 def __init__(self, id, width=400, height=300):
self.id = id
self.width = width
self.height = height
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 def _repr_html_(self):
"""return YouTube embed iframe for this video id"""
return """
Bernardo B. Marques
remove all trailling spaces
r4872 <iframe
width="%i"
height="%i"
src="http://www.youtube.com/embed/%s"
frameborder="0"
Brian E. Granger
Added lib/display.py for extra display related things.
r4408 allowfullscreen
></iframe>
"""%(self.width, self.height, self.id)
Greg Caporaso
renamed classes as recommended by @takluyver
r8365 class FileLink(object):
Greg Caporaso
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.
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
renamed classes as recommended by @takluyver
r8365 local_file = FileLink("my/data.txt")
Greg Caporaso
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.
r8272 display(local_file)
Greg Caporaso
documentation modification
r8276
or in the HTML notebook, just
Greg Caporaso
renamed classes as recommended by @takluyver
r8365 FileLink("my/data.txt")
Greg Caporaso
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.
r8272 """
Greg Caporaso
defined __repr__ for FileLink and FileLinks. this now presents the absolute path when run from an ipython session, and a link when run from the HTML notebook
r8442 html_link_str = "<a href='%s' target='_blank'>%s</a>"
Greg Caporaso
made link_str a class-level attribute based on suggestion from @takluyver
r8438
Greg Caporaso
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.
r8272 def __init__(self,
path,
Greg Caporaso
moved / to url_prefix rather than ''.join calls as this allows the user to override it
r8446 url_prefix='files/',
Greg Caporaso
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.
r8439 result_html_prefix='',
result_html_suffix='<br>'):
Greg Caporaso
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.
r8272 """
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
Greg Caporaso
updated parameter name based on suggestion from @takluyver
r8437 self.url_prefix = url_prefix
Greg Caporaso
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.
r8439 self.result_html_prefix = result_html_prefix
self.result_html_suffix = result_html_suffix
Greg Caporaso
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.
r8272
def _format_path(self):
Greg Caporaso
moved / to url_prefix rather than ''.join calls as this allows the user to override it
r8446 fp = ''.join([self.url_prefix,self.path])
Greg Caporaso
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.
r8439 return ''.join([self.result_html_prefix,
Greg Caporaso
defined __repr__ for FileLink and FileLinks. this now presents the absolute path when run from an ipython session, and a link when run from the HTML notebook
r8442 self.html_link_str % (fp, self.path),
Greg Caporaso
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.
r8439 self.result_html_suffix])
Greg Caporaso
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.
r8272
def _repr_html_(self):
Greg Caporaso
defined __repr__ for FileLink and FileLinks. this now presents the absolute path when run from an ipython session, and a link when run from the HTML notebook
r8442 """return html link to file
Greg Caporaso
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.
r8272 """
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()
Greg Caporaso
defined __repr__ for FileLink and FileLinks. this now presents the absolute path when run from an ipython session, and a link when run from the HTML notebook
r8442
def __repr__(self):
Greg Caporaso
improved behavior of FileLinks.__repr__ to be more like the behvior of _repr_html_
r8443 """return absolute path to file
Greg Caporaso
defined __repr__ for FileLink and FileLinks. this now presents the absolute path when run from an ipython session, and a link when run from the HTML notebook
r8442 """
return abspath(self.path)
Greg Caporaso
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.
r8272 # 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
Greg Caporaso
defined __repr__ for FileLink and FileLinks. this now presents the absolute path when run from an ipython session, and a link when run from the HTML notebook
r8442 # we'll encourage users to reference these with a different class in
Greg Caporaso
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.
r8272 # case we want to change this in the future.
Greg Caporaso
renamed classes as recommended by @takluyver
r8365 DirectoryLink = FileLink
Greg Caporaso
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.
r8272
Greg Caporaso
renamed classes as recommended by @takluyver
r8365 class FileLinks(FileLink):
Greg Caporaso
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.
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
renamed classes as recommended by @takluyver
r8365 local_files = FileLinks("my/data")
Greg Caporaso
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.
r8272 display(local_files)
Greg Caporaso
documentation modification
r8276
or in the HTML notebook, just
Greg Caporaso
renamed classes as recommended by @takluyver
r8365 FileLinks("my/data")
Greg Caporaso
documentation modification
r8276
Greg Caporaso
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.
r8272 """
def __init__(self,
path,
Greg Caporaso
moved / to url_prefix rather than ''.join calls as this allows the user to override it
r8446 url_prefix='files/',
Greg Caporaso
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.
r8439 included_suffixes=None,
result_html_prefix='',
Greg Caporaso
improved formatting for FileLinks
r8619 result_html_suffix='<br>',
notebook_display_formatter=None,
terminal_display_formatter=None):
Greg Caporaso
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.
r8272 """
included_suffixes : list of filename suffixes to include when
formatting output [default: include all files]
Greg Caporaso
renamed classes as recommended by @takluyver
r8365 See the FileLink (baseclass of LocalDirectory) docstring for
Greg Caporaso
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.
r8272 information on additional parameters.
Greg Caporaso
fixed bug in test and in support for passing alternative formatters
r8626
Greg Caporaso
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.
r8272 """
Greg Caporaso
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.
r8439 self.included_suffixes = included_suffixes
Greg Caporaso
improved formatting for FileLinks
r8619
Greg Caporaso
renamed classes as recommended by @takluyver
r8365 FileLink.__init__(self,
Greg Caporaso
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.
r8272 path,
Greg Caporaso
updated parameter name based on suggestion from @takluyver
r8437 url_prefix,
Greg Caporaso
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.
r8439 result_html_prefix,
result_html_suffix)
Greg Caporaso
cleaned up to reduce duplicated code
r8624
self.notebook_display_formatter = \
Greg Caporaso
fixed bug in test and in support for passing alternative formatters
r8626 notebook_display_formatter or self._get_notebook_display_formatter()
Greg Caporaso
cleaned up to reduce duplicated code
r8624 self.terminal_display_formatter = \
Greg Caporaso
fixed bug in test and in support for passing alternative formatters
r8626 terminal_display_formatter or self._get_terminal_display_formatter()
Greg Caporaso
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.
r8272
Greg Caporaso
cleaned up to reduce duplicated code
r8624 def _get_display_formatter(self,
dirname_output_format,
fname_output_format,
fp_format):
included_suffixes = self.included_suffixes
Greg Caporaso
improved formatting for FileLinks
r8619 def f(output_lines, dirname, fnames):
""" """
# begin by figuring out which filenames, if any,
# are going to be displayed
display_fnames = []
for fname in fnames:
if (isfile(join(dirname,fname)) and
(included_suffixes == None or
splitext(fname)[1] in included_suffixes)):
display_fnames.append(fname)
Greg Caporaso
cleaned up to reduce duplicated code
r8624
Greg Caporaso
improved formatting for FileLinks
r8619 if len(display_fnames) == 0:
pass
else:
Greg Caporaso
cleaned up to reduce duplicated code
r8624 dirname_output_line = dirname_output_format % dirname
output_lines.append(dirname_output_line)
Greg Caporaso
improved formatting for FileLinks
r8619 for fname in display_fnames:
Greg Caporaso
cleaned up to reduce duplicated code
r8624 fp = fp_format % (dirname,fname)
try:
# output can include both a filepath and a filename...
fname_output_line = fname_output_format % (fp, fname)
except TypeError:
# ... or just a single filepath
fname_output_line = fname_output_format % fname
output_lines.append(fname_output_line)
Greg Caporaso
improved formatting for FileLinks
r8619 return
return f
Greg Caporaso
cleaned up to reduce duplicated code
r8624 def _get_notebook_display_formatter(self,
spacer="&nbsp;&nbsp;"):
Greg Caporaso
improved formatting for FileLinks
r8619 """ """
Greg Caporaso
cleaned up to reduce duplicated code
r8624 dirname_output_format = \
self.result_html_prefix + "%s" + self.result_html_suffix
fname_output_format = \
self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix
fp_format = self.url_prefix + '%s/%s'
return self._get_display_formatter(dirname_output_format,
fname_output_format,
fp_format)
Greg Caporaso
improved formatting for FileLinks
r8619
Greg Caporaso
cleaned up to reduce duplicated code
r8624 def _get_terminal_display_formatter(self,
spacer=" "):
""" """
dirname_output_format = "%s"
fname_output_format = spacer + "%s"
fp_format = '%s/%s'
return self._get_display_formatter(dirname_output_format,
fname_output_format,
fp_format)
Greg Caporaso
improved formatting for FileLinks
r8619
def _format_path(self):
result_lines = []
walk(self.path, self.notebook_display_formatter, result_lines)
return '\n'.join(result_lines)
Greg Caporaso
improved behavior of FileLinks.__repr__ to be more like the behvior of _repr_html_
r8443
def __repr__(self):
"""return newline-separated absolute paths
"""
Greg Caporaso
improved formatting for FileLinks
r8619 result_lines = []
walk(self.path, self.terminal_display_formatter, result_lines)
return '\n'.join(result_lines)