IPython Backend for the Sage Rich Output System¶
This module defines the IPython backends for
sage.repl.rich_output.
- class sage.repl.rich_output.backend_ipython.BackendIPython[source]¶
Bases:
BackendBaseCommon base for the IPython UIs.
EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPython sage: BackendIPython()._repr_() Traceback (most recent call last): ... NotImplementedError: derived classes must implement this method
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPython >>> BackendIPython()._repr_() Traceback (most recent call last): ... NotImplementedError: derived classes must implement this method
- display_immediately(plain_text, rich_output)[source]¶
Show output immediately.
This method is similar to the rich output
displayhook(), except that it can be invoked at any time.INPUT:
Same as
displayhook().OUTPUT: this method does not return anything
EXAMPLES:
sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: backend = BackendIPythonNotebook() sage: _ = backend.display_immediately(plain_text, plain_text) Example plain text output
>>> from sage.all import * >>> from sage.repl.rich_output.output_basic import OutputPlainText >>> plain_text = OutputPlainText.example() >>> from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook >>> backend = BackendIPythonNotebook() >>> _ = backend.display_immediately(plain_text, plain_text) Example plain text output
- install(**kwds)[source]¶
Switch the Sage rich output to the IPython backend.
INPUT:
shell– keyword argument; the IPython shell
No tests since switching away from the doctest rich output backend will break the doctests.
EXAMPLES:
sage: from sage.repl.interpreter import get_test_shell sage: from sage.repl.rich_output.backend_ipython import BackendIPython sage: backend = BackendIPython() sage: shell = get_test_shell() sage: backend.install(shell=shell) sage: shell.run_cell('1+1') 2
>>> from sage.all import * >>> from sage.repl.interpreter import get_test_shell >>> from sage.repl.rich_output.backend_ipython import BackendIPython >>> backend = BackendIPython() >>> shell = get_test_shell() >>> backend.install(shell=shell) >>> shell.run_cell('1+1') 2
- set_underscore_variable(obj)[source]¶
Set the
_builtin variable.Since IPython handles the history itself, this does nothing.
INPUT:
obj– anything
EXAMPLES:
sage: from sage.repl.interpreter import get_test_shell sage: from sage.repl.rich_output.backend_ipython import BackendIPython sage: backend = BackendIPython() sage: backend.set_underscore_variable(123) sage: _ 0
>>> from sage.all import * >>> from sage.repl.interpreter import get_test_shell >>> from sage.repl.rich_output.backend_ipython import BackendIPython >>> backend = BackendIPython() >>> backend.set_underscore_variable(Integer(123)) >>> _ 0
- class sage.repl.rich_output.backend_ipython.BackendIPythonCommandline[source]¶
Bases:
BackendIPythonBackend for the IPython Command Line.
EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: BackendIPythonCommandline() IPython command line
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> BackendIPythonCommandline() IPython command line
- default_preferences()[source]¶
Return the backend’s display preferences.
The default for the commandline is to not plot graphs since the launching of an external viewer is considered too disruptive.
OUTPUT:
Instance of
DisplayPreferences.EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() sage: backend.default_preferences() Display preferences: * align_latex is not specified * graphics is not specified * supplemental_plot = never * text is not specified
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> backend = BackendIPythonCommandline() >>> backend.default_preferences() Display preferences: * align_latex is not specified * graphics is not specified * supplemental_plot = never * text is not specified
- display_immediately(plain_text, rich_output)[source]¶
Show output without going back to the command line prompt.
This method is similar to the rich output
displayhook(), except that it can be invoked at any time. On the Sage command line it launches viewers just likedisplayhook().INPUT:
Same as
displayhook().OUTPUT: this method does not return anything
EXAMPLES:
sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() sage: backend.display_immediately(plain_text, plain_text) Example plain text output
>>> from sage.all import * >>> from sage.repl.rich_output.output_basic import OutputPlainText >>> plain_text = OutputPlainText.example() >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> backend = BackendIPythonCommandline() >>> backend.display_immediately(plain_text, plain_text) Example plain text output
- displayhook(plain_text, rich_output)[source]¶
Backend implementation of the displayhook.
INPUT:
plain_text– instance ofOutputPlainText. The plain text version of the output.rich_output– instance of an output container class (subclass ofOutputBase). Guaranteed to be one of the output containers returned fromsupported_output(), possibly the same asplain_text.
OUTPUT:
The IPython commandline display hook returns the IPython display data, a pair of dictionaries. The first dictionary contains mime types as keys and the respective output as value. The second dictionary is metadata.
EXAMPLES:
sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() sage: backend.displayhook(plain_text, plain_text) ({'text/plain': 'Example plain text output'}, {})
>>> from sage.all import * >>> from sage.repl.rich_output.output_basic import OutputPlainText >>> plain_text = OutputPlainText.example() >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> backend = BackendIPythonCommandline() >>> backend.displayhook(plain_text, plain_text) ({'text/plain': 'Example plain text output'}, {})
- is_in_terminal()[source]¶
Test whether the UI is meant to run in a terminal.
See
sage.repl.rich_output.display_manager.DisplayManager.is_in_terminal()for details.OUTPUT:
Truefor the IPython commandlineEXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() sage: backend.is_in_terminal() True
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> backend = BackendIPythonCommandline() >>> backend.is_in_terminal() True
- launch_jmol(output_jmol, plain_text)[source]¶
Launch the stand-alone jmol viewer.
INPUT:
output_jmol–OutputSceneJmol; the scene to launch Jmol withplain_text– string; the plain text representation
OUTPUT:
string; human-readable message indicating that the viewer was launched.
EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() sage: from sage.repl.rich_output.output_graphics3d import OutputSceneJmol sage: backend.launch_jmol(OutputSceneJmol.example(), 'Graphics3d object') # needs sage.plot 'Launched jmol viewer for Graphics3d object'
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> backend = BackendIPythonCommandline() >>> from sage.repl.rich_output.output_graphics3d import OutputSceneJmol >>> backend.launch_jmol(OutputSceneJmol.example(), 'Graphics3d object') # needs sage.plot 'Launched jmol viewer for Graphics3d object'
- launch_viewer(image_file, plain_text)[source]¶
Launch external viewer for the graphics file.
INPUT:
image_file– string; file name of the image fileplain_text– string; the plain text representation of the image file
OUTPUT:
String. Human-readable message indicating whether the viewer was launched successfully.
EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() sage: backend.launch_viewer('/path/to/foo.bar', 'Graphics object') 'Launched bar viewer for Graphics object'
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> backend = BackendIPythonCommandline() >>> backend.launch_viewer('/path/to/foo.bar', 'Graphics object') 'Launched bar viewer for Graphics object'
- supported_output()[source]¶
Return the outputs that are supported by the IPython commandline backend.
OUTPUT:
Iterable of output container classes, that is, subclass of
OutputBase). The order is ignored.EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() sage: supp = backend.supported_output(); supp # random output set([<class 'sage.repl.rich_output.output_graphics.OutputImageGif'>, ..., <class 'sage.repl.rich_output.output_graphics.OutputImagePng'>]) sage: from sage.repl.rich_output.output_basic import OutputLatex sage: OutputLatex in supp True
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> backend = BackendIPythonCommandline() >>> supp = backend.supported_output(); supp # random output set([<class 'sage.repl.rich_output.output_graphics.OutputImageGif'>, ..., <class 'sage.repl.rich_output.output_graphics.OutputImagePng'>]) >>> from sage.repl.rich_output.output_basic import OutputLatex >>> OutputLatex in supp True
- threejs_offline_scripts()[source]¶
Three.js script for the IPython command line.
OUTPUT: string containing script tag
EXAMPLES:
sage: # needs threejs sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline sage: backend = BackendIPythonCommandline() sage: backend.threejs_offline_scripts() '...<script ...</script>...'
>>> from sage.all import * >>> # needs threejs >>> from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline >>> backend = BackendIPythonCommandline() >>> backend.threejs_offline_scripts() '...<script ...</script>...'
- class sage.repl.rich_output.backend_ipython.BackendIPythonNotebook[source]¶
Bases:
BackendIPythonBackend for the IPython Notebook.
EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: BackendIPythonNotebook() IPython notebook
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook >>> BackendIPythonNotebook() IPython notebook
- displayhook(plain_text, rich_output)[source]¶
Backend implementation of the displayhook.
INPUT:
plain_text– instance ofOutputPlainText. The plain text version of the output.rich_output– instance of an output container class (subclass ofOutputBase). Guaranteed to be one of the output containers returned fromsupported_output(), possibly the same asplain_text.
OUTPUT:
The IPython notebook display hook returns the IPython display data, a pair of dictionaries. The first dictionary contains mime types as keys and the respective output as value. The second dictionary is metadata.
EXAMPLES:
sage: from sage.repl.rich_output.output_basic import OutputPlainText sage: plain_text = OutputPlainText.example() sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: backend = BackendIPythonNotebook() sage: backend.displayhook(plain_text, plain_text) ({'text/plain': 'Example plain text output'}, {})
>>> from sage.all import * >>> from sage.repl.rich_output.output_basic import OutputPlainText >>> plain_text = OutputPlainText.example() >>> from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook >>> backend = BackendIPythonNotebook() >>> backend.displayhook(plain_text, plain_text) ({'text/plain': 'Example plain text output'}, {})
- supported_output()[source]¶
Return the outputs that are supported by the IPython notebook backend.
OUTPUT:
Iterable of output container classes, that is, subclass of
OutputBase). The order is ignored.EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: backend = BackendIPythonNotebook() sage: supp = backend.supported_output(); supp # random output set([<class 'sage.repl.rich_output.output_graphics.OutputPlainText'>, ..., <class 'sage.repl.rich_output.output_graphics.OutputImagePdf'>]) sage: from sage.repl.rich_output.output_basic import OutputLatex sage: OutputLatex in supp True sage: from sage.repl.rich_output.output_graphics import OutputImageGif sage: OutputImageGif in supp True
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook >>> backend = BackendIPythonNotebook() >>> supp = backend.supported_output(); supp # random output set([<class 'sage.repl.rich_output.output_graphics.OutputPlainText'>, ..., <class 'sage.repl.rich_output.output_graphics.OutputImagePdf'>]) >>> from sage.repl.rich_output.output_basic import OutputLatex >>> OutputLatex in supp True >>> from sage.repl.rich_output.output_graphics import OutputImageGif >>> OutputImageGif in supp True
- threejs_offline_scripts()[source]¶
Three.js script for the IPython notebook.
OUTPUT: string containing script tag
EXAMPLES:
sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook sage: backend = BackendIPythonNotebook() sage: backend.threejs_offline_scripts() # needs sage.plot '...<script src="/nbextensions/threejs-sage/r.../three.min.js...<\/script>...'
>>> from sage.all import * >>> from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook >>> backend = BackendIPythonNotebook() >>> backend.threejs_offline_scripts() # needs sage.plot '...<script src="/nbextensions/threejs-sage/r.../three.min.js...<\/script>...'