update jupyter

This commit is contained in:
Vitor Santos Costa
2018-10-16 14:33:16 +01:00
parent 3fdbbc6a77
commit c59b01e53c
34 changed files with 483 additions and 257 deletions

View File

@@ -2,13 +2,14 @@
Authors : MinRK, gregcaporaso, dannystaple
"""
from html import escape as html_escape
from os.path import exists, isfile, splitext, abspath, join, isdir
from os import walk, sep
from os import walk, sep, fsdecode
from yap_ipython.core.display import DisplayObject
from yap_ipython.core.display import DisplayObject, TextDisplayObject
__all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',
'FileLink', 'FileLinks']
'FileLink', 'FileLinks', 'Code']
class Audio(DisplayObject):
@@ -197,7 +198,7 @@ class Audio(DisplayObject):
class IFrame(object):
"""
Generic class to embed an iframe in an yap_ipython notebook
Generic class to embed an iframe in an IPython notebook
"""
iframe = """
@@ -232,7 +233,7 @@ class IFrame(object):
params=params)
class YouTubeVideo(IFrame):
"""Class for embedding a YouTube Video in an yap_ipython session, based on its video id.
"""Class for embedding a YouTube Video in an IPython session, based on its video id.
e.g. to embed the video from https://www.youtube.com/watch?v=foo , you would
do::
@@ -273,7 +274,7 @@ class YouTubeVideo(IFrame):
class VimeoVideo(IFrame):
"""
Class for embedding a Vimeo video in an yap_ipython session, based on its video id.
Class for embedding a Vimeo video in an IPython session, based on its video id.
"""
def __init__(self, id, width=400, height=300, **kwargs):
@@ -282,7 +283,7 @@ class VimeoVideo(IFrame):
class ScribdDocument(IFrame):
"""
Class for embedding a Scribd document in an yap_ipython session
Class for embedding a Scribd document in an IPython session
Use the start_page params to specify a starting point in the document
Use the view_mode params to specify display type one off scroll | slideshow | book
@@ -297,9 +298,9 @@ class ScribdDocument(IFrame):
super(ScribdDocument, self).__init__(src, width, height, **kwargs)
class FileLink(object):
"""Class for embedding a local file link in an yap_ipython session, based on path
"""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 yap_ipython notebook as my/data.txt
e.g. to embed a link that was generated in the IPython notebook as my/data.txt
you would do::
@@ -334,15 +335,16 @@ class FileLink(object):
if isdir(path):
raise ValueError("Cannot display a directory using FileLink. "
"Use FileLinks to display '%s'." % path)
self.path = path
self.path = fsdecode(path)
self.url_prefix = url_prefix
self.result_html_prefix = result_html_prefix
self.result_html_suffix = result_html_suffix
def _format_path(self):
fp = ''.join([self.url_prefix,self.path])
fp = ''.join([self.url_prefix, html_escape(self.path)])
return ''.join([self.result_html_prefix,
self.html_link_str % (fp, self.path),
self.html_link_str % \
(fp, html_escape(self.path, quote=False)),
self.result_html_suffix])
def _repr_html_(self):
@@ -362,9 +364,9 @@ class FileLink(object):
return abspath(self.path)
class FileLinks(FileLink):
"""Class for embedding local file links in an yap_ipython session, based on path
"""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 yap_ipython notebook
e.g. to embed links to files that were generated in the IPython notebook
under ``my/data``, you would do::
local_files = FileLinks("my/data")
@@ -424,7 +426,7 @@ class FileLinks(FileLink):
raise ValueError("Cannot display a file using FileLinks. "
"Use FileLink to display '%s'." % path)
self.included_suffixes = included_suffixes
# remove trailing slashs for more consistent output formatting
# remove trailing slashes for more consistent output formatting
path = path.rstrip('/')
self.path = path
@@ -555,3 +557,52 @@ class FileLinks(FileLink):
for dirname, subdirs, fnames in walked_dir:
result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
return '\n'.join(result_lines)
class Code(TextDisplayObject):
"""Display syntax-highlighted source code.
This uses Pygments to highlight the code for HTML and Latex output.
Parameters
----------
data : str
The code as a string
url : str
A URL to fetch the code from
filename : str
A local filename to load the code from
language : str
The short name of a Pygments lexer to use for highlighting.
If not specified, it will guess the lexer based on the filename
or the code. Available lexers: http://pygments.org/docs/lexers/
"""
def __init__(self, data=None, url=None, filename=None, language=None):
self.language = language
super().__init__(data=data, url=url, filename=filename)
def _get_lexer(self):
if self.language:
from pygments.lexers import get_lexer_by_name
return get_lexer_by_name(self.language)
elif self.filename:
from pygments.lexers import get_lexer_for_filename
return get_lexer_for_filename(self.filename)
else:
from pygments.lexers import guess_lexer
return guess_lexer(self.data)
def __repr__(self):
return self.data
def _repr_html_(self):
from pygments import highlight
from pygments.formatters import HtmlFormatter
fmt = HtmlFormatter()
style = '<style>{}</style>'.format(fmt.get_style_defs('.output_html'))
return style + highlight(self.data, self._get_lexer(), fmt)
def _repr_latex_(self):
from pygments import highlight
from pygments.formatters import LatexFormatter
return highlight(self.data, self._get_lexer(), LatexFormatter())