This is the heart of the rich output system, the display manager arbitrates between
The display manager is a singleton class, Sage always has exactly one instance of it. Use 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
Bases: exceptions.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
Bases: sage.structure.sage_object.SageObject
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
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:
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
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 sage.plot.graphics.Graphics.show().
INPUT:
EXAMPLES:
sage: from sage.repl.rich_output import get_display_manager
sage: dm = get_display_manager()
sage: dm.display_immediately(1/2)
1/2
Implementation of the displayhook
Every backend must pass the value of the last statement of a line / cell to this method. See also display_immediately() if you want do display rich output while a program is running.
INPUT:
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
Get the singleton instance.
This class method is equivalent to 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
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:
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('\x89PNG')
True
sage: out.png.filename() # random
'/home/user/.sage/temp/localhost.localdomain/23903/tmp_pu5woK.png'
Return the preferences.
OUTPUT:
The display preferences as instance of 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
* text is not specified
Return the output container classes that can be used.
OUTPUT:
Frozen set of subclasses of 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())
<type 'frozenset'>
Switch to a new backend
INPUT:
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
Catalog of all output container types.
Note that every output type must be registered in sage.repl.rich_output.output_catalog.
OUTPUT:
Returns the 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'>
Bases: sage.repl.rich_output.display_manager.DisplayException
Wrong Output container.
The output containers are the subclasses of 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
Bases: exceptions.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
Get the singleton instance.
This class method is equivalent to 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
Bases: object
Context manager to temporarily restrict the accepted output types
INPUT:
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...>