IPython Displayhook Formatters#

The classes in this module can be used as IPython displayhook formatters. It has two main features, by default the displayhook contains a new facility for displaying lists of matrices in an easier to read format:

sage: [identity_matrix(i) for i in range(2,5)]
[
                [1 0 0 0]
       [1 0 0]  [0 1 0 0]
[1 0]  [0 1 0]  [0 0 1 0]
[0 1], [0 0 1], [0 0 0 1]
]

This facility uses _repr_() (and a simple string) to try do a nice read format (see sage.structure.parent.Parent._repr_option() for details).

With this displayhook there exists an other way for displaying object and more generally, all sage expression as an ASCII art object:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.run_cell('%display ascii_art')
sage: shell.run_cell('integral(x^2/pi^x, x)')                                       # needs sage.symbolic
   -x / 2    2                      \
-pi  *\x *log (pi) + 2*x*log(pi) + 2/
--------------------------------------
                 3
               log (pi)
sage: shell.run_cell("i = var('i')")                                                # needs sage.symbolic
sage: shell.run_cell('sum(i*x^i, i, 0, 10)')                                        # needs sage.symbolic
    10      9      8      7      6      5      4      3      2
10*x   + 9*x  + 8*x  + 7*x  + 6*x  + 5*x  + 4*x  + 3*x  + 2*x  + x
sage: shell.run_cell('StandardTableaux(4).list()')                                  # needs sage.combinat
[
[                                                                  1  4    1  3
[                 1  3  4    1  2  4    1  2  3    1  3    1  2    2       2
[   1  2  3  4,   2      ,   3      ,   4      ,   2  4,   3  4,   3   ,   4   ,

           1 ]
   1  2    2 ]
   3       3 ]
   4   ,   4 ]
sage: shell.run_cell('%display default')
sage: shell.quit()

This other facility uses a simple AsciiArt object (see and sage.structure.sage_object.SageObject._ascii_art_()).

class sage.repl.display.formatter.SageDisplayFormatter(**kwargs: Any)#

Bases: DisplayFormatter

This is where the Sage rich objects are translated to IPython

INPUT/OUTPUT:

See the IPython documentation.

EXAMPLES:

This is part of how Sage works with the IPython output system. It cannot be used in doctests:

sage: from sage.repl.display.formatter import SageDisplayFormatter
sage: fmt = SageDisplayFormatter()
Traceback (most recent call last):
...
RuntimeError: check failed: current backend is invalid
format(obj, include=None, exclude=None)#

Use the Sage rich output instead of IPython

INPUT/OUTPUT:

See the IPython documentation.

EXAMPLES:

sage: [identity_matrix(i) for i in range(3,7)]                              # needs sage.modules
[
                                 [1 0 0 0 0 0]
                    [1 0 0 0 0]  [0 1 0 0 0 0]
         [1 0 0 0]  [0 1 0 0 0]  [0 0 1 0 0 0]
[1 0 0]  [0 1 0 0]  [0 0 1 0 0]  [0 0 0 1 0 0]
[0 1 0]  [0 0 1 0]  [0 0 0 1 0]  [0 0 0 0 1 0]
[0 0 1], [0 0 0 1], [0 0 0 0 1], [0 0 0 0 0 1]
]
sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.run_cell('%display ascii_art')   # indirect doctest
sage: shell.run_cell("i = var('i')")                                        # needs sage.symbolic
sage: shell.run_cell('sum(i*x^i, i, 0, 10)')                                # needs sage.symbolic
    10      9      8      7      6      5      4      3      2
10*x   + 9*x  + 8*x  + 7*x  + 6*x  + 5*x  + 4*x  + 3*x  + 2*x  + x
sage: shell.run_cell('%display default')
sage: shell.quit()
class sage.repl.display.formatter.SagePlainTextFormatter(**kwargs: Any)#

Bases: PlainTextFormatter

Improved plain text IPython formatter.

In particular, it correctly print lists of matrices or other objects (see sage.structure.parent.Parent._repr_option()).

Warning

This IPython formatter is NOT used. You could use it to enable Sage formatting in IPython, but Sage uses its own rich output system that is more flexible and supports different backends.

INPUT/OUTPUT:

See the IPython documentation.

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.display_formatter.formatters['text/plain']
<IPython.core.formatters.PlainTextFormatter object at 0x...>
sage: shell.quit()