def __init__(self, html): """ HTML Output INPUT: - ``html`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. String containing the html fragment code. Excludes the surrounding ``<body>`` and ``<html>`` tag. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: OutputHtml('<div>Foo<b>B</b>ar</div>') OutputHtml container """ self.html = OutputBuffer(html) # if the html is a simple wrapper of latex for mathjax rendering, then # the latex string is saved for possible latex output such as Jupyter's # pdf export of a notebook m = latex_re.match(html) if m: mathjax_string = m.group('latex') latex_string = mathjax_string.replace('<', '<') if m.group('mathstart') == r'\[' and m.group('mathend') == r'\]': self.latex = OutputBuffer('$$' + latex_string + '$$') else: self.latex = OutputBuffer('$' + latex_string + '$') else: self.latex = None
def __init__(self, obj, mtl): """ Wavefront `*.obj` Scene The Wavefront format consists of two files, an ``.obj`` file defining the geometry data (mesh points, normal vectors, ...) together with a ``.mtl`` file defining texture data. INPUT: - ``obj`` -- bytes. The Wavefront obj file format describing the mesh shape. - ``mtl`` -- bytes. The Wavefront mtl file format describing textures. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputSceneWavefront sage: OutputSceneWavefront.example() OutputSceneWavefront container """ self.obj = OutputBuffer(obj) self.mtl = OutputBuffer(mtl) self._check_no_directory(self.mtllib())
def __init__(self, png): """ PNG Image .. NOTE:: Every backend that is capable of displaying any kind of graphics is supposed to support the PNG format at least. INPUT: - ``png`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. The PNG image data. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputImagePng sage: OutputImagePng.example() # indirect doctest OutputImagePng container """ self.png = OutputBuffer(png)
def __init__(self, plain_text): """ Plain Text Output INPUT: - ``plain_text`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a bytes (string in Python 2.x) or string (unicode in Python 2.x) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. The plain text output. This should always be exactly the same as the (non-rich) output from the ``_repr_`` method. Every backend object must support plain text output as fallback. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputPlainText sage: OutputPlainText('foo') OutputPlainText container """ self.text = OutputBuffer(plain_text)
def __init__(self, latex): """ LaTeX Output .. note:: The LaTeX commands will only use a subset of LaTeX that can be displayed by MathJax. INPUT: - ``latex`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. String containing the latex equation code. Excludes the surrounding dollar signs / LaTeX equation environment. Also excludes the surrounding MathJax ``<html>`` tag. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputLatex sage: OutputLatex('<html><script type="math/tex; mode=display">1</script></html>') OutputLatex container """ self.latex = OutputBuffer(latex)
def __init__(self, unicode_art): """ Unicode Art Output Similar to :class:`OutputAsciiArt` but using the entire unicode range. INPUT: - ``unicode_art`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (unicode in Python 2.x) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Unicode art rendered into a string. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputUnicodeArt sage: OutputUnicodeArt(u':-}') OutputUnicodeArt container """ # Internally, all buffers store bytes. Unicode is always utf-8 # encoded. if not isinstance(unicode_art, bytes): unicode_art = unicode_art.encode('utf-8') self.unicode_art = OutputBuffer(unicode_art)
def __init__(self, scene_zip, preview_png): """ JMol Scene By our (Sage) convention, the actual scene is called ``SCENE`` inside the zip archive. INPUT: - ``scene_zip`` -- string/bytes. The jmol scene (a zip archive). - ``preview_png`` -- string/bytes. Preview as png file. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputSceneJmol sage: OutputSceneJmol.example() OutputSceneJmol container """ self.scene_zip = OutputBuffer(scene_zip) self.preview_png = OutputBuffer(preview_png)
def __init__(self, html): """ Three.js Scene INPUT: - ``html`` -- string/bytes. The Three.js HTML data. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputSceneThreejs sage: OutputSceneThreejs('<html></html>') OutputSceneThreejs container """ self.html = OutputBuffer(html)
def __init__(self, canvas3d): """ Canvas3d Scene INPUT: - ``canvas3d`` -- string/bytes. The canvas3d data. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputSceneCanvas3d sage: OutputSceneCanvas3d.example() OutputSceneCanvas3d container """ self.canvas3d = OutputBuffer(canvas3d)
def __init__(self, gif): """ GIF Image (possibly animated) INPUT: - ``gif`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. The GIF image data. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputImageGif sage: OutputImageGif.example() # indirect doctest OutputImageGif container """ self.gif = OutputBuffer(gif)
def __init__(self, dvi): """ DVI Image INPUT: - ``dvi`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. The DVI data. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputImageDvi sage: OutputImageDvi.example() # indirect doctest OutputImageDvi container """ self.dvi = OutputBuffer(dvi)
def __init__(self, pdf): """ PDF Image INPUT: - ``pdf`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. The PDF data. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputImagePdf sage: OutputImagePdf.example() # indirect doctest OutputImagePdf container """ self.pdf = OutputBuffer(pdf)
def __init__(self, svg): """ SVG Image INPUT: - ``svg`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. The SVG image data. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputImageSvg sage: OutputImageSvg.example() # indirect doctest OutputImageSvg container """ self.svg = OutputBuffer(svg)
def __init__(self, ascii_art): """ ASCII Art Output INPUT: - ``ascii_art`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Ascii art rendered into a string. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputAsciiArt sage: OutputAsciiArt(':-}') OutputAsciiArt container """ self.ascii_art = OutputBuffer(ascii_art)
def __init__(self, html): """ HTML Output INPUT: - ``html`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. String containing the html fragment code. Excludes the surrounding ``<body>`` and ``<html>`` tag. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: OutputHtml('<div>Foo<b>B</b>ar</div>') OutputHtml container """ self.html = OutputBuffer(html)
def _rich_repr_(self, display_manager, **kwds): """ Rich Output Magic Method See :mod:`sage.repl.rich_output` for details. EXAMPLES:: sage: from sage.repl.image import Image sage: img = Image('1', (16, 16), 'white') sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: img._rich_repr_(dm) OutputImagePng container sage: img = Image('F', (16, 16), 'white') # not supported in PNG sage: img._rich_repr_(dm) OutputImageGif container """ if display_manager.preferences.graphics == 'disable': return types = display_manager.types preferred = ( ('PNG', types.OutputImagePng), ('JPEG', types.OutputImageJpg), ('GIF', types.OutputImageGif), ) import StringIO from sage.repl.rich_output.buffer import OutputBuffer for format, output_container in preferred: if output_container in display_manager.supported_output(): stream = StringIO.StringIO() try: self.pil.save(stream, format=format) except IOError: # not all formats support all modes, e.g. no alpha support in gif continue buf = OutputBuffer(stream.getvalue()) return output_container(buf)