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)')
 / 2    2                      \  -x*log(pi)
-\x *log (pi) + 2*x*log(pi) + 2/*e
---------------------------------------------
                     3
                  log (pi)
sage: shell.run_cell("i = var('i')")
sage: shell.run_cell('sum(i*x^i, i, 0, 10)')
    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()')
[
[                                                                  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 simple')

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

class sage.repl.display.formatter.SageConsoleTextFormatter(*args, **kwds)

Bases: sage.repl.display.formatter.SagePlainTextFormatter

Improved plain text formatter.

In particular, it has the following two features:

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.display_formatter.formatters['text/plain']
<sage.repl.display.formatter.SagePlainTextFormatter object at 0x...>
sage: shell.run_cell('a = identity_matrix(ZZ, 2); [a,a]')
[
[1 0]  [1 0]
[0 1], [0 1]
]
class sage.repl.display.formatter.SageDoctestTextFormatter(*args, **kwds)

Bases: sage.repl.display.formatter.SagePlainTextFormatter

Improved plain text formatter.

In particular, it has the following two features:

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.display_formatter.formatters['text/plain']
<sage.repl.display.formatter.SagePlainTextFormatter object at 0x...>
sage: shell.run_cell('a = identity_matrix(ZZ, 2); [a,a]')
[
[1 0]  [1 0]
[0 1], [0 1]
]
class sage.repl.display.formatter.SageNBTextFormatter(*args, **kwds)

Bases: sage.repl.display.formatter.SagePlainTextFormatter

Improved plain text formatter.

In particular, it has the following two features:

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.display_formatter.formatters['text/plain']
<sage.repl.display.formatter.SagePlainTextFormatter object at 0x...>
sage: shell.run_cell('a = identity_matrix(ZZ, 2); [a,a]')
[
[1 0]  [1 0]
[0 1], [0 1]
]
class sage.repl.display.formatter.SagePlainTextFormatter(*args, **kwds)

Bases: IPython.core.formatters.PlainTextFormatter

Improved plain text formatter.

In particular, it has the following two features:

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.display_formatter.formatters['text/plain']
<sage.repl.display.formatter.SagePlainTextFormatter object at 0x...>
sage: shell.run_cell('a = identity_matrix(ZZ, 2); [a,a]')
[
[1 0]  [1 0]
[0 1], [0 1]
]
ascii_art

Whether the mode is the ascii art display.

OUTPUT:

Boolean.

EXAMPLES:

sage: sys.displayhook.formatter.simple
True
sage: sys.displayhook.formatter.ascii_art
False
set_display(mode)

Select the text formatting method.

INPUT:

  • mode – string. One of simple, ascii_art, or typeset.

EXAMPLES:

sage: [identity_matrix(i) for i in range(3,7)]
[
                                 [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')")
sage: shell.run_cell('sum(i*x^i, i, 0, 10)')
    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 simple')
simple

Whether the mode is the “simple” (default) display.

OUTPUT:

Boolean.

EXAMPLES:

sage: sys.displayhook.formatter.simple
True
sage: sys.displayhook.formatter.ascii_art
False
typeset

Whether the mode is the notebook “Typeset” display.

OUTPUT:

Boolean.

EXAMPLES:

sage: sys.displayhook.formatter.simple
True
sage: sys.displayhook.formatter.typeset
False

Previous topic

Keep track of attached files

Next topic

The Sage pretty printer

This Page