Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- encoding: utf-8 -*- r""" Display Manager
This is the heart of the rich output system, the display manager arbitrates between
* Backend capabilities: what can be displayed
* Backend preferences: what gives good quality on the backend
* Sage capabilities: every Sage object can only generate certain representations, and
* User preferences: typeset vs. plain text vs. ascii art, etc.
The display manager is a singleton class, Sage always has exactly one instance of it. Use :func:`get_display_manager` to obtain it.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager(); dm The Sage display manager using the doctest backend """
#***************************************************************************** # Copyright (C) 2015 Volker Braun <vbraun.name@gmail.com> # # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
import warnings
from sage.structure.sage_object import SageObject from sage.repl.rich_output.output_basic import ( OutputPlainText, OutputAsciiArt, OutputUnicodeArt, OutputLatex, ) from sage.repl.rich_output.preferences import DisplayPreferences
class DisplayException(Exception): """ Base exception for all rich output-related exceptions.
EXAMPLES::
sage: from sage.repl.rich_output.display_manager import DisplayException sage: raise DisplayException('foo') Traceback (most recent call last): ... DisplayException: foo """ pass
class OutputTypeException(DisplayException): """ Wrong Output container.
The output containers are the subclasses of :class:`~sage.repl.rich_output.output_basic.OutputBase` that contain the entire output. The display backends must create output containers of a suitable type depending on the displayed Python object. This exception indicates that there is a mistake in the backend and it returned the wrong type of output container.
EXAMPLES::
sage: from sage.repl.rich_output.display_manager import OutputTypeException sage: raise OutputTypeException('foo') Traceback (most recent call last): ... OutputTypeException: foo """ pass
class RichReprWarning(UserWarning): """ Warning that is throws if a call to ``_rich_repr_`` fails.
If an object implements ``_rich_repr_`` then it must return a value, possibly ``None`` to indicate that no rich output can be generated. But it may not raise an exception as it is very confusing for the user if the displayhook fails.
EXAMPLES::
sage: from sage.repl.rich_output.display_manager import RichReprWarning sage: raise RichReprWarning('foo') Traceback (most recent call last): ... RichReprWarning: foo """ pass
class restricted_output(object):
def __init__(self, display_manager, output_classes): """ Context manager to temporarily restrict the accepted output types
In the context, the output is restricted to the output container types listed in ``output_classes``. Additionally, display preferences are changed not to show graphics.
INPUT:
- ``display_manager`` -- the display manager.
- ``output_classes`` -- iterable of output container types.
EXAMPLES::
sage: from sage.repl.rich_output.display_manager import ( ....: get_display_manager, restricted_output) sage: dm = get_display_manager() sage: restricted_output(dm, [dm.types.OutputPlainText]) <sage.repl.rich_output.display_manager.restricted_output object at 0x...> """
def __enter__(self): """ Enter the restricted output context
EXAMPLES::
sage: from sage.repl.rich_output.display_manager import ( ....: get_display_manager, restricted_output) sage: dm = get_display_manager() sage: len(dm.supported_output()) > 1 True sage: with restricted_output(dm, [dm.types.OutputPlainText]): ....: dm.supported_output() frozenset({<class 'sage.repl.rich_output.output_basic.OutputPlainText'>})
sage: dm.preferences.supplemental_plot 'never' sage: dm.preferences.supplemental_plot = 'always' sage: with restricted_output(dm, [dm.types.OutputPlainText]): ....: dm.preferences Display preferences: * graphics = disable * supplemental_plot = never * text is not specified sage: dm.preferences.supplemental_plot = 'never' """
def __exit__(self, exception_type, value, traceback): """ Exit the restricted output context
EXAMPLES::
sage: from sage.repl.rich_output.display_manager import ( ....: get_display_manager, restricted_output) sage: dm = get_display_manager() sage: with restricted_output(dm, [dm.types.OutputPlainText]): ....: assert len(dm.supported_output()) == 1 sage: assert len(dm.supported_output()) > 1 """
class DisplayManager(SageObject):
_instance = None
def __init__(self): """ The Display Manager
Used to decide what kind of rich output is best.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: get_display_manager() The Sage display manager using the doctest backend """ assert DisplayManager._instance is None DisplayManager._instance = self from sage.repl.rich_output.backend_base import BackendSimple self.switch_backend(BackendSimple())
@classmethod def get_instance(cls): """ Get the singleton instance.
This class method is equivalent to :func:`get_display_manager`.
OUTPUT:
The display manager singleton.
EXAMPLES::
sage: from sage.repl.rich_output.display_manager import DisplayManager sage: DisplayManager.get_instance() The Sage display manager using the doctest backend """ else: return cls()
def _repr_(self): """ Return a string representation.
OUTPUT:
String.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: get_display_manager() The Sage display manager using the doctest backend """
@property def types(self): """ Catalog of all output container types.
Note that every output type must be registered in :mod:`sage.repl.rich_output.output_catalog`.
OUTPUT:
Returns the :mod:`sage.repl.rich_output.output_catalog` module.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm.types.OutputPlainText <class 'sage.repl.rich_output.output_basic.OutputPlainText'> """
def switch_backend(self, backend, **kwds): """ Switch to a new backend
INPUT:
- ``backend`` -- instance of :class:`~sage.repl.rich_output.backend_base.BackendBase`.
- ``kwds`` -- optional keyword arguments that are passed on to the :meth:`~sage.repl.rich_output.backend_base.BackendBase.install` method.
OUTPUT:
The previous backend.
EXAMPLES::
sage: from sage.repl.rich_output.backend_base import BackendSimple sage: simple = BackendSimple() sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager(); dm The Sage display manager using the doctest backend
sage: previous = dm.switch_backend(simple) sage: dm The Sage display manager using the simple backend
Restore the doctest backend::
sage: dm.switch_backend(previous) is simple True """ raise ValueError('backend must be instance of BackendBase class') raise ValueError('every backend must support plain text') except AttributeError: pass # first time we switch # clear caches map(self._demote_output_class, backend.supported_output())) # install new backend except AttributeError: old_backend = None
@property def preferences(self): """ Return the preferences.
OUTPUT:
The display preferences as instance of :class:`~sage.repl.rich_output.preferences.DisplayPreferences`.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm.preferences Display preferences: * graphics is not specified * supplemental_plot = never * text is not specified """
def is_in_terminal(self): """ Test whether the UI is meant to run in a terminal
When this method returns ``True``, you can assume that it is possible to use ``raw_input`` or launch external programs that take over the input.
Otherwise, you should assume that the backend runs remotely or in a pty controlled by another program. Then you should not launch external programs with a (text or graphical) UI.
This is used to enable/disable interpreter consoles.
OUTPUT:
Boolean. """
def check_backend_class(self, backend_class): """ Check that the current backend is an instance of ``backend_class``.
This is, for example, used by the Sage IPython display formatter to ensure that the IPython backend is in use.
INPUT:
- ``backend_class`` -- type of a backend class.
OUTPUT:
This method returns nothing. A ``RuntimeError`` is raised if ``backend_class`` is not the type of the current backend.
EXAMPLES::
sage: from sage.repl.rich_output.backend_base import BackendSimple sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm.check_backend_class(BackendSimple) Traceback (most recent call last): ... RuntimeError: check failed: current backend is invalid """
def _demote_output_class(self, output_class): """ Helper for :meth:`switch_backend`.
INPUT:
- ``output_class`` -- a possibly derived class from one of the output container classes in :meth:`types`.
OUTPUT:
The underlying container class that it was derived from.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm._demote_output_class(dm.types.OutputPlainText) <class 'sage.repl.rich_output.output_basic.OutputPlainText'> """ raise OutputTypeException( 'invalid output container type: {0} is not subclass of OutputBase' .format(output_class)) continue raise OutputTypeException( '{0} inherits from multiple output classes' .format(output_class)) else: raise OutputTypeException( '{0} does not inherit from any known output class' .format(output_class))
def _promote_output(self, output): """ Promote output container to a backend-specific subclass
INPUT:
- ``output`` -- instance of a subclass of :class:`~sage.repl.rich_output.output_basic.OutputBase`. Backend-agnostic output container.
OUTPUT:
If the backend returned a subclass of the type of ``output``: promote the class to the subclass and return it. Otherwise, the unchanged output is returned.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: out = dm._promote_output(dm.types.OutputPlainText('test')) sage: type(out) <class 'sage.repl.rich_output.output_basic.OutputPlainText'> """
def _preferred_text_formatter(self, obj, plain_text=None, **kwds): """ Return the preferred textual representation
INPUT:
- ``obj`` -- anything. The objects to format.
- ``plain_text`` -- ``None`` (default) or string. The plain text representation. If specified, this will be used for plain text output.
OUTPUT:
One of :class:`~sage.repl.rich_output.output_basic.OutputPlainText`, :class:`~sage.repl.rich_output.output_basic.OutputAsciiArt`, or :class:`~sage.repl.rich_output.output_basic.OutputLatex` containing the preferred textual representation of ``obj`` supported by the backend.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm.preferences.text is None True sage: dm._preferred_text_formatter([1/42]) OutputPlainText container
sage: dm.preferences.text = 'plain' sage: dm._preferred_text_formatter([1/42]) OutputPlainText container
sage: dm.preferences.text = 'ascii_art' sage: dm._preferred_text_formatter([1/42]) OutputAsciiArt container
sage: dm.preferences.text = 'unicode_art' sage: dm._preferred_text_formatter([1/42]) OutputUnicodeArt container
sage: dm.preferences.text = 'latex' sage: dm._preferred_text_formatter([1/42]) \newcommand{\Bold}[1]{\mathbf{#1}}\verb|OutputLatex|\phantom{\verb!x!}\verb|container|
sage: del dm.preferences.text # reset to default """ raise OutputTypeException('backend returned wrong output type, require AsciiArt') raise OutputTypeException('backend returned wrong output type, require UnicodeArt') raise OutputTypeException('backend returned wrong output type, require Latex') raise OutputTypeException('backend returned wrong output type, require PlainText') raise OutputTypeException('backend returned wrong output type, require PlainText')
def _call_rich_repr(self, obj, rich_repr_kwds): """ Call the ``_rich_repr_`` method
This method calls ``obj._rich_repr_``. If this raises an exception, it is caught and a suitable warning is displayed.
INPUT:
- ``obj`` -- anything.
- ``rich_repr_kwds`` -- dictionary. Optional keyword arguments that are passed through to ``obj._rich_repr_``.
OUTPUT:
Whatever ``_rich_repr_`` returned. If it raises an exception, then a :class:`DisplayFormatterWarning` is displayed and ``None`` is returned.
EXAMPLES::
sage: class Foo(SageObject): ....: def _rich_repr_(self, display_manager, **kwds): ....: raise ValueError('reason')
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm._call_rich_repr(Foo(), {}) doctest:...: ...RichReprWarning: Exception in _rich_repr_ while displaying object: reason """ # do not ignore errors from invalid options # don't warn on NotImplementedErrors return None 'Exception in _rich_repr_ while displaying object: {0}'.format(e), RichReprWarning, )
def _rich_output_formatter(self, obj, rich_repr_kwds): """ Generate appropriate rich output containers.
INPUT:
- ``obj`` -- anything.
- ``rich_repr_kwds`` -- dictionary. Optional keyword arguments that are passed through to ``obj._rich_repr_``.
OUTPUT:
A pair of rich output containers. The first is plain text, that is, an instance of :class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The second is the best rich output available, subject to the constraints of the backend.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm._rich_output_formatter(1/2, dict()) (OutputPlainText container, OutputPlainText container) """ obj, plain_text=plain_text, **rich_repr_kwds) # promote output container types to backend-specific containers # check that the output container types are valid for the backend raise OutputTypeException( 'text output container not supported: {0}'.format(type(plain_text))) raise OutputTypeException( 'output container not supported: {0}'.format(type(rich_output)))
def graphics_from_save(self, save_function, save_kwds, file_extension, output_container, figsize=None, dpi=None): r""" Helper to construct graphics.
This method can be used to simplify the implementation of a ``_rich_repr_`` method of a graphics object if there is already a function to save graphics to a file.
INPUT:
- ``save_function`` -- callable that can save graphics to a file and accepts options like :meth:`sage.plot.graphics.Graphics.save`.
- ``save_kwds`` -- dictionary. Keyword arguments that are passed to the save function.
- ``file_extension`` -- string starting with ``'.'``. The file extension of the graphics file.
- ``output_container`` -- subclass of :class:`sage.repl.rich_output.output_basic.OutputBase`. The output container to use. Must be one of the types in :meth:`supported_output`.
- ``figsize`` -- pair of integers (optional). The desired graphics size in pixels. Suggested, but need not be respected by the output.
- ``dpi`` -- integer (optional). The desired resolution in dots per inch. Suggested, but need not be respected by the output.
OUTPUT:
Return an instance of ``output_container``.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: plt = plot(sin) sage: out = dm.graphics_from_save(plt.save, dict(), '.png', dm.types.OutputImagePng) sage: out OutputImagePng container sage: out.png.get().startswith(b'\x89PNG') True sage: out.png.filename() # random '/home/user/.sage/temp/localhost.localdomain/23903/tmp_pu5woK.png' """ raise ValueError('file_extension must start with a period') raise OutputTypeException('output_container is not supported by backend') # Call the save_function with the right arguments kwds['figsize'] = figsize kwds['dpi'] = dpi
def threejs_scripts(self, online): """ Return Three.js script tags for the current backend.
INPUT:
- ``online`` -- Boolean determining script usage context
OUTPUT:
String containing script tags
.. NOTE::
This base method handles ``online=True`` case only, serving CDN script tags. Location of scripts for offline usage is backend-specific.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: get_display_manager().threejs_scripts(online=True) '...<script src="https://cdn.rawgit.com/mrdoob/three.js/...' sage: get_display_manager().threejs_scripts(online=False) Traceback (most recent call last): ... ValueError: current backend does not support offline threejs graphics """ <script src="https://cdn.rawgit.com/mrdoob/three.js/{0}/build/three.min.js"></script> <script src="https://cdn.rawgit.com/mrdoob/three.js/{0}/examples/js/controls/OrbitControls.js"></script> """.format(version) 'current backend does not support offline threejs graphics')
def supported_output(self): """ Return the output container classes that can be used.
OUTPUT:
Frozen set of subclasses of :class:`~sage.repl.rich_output.output_basic.OutputBase`. If the backend defines derived container classes, this method will always return their base classes.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm.types.OutputPlainText in dm.supported_output() True sage: type(dm.supported_output()) <... 'frozenset'> """
def displayhook(self, obj): """ Implementation of the displayhook
Every backend must pass the value of the last statement of a line / cell to this method. See also :meth:`display_immediately` if you want do display rich output while a program is running.
INPUT:
- ``obj`` -- anything. The object to be shown.
OUTPUT:
Returns whatever the backend's displayhook method returned.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm.displayhook(1/2) 1/2 """
def display_immediately(self, obj, **rich_repr_kwds): """ Show output without going back to the command line prompt.
This method must be called to create rich output from an object when we are not returning to the command line prompt, for example during program execution. Typically, it is being called by :meth:`sage.plot.graphics.Graphics.show`.
INPUT:
- ``obj`` -- anything. The object to be shown.
- ``rich_repr_kwds`` -- optional keyword arguments that are passed through to ``obj._rich_repr_``.
EXAMPLES::
sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() sage: dm.display_immediately(1/2) 1/2 """
get_display_manager = DisplayManager.get_instance |